JavaFX key listener for multiple keys pressed implementation?


I would like to create an event handler that listens for multiple key combinations such as holding Ctrl and C at the same time.

Why doesn't something like if((... == Control) && (... == C)) work?

Here is the code I trying to work with:

textField.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
    public void handle(KeyEvent event) {
        if ((event.getCode() == KeyCode.CONTROL) && (event.getCode() == KeyCode.C)) {
            System.out.println("Control pressed");
11/18/2017 7:18:40 AM

Accepted Answer

One way to tackle this problem is to create a KeyCombination object and set some of its properties to what you see below.

Try the following:

textfield.getScene().getAccelerators().put(new KeyCodeCombination(
    KeyCode.C, KeyCombination.CONTROL_ANY), new Runnable() {
    @Override public void run() {
        //Insert conditions here
11/18/2017 7:31:48 AM

You can try this solution, it worked for me!

final KeyCombination keyCombinationShiftC = new KeyCodeCombination(
KeyCode.C, KeyCombination.CONTROL_DOWN);

textField.setOnKeyPressed(new EventHandler<KeyEvent>() {
    public void handle(KeyEvent event) {
        if (keyCombinationShiftC.match(event)) {
  "CTRL + C Pressed");

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