I want to detect a KeyEvent
regardless of what element has focus in my JavaFX app - in particular, I want to detect the SPACE
key. I tried to add a listener to the Scene
that corresponds to my window:
scene.setOnKeyPressed(ev -> {
if (ev.getCode() == KeyCode.SPACE) {
System.out.println("GOOD");
}
});
But if I have a particular node with focus (like a ListView
or Button
) then it won't be detected.
How can I detect when the SPACE
key is pressed regardless of whatever the user is doing in my app? I don't intend to interrupt whatever node is receiving the KeyEvent
- I just want to know if it happens. One (ugly) solution would be adding the listener to all my nodes, but I would rather not if possible.