KeyBindings in JavaFX 2


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>() {
        public void handle(KeyEvent keyEvent) {
            if (keyEvent.getCode() == KeyCode.ENTER) {
5/3/2012 10:20:37 PM

Accepted Answer

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.

5/4/2012 12:46:26 AM

As an addition to jewelsea's answer. To control key combinations use:

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

in your handler.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow