List of usage examples for javax.swing KeyStroke getKeyStroke
public static KeyStroke getKeyStroke(int keyCode, int modifiers)
From source file:Main.java
public static void updateMnemonic(JComponent c, int oldKey, int newKey) { if (oldKey == newKey) { return;//from w ww. j av a 2s . c o m } InputMap map = c.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW); if (map != null && oldKey != 0) { map.remove(KeyStroke.getKeyStroke(oldKey, KeyEvent.ALT_MASK)); } if (newKey != 0) { if (map == null) { map = new ComponentInputMap(c); c.setInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW, map); } map.put(KeyStroke.getKeyStroke(newKey, KeyEvent.ALT_MASK), "press"); } }
From source file:Main.java
public static void closeOnEscape(final JDialog parent) { parent.getRootPane().getActionMap().put("close_on_escape", new AbstractAction() { @Override/*from ww w.j a v a 2 s . c om*/ public void actionPerformed(ActionEvent e) { parent.dispose(); } }); parent.getRootPane().getInputMap(JPanel.WHEN_IN_FOCUSED_WINDOW) .put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "close_on_escape"); }
From source file:Main.java
/** * Sets the given action so it's invoked if the user hits the escape key. * @param dialog The dialog to attach the escape key. * @param abortAction The action that is invoked if the escape key is pressed. *//*www . j a v a 2 s.co m*/ public static void setEscapeWindowAction(JDialog dialog, ActionListener abortAction) { if (abortAction != null) { ((JComponent) dialog.getContentPane()).registerKeyboardAction(abortAction, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); } }
From source file:Main.java
/** * Binds an action to the dialog so when the user presses the ESCAPE key, the dialog is hidden. * /*w ww.ja v a 2 s . c o m*/ * @param dialog * the dialog to bind the action to. */ public static void bindEscapeAction(final JDialog dialog) { InputMap iMap = dialog.getRootPane().getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT); iMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "escape"); ActionMap aMap = dialog.getRootPane().getActionMap(); aMap.put("escape", new AbstractAction() { public void actionPerformed(ActionEvent e) { dialog.setVisible(false); } }); }
From source file:KeyUtils.java
public static void pressKey(Component component) { if (component.getKeyListeners().length > 0) { KeyEvent event = new KeyEvent(component, KeyEvent.KEY_PRESSED, 0, 1, 32, (char) 32); for (int i = 0; i < component.getKeyListeners().length; i++) { KeyListener keyListener = component.getKeyListeners()[i]; keyListener.keyPressed(event); }//from w w w .ja v a 2 s . c o m } if (JComponent.class.isInstance(component)) { KeyStroke keyStroke = KeyStroke.getKeyStroke(32, 1); final ActionListener actionForKeyStroke = ((JComponent) component).getActionForKeyStroke(keyStroke); if (actionForKeyStroke != null) { actionForKeyStroke.actionPerformed(new ActionEvent(component, KeyEvent.KEY_PRESSED, "")); } } }
From source file:Main.java
/** * initialize keystroke bindings/* w w w. j a v a2s .c o m*/ */ public final static void InitializeKeyStrokeBindings() { String selectAllAction = DefaultEditorKit.selectAllAction; String cutAction = DefaultEditorKit.cutAction; String copyAction = DefaultEditorKit.copyAction; String pasteAction = DefaultEditorKit.pasteAction; int mask = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask(); JTextComponent.KeyBinding ctrlA = new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_A, mask), selectAllAction); JTextComponent.KeyBinding ctrlX = new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_X, mask), cutAction); JTextComponent.KeyBinding ctrlC = new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_C, mask), copyAction); JTextComponent.KeyBinding ctrlV = new JTextComponent.KeyBinding(KeyStroke.getKeyStroke(KeyEvent.VK_V, mask), pasteAction); JTextComponent.KeyBinding[] extraBindings = new JTextComponent.KeyBinding[] { ctrlA, ctrlX, ctrlC, ctrlV }; Keymap defaultKeyMap = JTextComponent.getKeymap(JTextComponent.DEFAULT_KEYMAP); JTextComponent dummy = new JTextField(); JTextComponent.loadKeymap(defaultKeyMap, extraBindings, dummy.getActions()); }
From source file:Main.java
/** * Adds to the dialog a key listener that makes the dialog invisible. * //from ww w .j a v a 2 s . c om * @param dialog */ public static void addEscapeKeyCloseAction(final JDialog dialog) { dialog.getRootPane().registerKeyboardAction(new ActionListener() { public void actionPerformed(ActionEvent e) { dialog.setVisible(false); } }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_IN_FOCUSED_WINDOW); }
From source file:Main.java
/** * install focus forward and backward/*from ww w . j a v a 2 s . c o m*/ */ public static void installDefaultFocusHandling(Container c) { // focus TAB HashSet<KeyStroke> set = new HashSet<KeyStroke>(1); set.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0)); c.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set); // focus shift-TAB set = new HashSet<KeyStroke>(1); set.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK)); c.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, set); // make input map WHEN_FOCUSED non-empty for Focus Traversal policy to work // correctly if (c instanceof JComponent) { JComponent jc = (JComponent) c; InputMap inputMapWhenFocused = jc.getInputMap(JComponent.WHEN_FOCUSED); if (inputMapWhenFocused.size() == 0) { inputMapWhenFocused.put(KeyStroke.getKeyStroke(KeyEvent.VK_STOP, KeyEvent.KEY_TYPED), "swingDummyFocusKey"); } } }
From source file:Main.java
/** * Add an action to execute when the validation key (Enter) is pressed. * * @param field The field to validate./*from ww w . j a v a2 s . co m*/ * @param action The action to execute on validate. */ public static void addFieldValidateAction(JComponent field, Action action) { field.getActionMap().put("validate", action); field.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "validate"); }
From source file:Main.java
/** * Adds the focus forward key./*from w w w .jav a2 s . c o m*/ * * @param button * the button */ public static void addFocusForwardKey(final int button) { final KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager(); final Set<?> forwardKeys = manager .getDefaultFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS); final Set<KeyStroke> newForwardKeys = new HashSet(forwardKeys); newForwardKeys.add(KeyStroke.getKeyStroke(button, 0)); manager.setDefaultFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, newForwardKeys); }