Multiple key press on JavaFX scene


Question

How are multiple key events detected in a single scene? I need my program to detect when the space bar and the right arrow keys are pressed simultaneously.

scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
    public void handle(KeyEvent ke) {
        if (ke.getCode() == KeyCode.RIGHT) {
            ///
        }

        if (ke.getCode() == KeyCode.LEFT) {
            ///
        }

        if (ke.getCode() == KeyCode.SPACE) {
            ///
        }

        if (ke.getCode() == KeyCode.RIGHT && ke.getCode() == KeyCode.SPACE) {
            // How??
        }
    }
});

The first 3 expressions look for a single key and work fine. The final expression never returns true. I believe only the latest key event is passed to the handler.

I looked at KeyCodeCombination, however this appears to be for use in cases when a key has a modifier key from a specified list (ALT_DOWN, SHIFT_DOWN, etc).

Is there a utility in FX that I can use?

1
3
11/18/2017 1:47:49 PM

Accepted Answer

Try this:

final BooleanProperty spacePressed = new SimpleBooleanProperty(false);
final BooleanProperty rightPressed = new SimpleBooleanProperty(false);
final BooleanBinding spaceAndRightPressed = spacePressed.and(rightPressed);

// How to respond to both keys pressed together:
spaceAndRightPressed.addListener(new ChangeListener<Boolean>() {
    @Override
    public void changed(ObservableValue<Boolean> obs, Boolean werePressed, Boolean arePressed) {
        System.out.println("Space and right pressed together");
    }
});

// Wire up properties to key events:
scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
    @Override
    public void handle(KeyEvent ke) {
        if (ke.getCode() == KeyCode.SPACE) {
            spacePressed.set(true);
        } else if (ke.getCode() == KeyCode.RIGHT) {
            rightPressed.set(true);
        }
    }
});

scene.setOnKeyReleased(new EventHandler<KeyEvent>() {
    @Override
    public void handle(KeyEvent ke) {
        if (ke.getCode() == KeyCode.SPACE) {
            spacePressed.set(false);
        } else if (ke.getCode() == KeyCode.RIGHT) {
            rightPressed.set(false);
        }
    }
});
11
4/14/2014 11:11:08 AM

Close the application using hot keys.
mac: command + w / esc
windows: alt + F4 / esc

    public class JavaApp {/*inner: public class YourApplication extends Application*/
        public void exit() {
            stage.close();
            System.exit(0);
        }
    }

        KeyCombination keyCombinationMac = new KeyCodeCombination(KeyCode.W, KeyCombination.SHORTCUT_DOWN);
        KeyCombination keyCombinationWin = new KeyCodeCombination(KeyCode.F4, KeyCombination.ALT_DOWN);
        scene.addEventHandler(KeyEvent.KEY_RELEASED, event -> {
            if (keyCombinationMac.match(event)
                    || keyCombinationWin.match(event)
                    || event.getCode() == KeyCode.ESCAPE
                    ) {
                new JavaApp().exit();
            }
        });

from: https://dzone.com/articles/handling-keyboard-sortcuts

final KeyCombination keyComb1 = new KeyCodeCombination(KeyCode.R, KeyCombination.CONTROL_DOWN);
scene.addEventHandler(KeyEvent.KEY_RELEASED, new EventHandler() {
                @Override
                public void handle(KeyEvent event) {
                    if (keyComb1.match(event)) {
                        System.out.println("Ctrl+R pressed");
                    }
                }
            });


scene.setOnKeyPressed(new EventHandler<KeyEvent>() {
 public void handle(final KeyEvent keyEvent) {
   if (keyEvent.getCode() == KeyCode.F5) {
    System.out.println("F5 pressed");
    //Stop letting it do anything else
    keyEvent.consume();
   }
 }
});

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