Java Utililty Methods Swing KeyStroke

List of utility methods to do Swing KeyStroke

Description

The list of methods to do Swing KeyStroke are organized into topic(s).

Method

KeyStrokeconvertShortcutMask(KeyStroke ks, int shortcutMask)
convert Shortcut Mask
final int mod = ks.getModifiers();
if ((mod & (CTRL_DOWN_MASK | CTRL_MASK)) != 0) {
    final int newmod = mod & ~(CTRL_DOWN_MASK | CTRL_MASK) | shortcutMask;
    return KeyStroke.getKeyStroke(ks.getKeyCode(), newmod);
return ks;
JMenuItemcreateItem(String name, Integer mnem, KeyStroke accel)
Creates and configures a menu item.
JMenuItem item = new JMenuItem(name);
if (mnem != null) {
    item.setMnemonic(mnem.intValue());
if (accel != null) {
    item.setAccelerator(accel);
return item;
...
KeyStrokecreateKeyMask(final int aKeyStroke, final int... aMasks)
Convenience method to create a key mask.
int modifiers = 0;
for (int aMask : aMasks) {
    modifiers |= aMask;
return KeyStroke.getKeyStroke(aKeyStroke, modifiers);
voidfixTabKeys(final JComponent component)
This method sets the FocusTraversalKeys for a component to be the standard keys.
final Set<AWTKeyStroke> forward = new HashSet<AWTKeyStroke>(
        component.getFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS));
forward.add(KeyStroke.getKeyStroke("TAB"));
component.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, forward);
final Set<AWTKeyStroke> backward = new HashSet<AWTKeyStroke>(
        component.getFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS));
backward.add(KeyStroke.getKeyStroke("shift TAB"));
component.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, backward);
...
KeyStrokegetAccelerator(final int key)
Returns the KeyStroke for a key combined with the platform dependent menu shortcut key.
return KeyStroke.getKeyStroke(key, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask());
ActiongetActionForKeystroke(JComponent component, int inputMapId, KeyStroke keyStroke)
Obtine actiunea inregistrata pentru apasarea de tasta
InputMap inputMap = component.getInputMap(inputMapId);
String key = (String) inputMap.get(keyStroke);
if (key != null) {
    ActionMap localMap = component.getActionMap();
    return localMap.get(key);
} else {
    return null;
KeyStrokegetKeyStroke(String s)
get Key Stroke
KeyStroke ks = KeyStroke.getKeyStroke(s);
if (ks != null && s.matches("(?i).*Ctrl.*")) {
    return convertShortcutMask(ks, getMenuShortcutKeyMask());
return ks;
KeyStrokegetKeyStrokeWithoutCtrlModifier(KeyStroke stroke)
get Key Stroke Without Ctrl Modifier
try {
    Method method = AWTKeyStroke.class.getDeclaredMethod("getCachedStroke", char.class, int.class,
            int.class, boolean.class);
    method.setAccessible(true);
    int modifiers = stroke.getModifiers() & ~InputEvent.CTRL_MASK & ~InputEvent.CTRL_DOWN_MASK;
    return (KeyStroke) method.invoke(null, stroke.getKeyChar(), stroke.getKeyCode(), modifiers,
            stroke.isOnKeyRelease());
} catch (Exception exception) {
...
intgetMnemonic(final String key)
Returns the mnemonic for a key.
return KeyStroke.getKeyStroke(key).getKeyCode();
voidinstallAction(JComponent comp, Action action, KeyStroke keyStroke, String actionKey)
install Action
comp.getActionMap().put(actionKey, action);
comp.getInputMap().put(keyStroke, actionKey);