Reputation: 21547
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();
}
}
});
Upvotes: 3
Views: 6284
Reputation: 49185
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.
Upvotes: 9
Reputation: 159281
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.
Upvotes: 6