KeyBindings in JavaFX 2

How to use KeyBindings in JFX 2? I need to reassign the Enter key from carrige, returning to my own function, and to return carrige, assign CTRL + ENTER

I tried this way, but still it creates a new line.

messageArea.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
        @Override
        public void handle(KeyEvent keyEvent) {
            if (keyEvent.getCode() == KeyCode.ENTER) {
                sendMessage();
            }
        }
    });
+3
source share
2 answers

If you want to prevent the default behavior of the event you are filtering, you need consume it.

There are many kinds of KeyEvents, you can filter on KeyEvent.ANY instead of just KeyEvent.KEY_PRESSEDconsuming them all.

+6
source

As a complement to the jewelry answer. To control keyboard shortcuts, use:

if (event.getCode().equals(KeyCode.ENTER) && event.isControlDown()) { // CTRL + ENTER
    messageArea.setText(messageArea.getText() + "\n");
}

in your handler.

+9

All Articles