Example usage for javax.swing KeyStroke getKeyStroke

List of usage examples for javax.swing KeyStroke getKeyStroke

Introduction

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

Prototype

public static KeyStroke getKeyStroke(String s) 

Source Link

Document

Parses a string and returns a KeyStroke.

Usage

From source file:Main.java

public static void addEscAction(JComponent comp, Action action) {
    comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ESCAPE"), action);
    comp.getActionMap().put(action, action);
}

From source file:Main.java

public static void addEnterAction(JComponent comp, Action action) {
    comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke("ENTER"), action);
    comp.getActionMap().put(action, action);
}

From source file:Main.java

public static void bindUndoManager(final JTextComponent text, final UndoManager undo) {

    text.getDocument().addUndoableEditListener(new UndoableEditListener() {
        @Override/*from  ww  w . j  a va2s  .co m*/
        public void undoableEditHappened(UndoableEditEvent e) {
            undo.addEdit(e.getEdit());
        }
    });

    text.getActionMap().put("Undo", new AbstractAction("Undo") {
        private static final long serialVersionUID = 1L;

        public void actionPerformed(ActionEvent evt) {
            try {
                if (undo.canUndo()) {
                    undo.undo();
                }
            } catch (CannotUndoException e) {
            }
        }
    });
    // Bind the undo action to ctl-Z
    text.getInputMap().put(KeyStroke.getKeyStroke("control Z"), "Undo");

    // Create a redo action and add it to the text component
    text.getActionMap().put("Redo", new AbstractAction("Redo") {
        private static final long serialVersionUID = 1L;

        public void actionPerformed(ActionEvent evt) {
            try {
                if (undo.canRedo()) {
                    undo.redo();
                }
            } catch (CannotRedoException e) {
            }
        }
    });
    // Bind the redo action to ctl-Y 
    text.getInputMap().put(KeyStroke.getKeyStroke("control Y"), "Redo");
}

From source file:EscapeDialog.java

License:asdf

protected JRootPane createRootPane() {
    JRootPane rootPane = new JRootPane();
    KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE");
    Action actionListener = new AbstractAction() {
        public void actionPerformed(ActionEvent actionEvent) {
            System.out.println("about to disappear");
            setVisible(false);/*from w ww .ja  v a  2  s  .c  om*/
        }
    };
    InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(stroke, "ESCAPE");
    rootPane.getActionMap().put("ESCAPE", actionListener);

    return rootPane;
}

From source file:Main.java

public ShowAction() {
    super("About");

    putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke("A"));
    putValue(Action.NAME, "Go to number ");

}

From source file:Main.java

private void addKeyBindings(JComponent c) {
    c.getInputMap(JComboBox.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("ENTER"),
            "doSomething");
    c.getActionMap().put("doSomething", new AbstractAction() {

        @Override// www  .j  a  v  a 2 s.co  m
        public void actionPerformed(ActionEvent e) {
            Object selectedItem = comboBox.getSelectedItem();
            if (selectedItem != null) {
                model.addElement((String) selectedItem);
            }
        }
    });
}

From source file:Main.java

/**
 * Sets the hot key for focus./*  w  ww  .  j  a  v  a2  s .c  o  m*/
 *
 * @param comp
 *            the comp
 * @param keyStroke
 *            the key stroke
 * @param actionName
 *            the action name
 */
public static void setHotKeyForFocus(final JComponent comp, final String keyStroke, final String actionName) {
    // get the button's Action map
    final ActionMap amap = comp.getActionMap();
    // add an action to the button's action map
    // and give it a name(it can be any object not just String)
    amap.put(actionName, new AbstractAction() {
        /**
         *
         */
        private static final long serialVersionUID = 1L;

        @Override
        public void actionPerformed(final ActionEvent e) {
            // call your a method that contains your action code
            comp.requestFocus();
        }
    });
    // get the input map for the button
    final InputMap imap = comp.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    // add a key stroke associated with an action in the action map(action
    // name).
    // imap.put(KeyStroke.getKeyStroke("F1"),"ActionName");
    // you can do the same for more than one key.
    imap.put(KeyStroke.getKeyStroke(keyStroke), actionName);
}

From source file:Main.java

private void addKeyBind(JComponent contentPane, String key) {
    InputMap iMap = contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    ActionMap aMap = contentPane.getActionMap();
    iMap.put(KeyStroke.getKeyStroke(key), DISABLE_CLICKER);
    aMap.put(DISABLE_CLICKER, disableButtonAction);
}

From source file:com.tulskiy.musique.plugins.hotkeys.HotkeyConfiguration.java

public static Map<KeyStroke, HotKeyEvent> getHotkeys(Logger logger) {
    Configuration config = Application.getInstance().getConfiguration();
    List<String> hotkeysRaw = (List<String>) config.getList(getHotkeyKey());
    Map<KeyStroke, HotKeyEvent> hotkeys = new LinkedHashMap<KeyStroke, HotKeyEvent>();
    if (!CollectionUtils.isEmpty(hotkeysRaw)) {
        for (String hotkeyRaw : hotkeysRaw) {
            try {
                String[] tokens = hotkeyRaw.split(": ");

                HotKeyEvent event = HotKeyEvent.valueOf(tokens[0]);
                KeyStroke keyStroke = KeyStroke.getKeyStroke(tokens[1]);

                hotkeys.put(keyStroke, event);
            } catch (IllegalArgumentException e) {
                logger.warning("Could not parse hotkey for string: " + hotkeyRaw);
            }//from ww  w .j a v  a  2 s .  c o m
        }
    }

    return hotkeys;
}

From source file:daylightchart.gui.actions.BaseBrowserAction.java

BaseBrowserAction(final String text, final String iconResource, final String shortcutKey, final String url) {
    super(text, iconResource);
    if (StringUtils.isNotBlank(shortcutKey)) {
        setShortcutKey(KeyStroke.getKeyStroke(shortcutKey));
    }/*w  w w.j a  v a2s . com*/
    addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(final ActionEvent actionevent) {
            BareBonesBrowserLaunch.openURL(url);
        }
    });
}