/** * This is hack. AWT doesn't allow to create KeyStroke with specified key code and key char * simultaneously. Therefore we are using reflection. */ private static KeyStroke getKeyStrokeWithoutMouseModifiers(KeyStroke originalKeyStroke) { int modifier = originalKeyStroke.getModifiers() & ~InputEvent.BUTTON1_DOWN_MASK & ~InputEvent.BUTTON1_MASK & ~InputEvent.BUTTON2_DOWN_MASK & ~InputEvent.BUTTON2_MASK & ~InputEvent.BUTTON3_DOWN_MASK & ~InputEvent.BUTTON3_MASK; try { Method[] methods = AWTKeyStroke.class.getDeclaredMethods(); Method getCachedStrokeMethod = null; for (Method method : methods) { if (GET_CACHED_STROKE_METHOD_NAME.equals(method.getName())) { getCachedStrokeMethod = method; getCachedStrokeMethod.setAccessible(true); break; } } if (getCachedStrokeMethod == null) { throw new IllegalStateException("not found method with name getCachedStrokeMethod"); } Object[] getCachedStrokeMethodArgs = new Object[] { originalKeyStroke.getKeyChar(), originalKeyStroke.getKeyCode(), modifier, originalKeyStroke.isOnKeyRelease() }; return (KeyStroke) getCachedStrokeMethod.invoke(originalKeyStroke, getCachedStrokeMethodArgs); } catch (Exception exc) { throw new IllegalStateException(exc.getMessage()); } }
private int getMnemonicCharIndex(String text) { final int mnemonicIndex = myPresentation.getDisplayedMnemonicIndex(); if (mnemonicIndex != -1) { return mnemonicIndex; } final ShortcutSet shortcutSet = myAction.getShortcutSet(); final Shortcut[] shortcuts = shortcutSet.getShortcuts(); for (Shortcut shortcut : shortcuts) { if (!(shortcut instanceof KeyboardShortcut)) continue; KeyboardShortcut keyboardShortcut = (KeyboardShortcut) shortcut; if (keyboardShortcut.getSecondKeyStroke() == null) { // we are interested only in "mnemonic-like" shortcuts final KeyStroke keyStroke = keyboardShortcut.getFirstKeyStroke(); final int modifiers = keyStroke.getModifiers(); if (BitUtil.isSet(modifiers, InputEvent.ALT_MASK)) { return (keyStroke.getKeyChar() != KeyEvent.CHAR_UNDEFINED) ? text.indexOf(keyStroke.getKeyChar()) : text.indexOf(KeyEvent.getKeyText(keyStroke.getKeyCode())); } } } return -1; }