Java Code Examples for javax.swing.KeyStroke#getKeyCode()
The following examples show how to use
javax.swing.KeyStroke#getKeyCode() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DockingToolBarUtils.java From ghidra with Apache License 2.0 | 6 votes |
private static String getKeyBindingAcceleratorText(JButton button, KeyStroke keyStroke) { if (keyStroke == null) { return null; } // This code is based on that of BasicMenuItemUI StringBuilder builder = new StringBuilder(); int modifiers = keyStroke.getModifiers(); if (modifiers > 0) { builder.append(InputEvent.getModifiersExText(modifiers)); // The Aqua LaF does not use the '+' symbol between modifiers if (!DockingWindowsLookAndFeelUtils.isUsingAquaUI(button.getUI())) { builder.append('+'); } } int keyCode = keyStroke.getKeyCode(); if (keyCode != 0) { builder.append(KeyEvent.getKeyText(keyCode)); } else { builder.append(keyStroke.getKeyChar()); } return builder.toString(); }
Example 2
Source File: AbstractDockingTest.java From ghidra with Apache License 2.0 | 6 votes |
/** * Simulates a user initiated keystroke using the keybinding of the given action * * @param destination the component for the action being executed * @param action The action to simulate pressing */ public static void triggerActionKey(Component destination, DockingActionIf action) { Objects.requireNonNull(destination); KeyStroke keyStroke = action.getKeyBinding(); if (keyStroke == null) { throw new IllegalArgumentException("No KeyStroke assigned for the given action"); } int modifiers = keyStroke.getModifiers(); int keyCode = keyStroke.getKeyCode(); char keyChar = keyStroke.getKeyChar(); boolean isDefined = Character.isDefined(keyChar); if (!isDefined) { keyChar = KeyEvent.VK_UNDEFINED; } triggerKey(destination, modifiers, keyCode, keyChar); }
Example 3
Source File: Actions.java From netbeans with Apache License 2.0 | 6 votes |
/** Same method as above, but works just with plain actions. */ private static String findKey(Action action) { if (action == null) { return null; } KeyStroke accelerator = (KeyStroke) action.getValue(Action.ACCELERATOR_KEY); if (accelerator == null) { return null; } int modifiers = accelerator.getModifiers(); String acceleratorText = ""; // NOI18N if (modifiers > 0) { acceleratorText = KeyEvent.getKeyModifiersText(modifiers); acceleratorText += "+"; // NOI18N } else if (accelerator.getKeyCode() == KeyEvent.VK_UNDEFINED) { return ""; // NOI18N } acceleratorText += KeyEvent.getKeyText(accelerator.getKeyCode()); return acceleratorText; }
Example 4
Source File: KeyShortcut.java From netbeans-mmd-plugin with Apache License 2.0 | 5 votes |
public boolean doesConflictWith(@Nullable final KeyStroke stroke) { boolean result = false; if (stroke != null) { result = stroke.getKeyCode() == this.keyCode && (this.modifiers & stroke.getModifiers()) == this.modifiers; } return result; }
Example 5
Source File: Utils.java From netbeans-mmd-plugin with Apache License 2.0 | 5 votes |
public static boolean isKeyStrokeEvent(@Nullable final KeyStroke keyStroke, final int keyEventType, @Nullable final KeyEvent event) { boolean result = false; if (keyStroke != null && event != null) { if (keyEventType == keyStroke.getKeyEventType()) { result = ((keyStroke.getModifiers() & event.getModifiers()) == keyStroke.getModifiers()) && (keyStroke.getKeyCode() == event.getKeyCode()); } } return result; }
Example 6
Source File: SearchResultRender.java From netbeans with Apache License 2.0 | 5 votes |
static String getKeyStrokeAsText (KeyStroke keyStroke) { if (keyStroke == null) return ""; int modifiers = keyStroke.getModifiers (); StringBuffer sb = new StringBuffer (); if ((modifiers & InputEvent.CTRL_DOWN_MASK) > 0) sb.append ("Ctrl+"); if ((modifiers & InputEvent.ALT_DOWN_MASK) > 0) sb.append ("Alt+"); if ((modifiers & InputEvent.SHIFT_DOWN_MASK) > 0) sb.append ("Shift+"); if ((modifiers & InputEvent.META_DOWN_MASK) > 0) if (Utilities.isMac()) { // Mac cloverleaf symbol sb.append ("\u2318+"); } else if (isSolaris()) { // Sun meta symbol sb.append ("\u25C6+"); } else { sb.append ("Meta+"); } if (keyStroke.getKeyCode () != KeyEvent.VK_SHIFT && keyStroke.getKeyCode () != KeyEvent.VK_CONTROL && keyStroke.getKeyCode () != KeyEvent.VK_META && keyStroke.getKeyCode () != KeyEvent.VK_ALT && keyStroke.getKeyCode () != KeyEvent.VK_ALT_GRAPH ) sb.append (Utilities.keyToString ( KeyStroke.getKeyStroke (keyStroke.getKeyCode (), 0) )); return sb.toString (); }
Example 7
Source File: KeyStrokeUtils.java From netbeans with Apache License 2.0 | 5 votes |
/** * Get human-readable name for a {@link KeyStroke}. */ public static String getKeyStrokeAsText(@NonNull KeyStroke keyStroke) { int modifiers = keyStroke.getModifiers (); StringBuilder sb = new StringBuilder (); if ((modifiers & InputEvent.CTRL_DOWN_MASK) > 0) { sb.append(EMACS_CTRL); } if ((modifiers & InputEvent.ALT_DOWN_MASK) > 0) { sb.append(STRING_ALT); } if ((modifiers & InputEvent.SHIFT_DOWN_MASK) > 0) { sb.append (EMACS_SHIFT); } if ((modifiers & InputEvent.META_DOWN_MASK) > 0) { sb.append(STRING_META); } if (keyStroke.getKeyCode () != KeyEvent.VK_SHIFT && keyStroke.getKeyCode () != KeyEvent.VK_CONTROL && keyStroke.getKeyCode () != KeyEvent.VK_META && keyStroke.getKeyCode () != KeyEvent.VK_ALT && keyStroke.getKeyCode () != KeyEvent.VK_ALT_GRAPH) { sb.append (Utilities.keyToString ( KeyStroke.getKeyStroke (keyStroke.getKeyCode (), 0) )); } return sb.toString (); }
Example 8
Source File: Utilities.java From netbeans with Apache License 2.0 | 5 votes |
/** * Creates nice textual representation of KeyStroke. * Modifiers and an actual key label are concated by plus signs * @param the KeyStroke to get description of * @return String describing the KeyStroke */ public static String keyStrokeToString( KeyStroke stroke ) { String modifText = KeyEvent.getKeyModifiersText( stroke.getModifiers() ); String keyText = (stroke.getKeyCode() == KeyEvent.VK_UNDEFINED) ? String.valueOf(stroke.getKeyChar()) : getKeyText(stroke.getKeyCode()); if( modifText.length() > 0 ) return modifText + '+' + keyText; else return keyText; }
Example 9
Source File: OSUtils.java From marathonv5 with Apache License 2.0 | 5 votes |
private static CharSequence getKeys(String keysFor, String sKeyStroke) { KeyStroke ks = KeyStroke.getKeyStroke(sKeyStroke); if (ks == null) { throw new WebDriverException("Unable to parse keystroke for " + keysFor + " trying to parse " + sKeyStroke); } StringBuilder sb = new StringBuilder(); int modifiers = ks.getModifiers(); if ((modifiers & InputEvent.CTRL_DOWN_MASK) == InputEvent.CTRL_DOWN_MASK) { sb.append(Keys.CONTROL); } if ((modifiers & InputEvent.ALT_DOWN_MASK) == InputEvent.ALT_DOWN_MASK) { sb.append(Keys.ALT); } if ((modifiers & InputEvent.META_DOWN_MASK) == InputEvent.META_DOWN_MASK) { sb.append(Keys.META); } if ((modifiers & InputEvent.SHIFT_DOWN_MASK) == InputEvent.SHIFT_DOWN_MASK) { sb.append(Keys.SHIFT); } int keyCode = ks.getKeyCode(); if (keyCode == KeyEvent.VK_UNDEFINED) { sb.append(ks.getKeyChar()); } else { sb.append(keyCharFromKeyCode(keyCode, keysFor)); } sb.append(Keys.NULL); return sb.toString(); }
Example 10
Source File: KeyStrokeDisplay.java From gcs with Mozilla Public License 2.0 | 5 votes |
/** * @param ks The {@link KeyStroke} to use. * @return The text that represents the {@link KeyStroke}. */ public static String getKeyStrokeDisplay(KeyStroke ks) { StringBuilder buffer = new StringBuilder(); if (ks != null) { int modifiers = ks.getModifiers(); if (modifiers > 0) { String modifierText = InputEvent.getModifiersExText(modifiers); if (Platform.isMacintosh()) { buffer.append(modifierText.replaceAll("\\+", "")); } else { buffer.append(modifierText); String delimiter = UIManager.getString("MenuItem.acceleratorDelimiter"); if (delimiter == null) { delimiter = "+"; } buffer.append(delimiter); } } int keyCode = ks.getKeyCode(); if (keyCode == 0) { buffer.append(ks.getKeyChar()); } else { buffer.append(getKeyText(keyCode)); } } return buffer.toString(); }
Example 11
Source File: GuiUtil.java From FancyBing with GNU General Public License v3.0 | 5 votes |
/** Parse text that has the mnemonic marked with a preceding'&' (like in Qt) and set the text and mnemonic of the button. */ public static void setTextAndMnemonic(AbstractButton button, String text) { int pos = text.indexOf('&'); text = text.replace("&", ""); button.setText(text); if (pos >= 0 && pos < text.length()) { String mnemonic = text.substring(pos, pos + 1).toUpperCase(); KeyStroke keyStroke = KeyStroke.getKeyStroke(mnemonic); int code = keyStroke.getKeyCode(); button.setMnemonic(code); button.setDisplayedMnemonicIndex(pos); } }
Example 12
Source File: BetterAction.java From WorldPainter with GNU General Public License v3.0 | 5 votes |
public final void setShortDescription(String shortDescription) { StringBuilder sb = new StringBuilder(); sb.append(shortDescription); KeyStroke accelerator = (KeyStroke) getValue(ACCELERATOR_KEY); if (accelerator != null) { sb.append(" ("); if ((accelerator.getModifiers() & InputEvent.CTRL_DOWN_MASK) != 0) { sb.append("Ctrl+"); } if ((accelerator.getModifiers() & InputEvent.META_DOWN_MASK) != 0) { sb.append("⌘+"); } if ((accelerator.getModifiers() & InputEvent.ALT_DOWN_MASK) != 0) { sb.append("Alt+"); } if ((accelerator.getModifiers() & InputEvent.ALT_GRAPH_DOWN_MASK) != 0) { sb.append("AltGr+"); } if ((accelerator.getModifiers() & InputEvent.SHIFT_DOWN_MASK) != 0) { sb.append("Shift+"); } int keyCode = accelerator.getKeyCode(); if (keyCode == KeyEvent.VK_ADD) { sb.append('+'); } else if (keyCode == KeyEvent.VK_SUBTRACT) { sb.append('-'); } else { sb.append((char) keyCode); } sb.append(')'); } putValue(SHORT_DESCRIPTION, sb.toString()); }
Example 13
Source File: ReservedKeyBindings.java From ghidra with Apache License 2.0 | 5 votes |
public static boolean isReservedKeystroke(KeyStroke keyStroke) { int code = keyStroke.getKeyCode(); if (code == KeyEvent.VK_SHIFT || code == KeyEvent.VK_ALT || code == KeyEvent.VK_CONTROL || code == KeyEvent.VK_CAPS_LOCK || code == KeyEvent.VK_TAB || HELP_KEY1.equals(keyStroke) || HELP_KEY2.equals(keyStroke) || HELP_INFO_KEY.equals(keyStroke) || UPDATE_KEY_BINDINGS_KEY.equals(keyStroke) || FOCUS_INFO_KEY.equals(keyStroke) || FOCUS_CYCLE_INFO_KEY.equals(keyStroke)) { return true; } return false; }
Example 14
Source File: PCGenAction.java From pcgen with GNU Lesser General Public License v2.1 | 4 votes |
public PCGenAction(String prop, String command, String accelerator, Icons icon, Object... substitutes) { CommonMenuText.name(this, prop, substitutes); if (command != null) { putValue(ACTION_COMMAND_KEY, command); } if (accelerator != null) { // accelerator has three possible forms: // 1) shortcut + // 2) shortcut-alt + // 3) F1 // (error checking is for the weak!) int iShortCut = InputEvent.CTRL_DOWN_MASK; int menuShortcutKeyMask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMaskEx(); StringTokenizer aTok = new StringTokenizer(accelerator); // get the first argument String aString = aTok.nextToken(); if (aString.equalsIgnoreCase("shortcut")) { iShortCut = menuShortcutKeyMask; } else if (aString.equalsIgnoreCase("alt")) { iShortCut = InputEvent.ALT_DOWN_MASK; } else if (aString.equalsIgnoreCase("shift-shortcut")) { iShortCut = menuShortcutKeyMask | InputEvent.SHIFT_DOWN_MASK; } else if (aString.matches("F[0-9]+")) { iShortCut = 0; } if (aTok.hasMoreTokens()) { // get the second argument aString = aTok.nextToken(); } KeyStroke aKey = KeyStroke.getKeyStroke(aString); if (aKey != null) { int iKeyCode = aKey.getKeyCode(); putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(iKeyCode, iShortCut)); } } if (icon != null) { putValue(SMALL_ICON, icon.getImageIcon()); } }
Example 15
Source File: MindMapDocumentEditor.java From netbeans-mmd-plugin with Apache License 2.0 | 4 votes |
@Override public void onNonConsumedKeyEvent(@Nonnull final MindMapPanel source, @Nonnull final KeyEvent e, @Nonnull final KeyEventType type) { if (type == KeyEventType.PRESSED) { if (e.getModifiers() == 0) { switch (e.getKeyCode()) { case KeyEvent.VK_UP: case KeyEvent.VK_LEFT: case KeyEvent.VK_RIGHT: case KeyEvent.VK_DOWN: { e.consume(); } break; } } } boolean activated = false; final ShortcutSet findAtMindMap = getFindAtMindMapShortcutSet(); if (findAtMindMap != null) { final KeyStroke eventStroke = KeyStroke.getKeyStrokeForEvent(e); for (final Shortcut c : findAtMindMap.getShortcuts()) { if (c instanceof KeyboardShortcut) { final KeyboardShortcut keyboardShortcut = (KeyboardShortcut) c; final KeyStroke firstKeyStroke = keyboardShortcut.getFirstKeyStroke(); final KeyStroke secondKeyStroke = keyboardShortcut.getSecondKeyStroke(); if (firstKeyStroke != null && firstKeyStroke.getModifiers() == eventStroke.getModifiers() && firstKeyStroke.getKeyCode() == eventStroke.getKeyCode() && firstKeyStroke.getKeyChar() == eventStroke.getKeyChar()) { activated = true; break; } if (secondKeyStroke != null && secondKeyStroke.getModifiers() == eventStroke.getModifiers() && secondKeyStroke.getKeyCode() == eventStroke.getKeyCode() && secondKeyStroke.getKeyChar() == eventStroke.getKeyChar()) { activated = true; break; } } } } if (activated) { e.consume(); activateTextSearchPanel(); } if (!e.isConsumed() && e.getModifiers() == 0 && e.getKeyCode() == KeyEvent.VK_ESCAPE) { ApplicationManager.getApplication().invokeLater(new Runnable() { @Override public void run() { findTextPanel.deactivate(); } }); } }
Example 16
Source File: NbKeymap.java From netbeans with Apache License 2.0 | 4 votes |
public Action getAction(final KeyStroke key) { switch (key.getKeyCode()) { case KeyEvent.VK_SHIFT: case KeyEvent.VK_CONTROL: case KeyEvent.VK_ALT: case KeyEvent.VK_ALT_GRAPH: case KeyEvent.VK_META: case KeyEvent.VK_UNDEFINED: case KeyEvent.CHAR_UNDEFINED: // Not actually a bindable key press. return null; } if (key.isOnKeyRelease()) { // Again, not really our business here. return null; } LOG.log(Level.FINE, "getAction {0}", key); Map<KeyStroke,Binding> binder = bindings(); for (KeyStroke ctx : context) { Binding sub = binder.get(ctx); if (sub == null) { resetContext(); return BROKEN; // no entry found after known prefix } binder = sub.nested; if (binder == null) { resetContext(); return BROKEN; // anomalous, expected to find submap here } } Binding b = binder.get(key); if (b == null) { resetContext(); return null; // normal, not found } if (b.nested == null) { resetContext(); return b.loadAction(); // found real action } else { return new AbstractAction() { public void actionPerformed(ActionEvent e) { shiftContext(key); // entering submap } }; } }
Example 17
Source File: RecentViewListAction.java From netbeans with Apache License 2.0 | 4 votes |
@Override public void actionPerformed(ActionEvent evt) { boolean editors = true; boolean views = !documentsOnly; if( "immediately".equals( evt.getActionCommand() ) ) { TopComponent activeTc = TopComponent.getRegistry().getActivated(); if( null != activeTc ) { if( TopComponentTracker.getDefault().isEditorTopComponent( activeTc ) ) { //switching in a document, go to some other document views = false; } else { //switching in a view, go to some other view editors = false; views = true; } } } TopComponent[] documents = getRecentWindows(editors, views); if (documents.length < 2) { return; } if(!"immediately".equals(evt.getActionCommand()) && // NOI18N !(evt.getSource() instanceof javax.swing.JMenuItem)) { // #46800: fetch key directly from action command KeyStroke keyStroke = Utilities.stringToKey(evt.getActionCommand()); if(keyStroke != null) { int triggerKey = keyStroke.getKeyCode(); int reverseKey = KeyEvent.VK_SHIFT; int releaseKey = 0; int modifiers = keyStroke.getModifiers(); if((InputEvent.CTRL_MASK & modifiers) != 0) { releaseKey = KeyEvent.VK_CONTROL; } else if((InputEvent.ALT_MASK & modifiers) != 0) { releaseKey = KeyEvent.VK_ALT; } else if((InputEvent.META_MASK & modifiers) != 0) { releaseKey = KeyEvent.VK_META; } if(releaseKey != 0) { if (!KeyboardPopupSwitcher.isShown()) { KeyboardPopupSwitcher.showPopup(documentsOnly, releaseKey, triggerKey, (evt.getModifiers() & KeyEvent.SHIFT_MASK) == 0); } return; } } } int documentIndex = (evt.getModifiers() & KeyEvent.SHIFT_MASK) == 0 ? 1 : documents.length-1; TopComponent tc = documents[documentIndex]; // #37226 Unmaximized the other mode if needed. WindowManagerImpl wm = WindowManagerImpl.getInstance(); ModeImpl mode = (ModeImpl) wm.findMode(tc); if(mode != null && mode != wm.getCurrentMaximizedMode()) { wm.switchMaximizedMode(null); } tc.requestActive(); }
Example 18
Source File: GUI.java From CQL with GNU Affero General Public License v3.0 | 4 votes |
@SuppressWarnings("deprecation") private static Menu makeFileMenu() { Menu fileMenu = new Menu("File"); MenuItem openItem = new MenuItem("Open"); MenuItem saveItem = new MenuItem("Save"); MenuItem saveAsItem = new MenuItem("Save As"); MenuItem saveAllItem = new MenuItem("Save All"); MenuItem closeItem = new MenuItem("Close"); MenuItem exitItem = new MenuItem("Quit"); Map<Language, MenuItem> newItems = new THashMap<>(); for (Language l : Language.values0()) { if (l.toString().contains(" ")) { continue; } if (l.toString().contains("EASIK")) { continue; } MenuItem newItem = new MenuItem("New " + l); fileMenu.add(newItem); newItems.put(l, newItem); newItem.addActionListener((ActionEvent e) -> newAction(null, "", l)); } fileMenu.add(openItem); // fileMenu.add(openItem2); fileMenu.add(saveItem); fileMenu.add(saveAsItem); fileMenu.add(saveAllItem); fileMenu.add(closeItem); fileMenu.add(exitItem); closeItem.addActionListener(e -> closeAction()); saveAsItem.addActionListener(e -> saveAsActionAlternate(getSelectedEditor())); KeyStroke ctrlS = KeyStroke.getKeyStroke(KeyEvent.VK_S, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()); MenuShortcut s = new MenuShortcut(ctrlS.getKeyCode()); saveItem.setShortcut(s); // TODO aql why doesn't shift mask work? order? MenuShortcut sA = new MenuShortcut(ctrlS.getKeyCode(), true); saveAllItem.setShortcut(sA); saveAllItem.addActionListener(e -> saveAllAction()); KeyStroke ctrlQ = KeyStroke.getKeyStroke(KeyEvent.VK_Q, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()); MenuShortcut q = new MenuShortcut(ctrlQ.getKeyCode()); exitItem.setShortcut(q); KeyStroke ctrlW = KeyStroke.getKeyStroke(KeyEvent.VK_W, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()); // KeyStroke commandW = KeyStroke.getKeyStroke(KeyEvent.VK_W, InputEvent.META_DOWN_MASK); MenuShortcut c = new MenuShortcut(ctrlW.getKeyCode()); closeItem.setShortcut(c); openItem.addActionListener(e -> openActionAlternate()); saveItem.addActionListener(e -> saveAction()); exitItem.addActionListener(e -> exitAction()); KeyStroke ctrlN = KeyStroke.getKeyStroke(KeyEvent.VK_N, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()); MenuShortcut n = new MenuShortcut(ctrlN.getKeyCode()); newItems.get(Language.getDefault()).setShortcut(n); KeyStroke ctrlO = KeyStroke.getKeyStroke(KeyEvent.VK_O, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()); MenuShortcut o = new MenuShortcut(ctrlO.getKeyCode()); openItem.setShortcut(o); return fileMenu; }
Example 19
Source File: ThreadsHistoryAction.java From netbeans with Apache License 2.0 | 4 votes |
@Override public void actionPerformed(ActionEvent evt) { List<DVThread> threads = getThreads(); int threadsCount = threads.size(); if (threadsCount < 1) { Toolkit.getDefaultToolkit().beep(); return; } if(!"immediately".equals(evt.getActionCommand()) && // NOI18N !(evt.getSource() instanceof javax.swing.JMenuItem)) { // #46800: fetch key directly from action command KeyStroke keyStroke = Utilities.stringToKey(evt.getActionCommand()); if(keyStroke != null) { int triggerKey = keyStroke.getKeyCode(); int releaseKey = 0; int modifiers = keyStroke.getModifiers(); if((InputEvent.CTRL_MASK & modifiers) != 0) { releaseKey = KeyEvent.VK_CONTROL; } else if((InputEvent.ALT_MASK & modifiers) != 0) { releaseKey = KeyEvent.VK_ALT; } else if((InputEvent.META_MASK & modifiers) != 0) { releaseKey = InputEvent.META_MASK; } if(releaseKey != 0) { if (!KeyboardPopupSwitcher.isShown()) { KeyboardPopupSwitcher.selectItem( createSwitcherItems(threads), releaseKey, triggerKey, true, true); // (evt.getModifiers() & KeyEvent.SHIFT_MASK) == 0 } return; } } } if (threadsCount == 1) { threads.get(0).makeCurrent(); } else { int index = (evt.getModifiers() & KeyEvent.SHIFT_MASK) == 0 ? 1 : threadsCount - 1; threads.get(index).makeCurrent(); } }
Example 20
Source File: AbstractDockingTest.java From ghidra with Apache License 2.0 | 3 votes |
/** * Fires a {@link KeyListener#keyPressed(KeyEvent)}, * {@link KeyListener#keyTyped(KeyEvent)} * and {@link KeyListener#keyReleased(KeyEvent)} for the given key stroke * * @param c the destination component * @param ks the key stroke */ public static void triggerKey(Component c, KeyStroke ks) { int modifiers = ks.getModifiers(); char keyChar = ks.getKeyChar(); int keyCode = ks.getKeyCode(); triggerKey(c, modifiers, keyCode, keyChar); }