Example usage for javax.swing KeyStroke getKeyCode

List of usage examples for javax.swing KeyStroke getKeyCode

Introduction

In this page you can find the example usage for javax.swing KeyStroke getKeyCode.

Prototype

public final int getKeyCode() 

Source Link

Document

Returns the numeric key code for this AWTKeyStroke .

Usage

From source file:com.maddyhome.idea.vim.helper.StringHelper.java

@NotNull
public static String escape(@NotNull List<KeyStroke> keys) {
    final StringBuffer res = new StringBuffer();
    for (KeyStroke key : keys) {
        final char c = key.getKeyChar();
        final int modifiers = key.getModifiers();
        final int code = key.getKeyCode();
        if (c < ' ') {
            res.append('^').append((char) (c + 'A' - 1));
        } else if (c == '\n') {
            res.append("^J");
        } else if (c == '\t') {
            res.append("^I");
        } else if (c == '\u0000') {
            res.append("^@");
        } else if ((modifiers & KeyEvent.CTRL_DOWN_MASK) != 0) {
            final char[] chars = Character.toChars(code);
            if (chars.length == 1) {
                res.append("^");
                res.append(chars);/*from   ww w. j  a v  a 2s  .  c om*/
            }
        } else {
            res.append(c);
        }
    }
    return res.toString();
}

From source file:Utilities.java

/** Converts a Swing key stroke descriptor to a familiar Emacs-like name.
* @param stroke key description//w  ww  .  j  a  va2  s.  co  m
* @return name of the key (e.g. <code>CS-F1</code> for control-shift-function key one)
* @see #stringToKey
*/
public static String keyToString(KeyStroke stroke) {
    StringBuffer sb = new StringBuffer();

    // add modifiers that must be pressed
    if (addModifiers(sb, stroke.getModifiers())) {
        sb.append('-');
    }

    HashMap[] namesAndValues = initNameAndValues();

    String c = (String) namesAndValues[1].get(new Integer(stroke.getKeyCode()));

    if (c == null) {
        sb.append(stroke.getKeyChar());
    } else {
        sb.append(c);
    }

    return sb.toString();
}

From source file:com.mirth.connect.client.ui.components.KeyStrokeTextField.java

public void setKeyStroke(KeyStroke keyStroke) {
    if (keyStroke != null) {
        keyCode = keyStroke.getKeyCode();
        modifiers = keyStroke.getModifiers();
        updateKeyStroke();//  www.java2  s. c om
    } else {
        reset();
    }
}

From source file:edu.ku.brc.af.ui.forms.ResultSetController.java

/**
 * Helper method for creating the menu items.
 * @param menuItem the parent menu it for the menu items
 * @param titleKey the I18N key for the button title
 * @param menuKey the I18N key for the mneumonic 
 * @param cmdType the command/*from w ww . j  av  a2 s  .c  o  m*/
 */
protected static void createMenuItem(final JMenuItem menuItem, final String titleKey,
        final CommandType cmdType) {
    KeyStroke ks = UIHelper.getKeyStroke(cmdType);
    JMenuItem mi = new JMenuItem(UIRegistry.getInstance().makeAction(commandsHash.get(cmdType),
            getResourceString(titleKey), null, "", ks.getKeyCode(), ks));
    menuItem.add(mi);
}

From source file:Main.java

/**
 * Returns the key stroke representation translated.
 * /*from  ww  w. j  a v  a 2s.com*/
 * @param keyStroke The key stroke.
 * @param locale The locale.
 * @return The key stroke representation translated.
 */
public static String translate(KeyStroke keyStroke, Locale locale) {
    //      String[] tokens = StringUtils.parse(StringUtils.toString(keyStroke), " ");
    //      if (tokens.length > 1) {
    //         Arrays.sort(tokens, 0, tokens.length - 1);
    //      }
    //      StringBuilder b = new StringBuilder();
    //      for (int i = 0; i < tokens.length; i++) {
    //         String token = tokens[i];
    //         String key = "key_" + token;
    //         String translated = TextServer.getString(key, locale);
    //         if (translated.equals(TextServer.notFoundKey(key))) {
    //            translated = token;
    //         }
    //         if (i > 0) {
    //            b.append("+");
    //         }
    //         b.append(translated);
    //      }
    //      return b.toString();
    String acceleratorText = "";
    if (keyStroke != null) {
        int modifiers = keyStroke.getModifiers();
        if (modifiers > 0) {
            acceleratorText = KeyEvent.getKeyModifiersText(modifiers);
            acceleratorText += "+";
        }
        acceleratorText += KeyEvent.getKeyText(keyStroke.getKeyCode());
    }
    return acceleratorText;
}

From source file:com.haulmont.cuba.desktop.gui.components.DesktopFilterHelper.java

@Override
public void addShortcutListener(TextField textField, final ShortcutListener listener) {
    final JTextField dTextField = (JTextField) DesktopComponentsHelper.unwrap(textField);
    final KeyStroke keyStroke = DesktopComponentsHelper.convertKeyCombination(listener.getKeyCombination());
    dTextField.addKeyListener(new KeyAdapter() {
        @Override/*from  w ww.j a  v  a  2  s . c  om*/
        public void keyReleased(KeyEvent e) {
            if (ObjectUtils.equals(e.getKeyCode(), keyStroke.getKeyCode())
                    && ObjectUtils.equals(e.getModifiers(), keyStroke.getModifiers())) {
                listener.handleShortcutPressed();
            }
        }
    });
}

From source file:Main.java

static void keyStroke2String(KeyStroke key) {
    int m = key.getModifiers();

    if ((m & (InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK)) != 0) {
        System.out.println("shift ");
    }// w w w  .j a va  2s. com
    if ((m & (InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK)) != 0) {
        System.out.println("ctrl ");
    }
    if ((m & (InputEvent.META_DOWN_MASK | InputEvent.META_MASK)) != 0) {
        System.out.println("meta ");
    }
    if ((m & (InputEvent.ALT_DOWN_MASK | InputEvent.ALT_MASK)) != 0) {
        System.out.println("alt ");
    }
    if ((m & (InputEvent.BUTTON1_DOWN_MASK | InputEvent.BUTTON1_MASK)) != 0) {
        System.out.println("button1 ");
    }
    if ((m & (InputEvent.BUTTON2_DOWN_MASK | InputEvent.BUTTON2_MASK)) != 0) {
        System.out.println("button2 ");
    }
    if ((m & (InputEvent.BUTTON3_DOWN_MASK | InputEvent.BUTTON3_MASK)) != 0) {
        System.out.println("button3 ");
    }
    switch (key.getKeyEventType()) {
    case KeyEvent.KEY_TYPED:
        System.out.println("typed ");
        System.out.println(key.getKeyChar() + " ");
        break;
    case KeyEvent.KEY_PRESSED:
        System.out.println("pressed ");
        System.out.println(getKeyText(key.getKeyCode()) + " ");
        break;
    case KeyEvent.KEY_RELEASED:
        System.out.println("released ");
        System.out.println(getKeyText(key.getKeyCode()) + " ");
        break;
    default:
        System.out.println("unknown-event-type ");
        break;
    }
}

From source file:Main.java

static void keyStroke2String(KeyStroke key) {
    int m = key.getModifiers();

    if ((m & (InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK)) != 0) {
        System.out.println("shift ");
    }/*from  w  ww .  ja va  2  s  .co  m*/
    if ((m & (InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK)) != 0) {
        System.out.println("ctrl ");
    }
    if ((m & (InputEvent.META_DOWN_MASK | InputEvent.META_MASK)) != 0) {
        System.out.println("meta ");
    }
    if ((m & (InputEvent.ALT_DOWN_MASK | InputEvent.ALT_MASK)) != 0) {
        System.out.println("alt ");
    }
    if ((m & (InputEvent.BUTTON1_DOWN_MASK | InputEvent.BUTTON1_MASK)) != 0) {
        System.out.println("button1 ");
    }
    if ((m & (InputEvent.BUTTON2_DOWN_MASK | InputEvent.BUTTON2_MASK)) != 0) {
        System.out.println("button2 ");
    }
    if ((m & (InputEvent.BUTTON3_DOWN_MASK | InputEvent.BUTTON3_MASK)) != 0) {
        System.out.println("button3 ");
    }

    switch (key.getKeyEventType()) {
    case KeyEvent.KEY_TYPED:
        System.out.println("typed ");
        System.out.println(key.getKeyChar() + " ");
        break;
    case KeyEvent.KEY_PRESSED:
        System.out.println("pressed ");
        System.out.println(getKeyText(key.getKeyCode()) + " ");
        break;
    case KeyEvent.KEY_RELEASED:
        System.out.println("released ");
        System.out.println(getKeyText(key.getKeyCode()) + " ");
        break;
    default:
        System.out.println("unknown-event-type ");
        break;
    }
}

From source file:KeystrokeUtil.java

public static String keyStroke2String(KeyStroke key) {
    if (key == null)
        return "";
    StringBuilder s = new StringBuilder(50);
    int m = key.getModifiers();

    if ((m & (InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK)) != 0) {
        s.append("Ctrl+");
    }/*w w  w .jav  a 2s  .  com*/
    if ((m & (InputEvent.META_DOWN_MASK | InputEvent.META_MASK)) != 0) {
        s.append("Meta+");
    }
    if ((m & (InputEvent.ALT_DOWN_MASK | InputEvent.ALT_MASK)) != 0) {
        s.append("Alt+");
    }
    if ((m & (InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK)) != 0) {
        s.append("Shift+");
    }
    if ((m & (InputEvent.BUTTON1_DOWN_MASK | InputEvent.BUTTON1_MASK)) != 0) {
        s.append("Button1+");
    }
    if ((m & (InputEvent.BUTTON2_DOWN_MASK | InputEvent.BUTTON2_MASK)) != 0) {
        s.append("Button2+");
    }
    if ((m & (InputEvent.BUTTON3_DOWN_MASK | InputEvent.BUTTON3_MASK)) != 0) {
        s.append("Button3+");
    }

    switch (key.getKeyEventType()) {
    case KeyEvent.KEY_TYPED:
        s.append(key.getKeyChar() + " ");
        break;
    case KeyEvent.KEY_PRESSED:
    case KeyEvent.KEY_RELEASED:
        s.append(getKeyText(key.getKeyCode()) + " ");
        break;
    default:
        s.append("unknown-event-type ");
        break;
    }

    return s.toString();
}

From source file:Main.java

/**
 * Serializes a Keystrike to a String./*from   ww  w.  ja  v  a2s. c o m*/
 * It can then be fed into KeyStroke.getKeyStroke(String).
 *
 * See: http://www.exampledepot.com/egs/javax.swing/Key2Str.html
 */
public static String keyStroke2String(KeyStroke key) {
    StringBuffer s = new StringBuffer(50);
    int m = key.getModifiers();

    if ((m & (InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK)) != 0) {
        s.append("shift ");
    }
    if ((m & (InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK)) != 0) {
        s.append("ctrl ");
    }
    if ((m & (InputEvent.META_DOWN_MASK | InputEvent.META_MASK)) != 0) {
        s.append("meta ");
    }
    if ((m & (InputEvent.ALT_DOWN_MASK | InputEvent.ALT_MASK)) != 0) {
        s.append("alt ");
    }
    if ((m & (InputEvent.BUTTON1_DOWN_MASK | InputEvent.BUTTON1_MASK)) != 0) {
        s.append("button1 ");
    }
    if ((m & (InputEvent.BUTTON2_DOWN_MASK | InputEvent.BUTTON2_MASK)) != 0) {
        s.append("button2 ");
    }
    if ((m & (InputEvent.BUTTON3_DOWN_MASK | InputEvent.BUTTON3_MASK)) != 0) {
        s.append("button3 ");
    }

    switch (key.getKeyEventType()) {
    case KeyEvent.KEY_TYPED:
        if (key.getKeyChar() == Character.MAX_VALUE) {
            s.append("unknown-typed-key ");
            break;
        }
        s.append("typed ").append(key.getKeyChar()).append(" ");
        break;
    case KeyEvent.KEY_PRESSED:
        s.append("pressed ").append(getKeyText(key.getKeyCode())).append(" ");
        break;
    case KeyEvent.KEY_RELEASED:
        s.append("released ").append(getKeyText(key.getKeyCode())).append(" ");
        break;
    default:
        s.append("unknown-event-type ");
        break;
    }

    return s.toString();
}