Example usage for javax.swing KeyStroke getKeyStrokeForEvent

List of usage examples for javax.swing KeyStroke getKeyStrokeForEvent

Introduction

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

Prototype

public static KeyStroke getKeyStrokeForEvent(KeyEvent anEvent) 

Source Link

Document

Returns a KeyStroke which represents the stroke which generated a given KeyEvent.

Usage

From source file:org.nekorp.workflow.desktop.view.AppMainWindow.java

/**
 * call this somewhere in your GUI construction
 *//*w  ww .  ja va  2  s.c  o  m*/
private void setupKeyShortcut() {
    KeyStroke key1 = KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_DOWN_MASK);
    actionMap.put(key1, new AbstractAction("guardar") {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (editorMonitor.hasChange()) {
                try {
                    aplication.guardaServicio();
                } catch (IllegalArgumentException ex) {
                    //no lo guardo.
                }
            }
        }
    });
    //        key1 = KeyStroke.getKeyStroke(KeyEvent.VK_Z, KeyEvent.CTRL_DOWN_MASK);
    //        actionMap.put(key1, new AbstractAction("deshacer") {
    //            @Override
    //            public void actionPerformed(ActionEvent e) {
    //                if (editorMonitor.hasChange()) {
    //                    editorMonitor.undo();
    //                }
    //            }
    //        });
    //        key1 = KeyStroke.getKeyStroke(KeyEvent.VK_Y, KeyEvent.CTRL_DOWN_MASK);
    //        actionMap.put(key1, new AbstractAction("rehacer") {
    //            @Override
    //            public void actionPerformed(ActionEvent e) {
    //                editorMonitor.redo();
    //            }
    //        });
    // add more actions..

    KeyboardFocusManager kfm = KeyboardFocusManager.getCurrentKeyboardFocusManager();
    kfm.addKeyEventDispatcher(new KeyEventDispatcher() {
        @Override
        public boolean dispatchKeyEvent(KeyEvent e) {
            KeyStroke keyStroke = KeyStroke.getKeyStrokeForEvent(e);
            if (actionMap.containsKey(keyStroke)) {
                final Action a = actionMap.get(keyStroke);
                final ActionEvent ae = new ActionEvent(e.getSource(), e.getID(), null);
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        a.actionPerformed(ae);
                    }
                });
                return true;
            }
            return false;
        }
    });
}