KeyBindings in JavaFX 2

event-handling, javafx

Solution

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

There are numerous kinds of KeyEvents, you may want to filter on KeyEvent.ANY instead of just `KeyEvent.KEY_PRESSED` and consume them all.

Problem

How to use KeyBindings in JFX 2? I need to reassign Enter key from carrige returning to my own function, and for carrige returning assign CTRL+ENTER I've tried this way, but still it makes a new line. ``` messageArea.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() { @Override public void handle(KeyEvent keyEvent) { if (keyEvent.getCode() == KeyCode.ENTER) { sendMessage(); } } }); ```

Original source