Detect keyboard events

    @Override
    public void onStart(IContext context) throws JFException {
        this.console = context.getConsole();
        this.engine = context.getEngine();
        this.history = context.getHistory();

        final IContext finalContext = context;

        KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher() {
            public boolean dispatchKeyEvent(final KeyEvent e) {
                boolean discardEvent = false;

                int closeMask = KeyEvent.CTRL_DOWN_MASK + 82; //Ctrl+r

                if (((e.getModifiersEx() + e.getKeyCode()) == closeMask) 
                                        && e.paramString().indexOf("KEY_PRESSED") >= 0) {
                    console.getOut().println("ctrl-r pressed");
                    finalContext.executeTask(new OnKeyTask());
                    discardEvent = true;

                }
                return discardEvent;
            }
        });
    }

    private class OnKeyTask implements Callable<Object> {

        public OnKeyTask() {
        }

        public Object call() throws Exception {
            console.getOut().println("task");
            return null;
        }

    }
    }

Keyboard.java

The information on this web site is provided only as general information, which may be incomplete or outdated. Click here for full disclaimer.