Java Code Examples for javax.swing.KeyStroke#getModifiers()
The following examples show how to use
javax.swing.KeyStroke#getModifiers() .
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: PathEditor.java From coordination_oru with GNU General Public License v3.0 | 6 votes |
private String getHelp() { String ret = ""; TreeMap<String,String> helpText = new TreeMap<String, String>(); for (KeyStroke key : panel.getInputMap().allKeys()) { if (key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_0,0)) || key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_1,0)) || key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_2,0)) || key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_3,0)) || key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_4,0)) || key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_5,0)) || key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_6,0)) || key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_7,0)) || key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_8,0)) || key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_9,0)) || key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_MINUS,0)) || key.equals(KeyStroke.getKeyStroke(KeyEvent.VK_COMMA,0))) continue; if (key.getModifiers() != 0) helpText.put(key.toString().replaceAll("pressed ", "").replaceAll(" ", "-"), panel.getInputMap().get(key).toString()); else helpText.put(key.toString().replaceAll("pressed ", ""), panel.getInputMap().get(key).toString()); } for (Entry<String,String> en : helpText.entrySet()) { ret += en.getKey() + ": " + en.getValue() + "\n"; } return ret; }
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: DeviceKBTest.java From marathonv5 with Apache License 2.0 | 6 votes |
private JavaAgentKeys getOSKey() { KeyStroke selectall = null; InputMap inputMap = new JTextField().getInputMap(); KeyStroke[] allKeys = inputMap.allKeys(); for (KeyStroke keyStroke : allKeys) { Object object = inputMap.get(keyStroke); if (object.equals("select-all")) { selectall = keyStroke; break; } } if ((selectall.getModifiers() & InputEvent.CTRL_DOWN_MASK) == InputEvent.CTRL_DOWN_MASK) { return JavaAgentKeys.CONTROL; } if ((selectall.getModifiers() & InputEvent.META_DOWN_MASK) == InputEvent.META_DOWN_MASK) { return JavaAgentKeys.META; } throw new RuntimeException("Which key?"); }
Example 5
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 6
Source File: Marathon.java From marathonv5 with Apache License 2.0 | 5 votes |
public String getCharSequence(String keys) { StringBuilder sb = new StringBuilder(); if (keys.length() == 1) { sb.append(keys); } else { KeyStrokeParser ksp = new KeyStrokeParser(keys); KeyStroke ks = ksp.getKeyStroke(); CharSequence keys2 = keyMapping.get(ks.getKeyCode()); if (keys2 == null) { String keysText = KeyEvent.getKeyText(ks.getKeyCode()); if (keysText.length() == 1 && Character.isUpperCase(keysText.charAt(0))) { keysText = keysText.toLowerCase(); } keys2 = keysText; } int modifiers = ks.getModifiers(); if ((modifiers & InputEvent.SHIFT_DOWN_MASK) != 0) { sb.append(Keys.SHIFT); } if ((modifiers & InputEvent.CTRL_DOWN_MASK) != 0) { sb.append(Keys.CONTROL); } if ((modifiers & InputEvent.META_DOWN_MASK) != 0) { sb.append(Keys.META); } if ((modifiers & InputEvent.ALT_DOWN_MASK) != 0) { sb.append(Keys.ALT); } sb.append(keys2); } return sb.toString(); }
Example 7
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 8
Source File: LGuiUtils.java From scelight with Apache License 2.0 | 5 votes |
/** * Returns a nice string representation of the specified key stroke. * <p> * For example returns <code>" (Ctrl+P)"</code> for the keystroke of CTRL+P. * </p> * * @param keyStroke key stroke whose nice string representation to return * @return a nice string representation of the specified key stroke */ public static String keyStrokeToString( final KeyStroke keyStroke ) { final StringBuilder sb = new StringBuilder( " (" ); if ( keyStroke.getModifiers() != 0 ) sb.append( KeyEvent.getKeyModifiersText( keyStroke.getModifiers() ) ).append( '+' ); sb.append( KeyEvent.getKeyText( keyStroke.getKeyCode() ) ).append( ')' ); return sb.toString(); }
Example 9
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 10
Source File: ActionsSupport.java From visualvm with GNU General Public License v2.0 | 5 votes |
public static String keyAcceleratorString(KeyStroke keyStroke) { if (keyStroke == null || NO_KEYSTROKE.equals(keyStroke)) return null; String keyText = KeyEvent.getKeyText(keyStroke.getKeyCode()); int modifiers = keyStroke.getModifiers(); if (modifiers == 0) return keyText; if (ACC_DELIMITER == null) { ACC_DELIMITER = UIManager.getString("MenuItem.acceleratorDelimiter"); // NOI18N if (ACC_DELIMITER == null) ACC_DELIMITER = "+"; // NOI18N // Note: NetBeans default, Swing uses '-' by default } return KeyEvent.getKeyModifiersText(modifiers) + ACC_DELIMITER + keyText; }
Example 11
Source File: ToggleProfilingPointAction.java From netbeans with Apache License 2.0 | 5 votes |
public void eventDispatched(AWTEvent event) { if (!(event instanceof KeyEvent)) return; KeyStroke eventKeyStroke = KeyStroke.getKeyStrokeForEvent((KeyEvent)event); if (acceleratorKeyStroke == null || eventKeyStroke == null) return; int acceleratorModifiers = acceleratorKeyStroke.getModifiers(); if (acceleratorModifiers == 0) return; if (acceleratorModifiers != eventKeyStroke.getModifiers()) modifierKeyStateChanged(); }
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: FreeColActionUI.java From freecol with GNU General Public License v2.0 | 5 votes |
/** * Removes the given {@code KeyStroke}. That is: * This action's {@code KeyStroke} is set to * {@code null} if it is the same as the given * {@code KeyStroke}. * * @param k The {@code KeyStroke} to be removed. */ public void removeKeyStroke(KeyStroke k) { if (k != null && keyStroke != null && k.getKeyCode() == keyStroke.getKeyCode() && k.getModifiers() == keyStroke.getModifiers()) { keyStroke = null; bl.setText(" "); } }
Example 14
Source File: FlatMenuItemRenderer.java From FlatLaf with Apache License 2.0 | 5 votes |
protected String getTextForAccelerator( KeyStroke accelerator ) { StringBuilder buf = new StringBuilder(); int modifiers = accelerator.getModifiers(); if( modifiers != 0 ) buf.append( InputEvent.getModifiersExText( modifiers ) ).append( acceleratorDelimiter ); int keyCode = accelerator.getKeyCode(); if( keyCode != 0 ) buf.append( KeyEvent.getKeyText( keyCode ) ); else buf.append( accelerator.getKeyChar() ); return buf.toString(); }
Example 15
Source File: StringUtils.java From xyTalk-pc with GNU Affero General Public License v3.0 | 4 votes |
public static String keyStroke2String(KeyStroke key) { StringBuilder s = new StringBuilder(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: s.append("typed "); s.append(key.getKeyChar()).append(" "); break; case KeyEvent.KEY_PRESSED: s.append("pressed "); s.append(getKeyText(key.getKeyCode())).append(" "); break; case KeyEvent.KEY_RELEASED: s.append("released "); s.append(getKeyText(key.getKeyCode())).append(" "); break; default: s.append("unknown-event-type "); break; } return s.toString(); }
Example 16
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 17
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 18
Source File: StringUtils.java From Spark with Apache License 2.0 | 4 votes |
public static String keyStroke2String(KeyStroke key) { StringBuilder s = new StringBuilder(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: s.append("typed "); s.append(key.getKeyChar()).append(" "); break; case KeyEvent.KEY_PRESSED: s.append("pressed "); s.append(getKeyText(key.getKeyCode())).append(" "); break; case KeyEvent.KEY_RELEASED: s.append("released "); s.append(getKeyText(key.getKeyCode())).append(" "); break; default: s.append("unknown-event-type "); break; } return s.toString(); }
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); }