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

voidinstallKey(final JComponent comp, final String keyString, final int key, final Action action)
install Key
KeyStroke enterKeyStroke = KeyStroke.getKeyStroke(key, 0, false);
comp.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(enterKeyStroke, keyString);
comp.getRootPane().getActionMap().put(keyString, action);
voidinstallKeyBinding(JComponent c, int condition, String actionName, Action action, KeyStroke... keyStrokes)
install Key Binding
InputMap iMap = c.getInputMap(condition);
ActionMap aMap = c.getActionMap();
for (KeyStroke keyStroke : keyStrokes) {
    iMap.put(keyStroke, actionName);
    aMap.put(actionName, action);
voidinstallKeyBindings()
install Key Bindings
if (isMacOSX()) {
    final KeyStroke copyKeyStroke = KeyStroke.getKeyStroke("meta pressed C");
    final KeyStroke pasteKeyStroke = KeyStroke.getKeyStroke("meta pressed V");
    final KeyStroke cutKeyStroke = KeyStroke.getKeyStroke("meta pressed X");
    final KeyStroke allKeyStroke = KeyStroke.getKeyStroke("meta pressed A");
    InputMap textFieldMap = (InputMap) UIManager.get("TextField.focusInputMap");
    textFieldMap.put(copyKeyStroke, DefaultEditorKit.copyAction);
    textFieldMap.put(pasteKeyStroke, DefaultEditorKit.pasteAction);
...
voidinstallKeystrokes(JComponent component, Action... actions)
Install the keystrokes for several actions on a single button.
for (Action action : actions) {
    KeyStroke stroke = (KeyStroke) action.getValue(Action.ACCELERATOR_KEY);
    if (stroke != null) {
        InputMap keyMap = component.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        keyMap.put(stroke, action);
        ActionMap actionMap = component.getActionMap();
        actionMap.put(action, action);
booleanisKeyStroke(String propClass)
is Key Stroke
return ("javax.swing.KeyStroke".equals(propClass));
voidmapInput(JComponent component, int scope, final int keycode, final int modifiers, final AbstractAction action)
map Input
if (component instanceof JComponent) {
    JComponent jc = (JComponent) component;
    String hash = String.valueOf(System.identityHashCode(action));
    jc.getInputMap(scope).put(KeyStroke.getKeyStroke(keycode, modifiers), hash);
    jc.getActionMap().put(hash, action);
voidmapKeyStrokeAction(JComponent component, String actionMapKey, Action action, KeyStroke keyStroke)
map Key Stroke Action
component.getActionMap().put(actionMapKey, action);
component.getInputMap().put(keyStroke, actionMapKey);
voidmapKeyStrokeToAction(final JComponent comp, final String ks, final String name, final AbstractAction action)
Adds to JComponent action and keystroke for it.
comp.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(ks), name);
comp.getActionMap().put(name, action);
voidmapKeyStrokeToButton(final JComponent comp, final String ks, final String name, final AbstractButton button)
Adds to JComponent button and keystroke for it.
comp.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke(ks), name);
comp.getActionMap().put(name, new AbstractAction() {
    private static final long serialVersionUID = -1;
    public void actionPerformed(ActionEvent evt) {
        button.doClick();
});
voidmaybeInstall(InputMap map, String action, KeyStroke stroke)
maybe Install
if (map.get(stroke) == null) {
    map.put(stroke, action);