Java Swing KeyStroke getKeyStroke(String s)

Here you can find the source of getKeyStroke(String s)

Description

get Key Stroke

License

Apache License

Declaration

static KeyStroke getKeyStroke(String s) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import static java.awt.event.InputEvent.CTRL_DOWN_MASK;
import static java.awt.event.InputEvent.CTRL_MASK;
import java.awt.*;

import javax.swing.*;

public class Main {
    static KeyStroke getKeyStroke(String s) {
        KeyStroke ks = KeyStroke.getKeyStroke(s);
        if (ks != null && s.matches("(?i).*Ctrl.*")) {
            return convertShortcutMask(ks, getMenuShortcutKeyMask());
        }//from w w w.ja va  2  s .co m
        return ks;
    }

    static KeyStroke convertShortcutMask(KeyStroke ks, int shortcutMask) {
        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;
    }

    static int getMenuShortcutKeyMask() {
        return Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
    }
}

Related

  1. createItem(String name, Integer mnem, KeyStroke accel)
  2. createKeyMask(final int aKeyStroke, final int... aMasks)
  3. fixTabKeys(final JComponent component)
  4. getAccelerator(final int key)
  5. getActionForKeystroke(JComponent component, int inputMapId, KeyStroke keyStroke)
  6. getKeyStrokeWithoutCtrlModifier(KeyStroke stroke)
  7. getMnemonic(final String key)
  8. installAction(JComponent comp, Action action, KeyStroke keyStroke, String actionKey)
  9. installKey(final JComponent comp, final String keyString, final int key, final Action action)