I'm trying to track global key events, in particular arrow keys being pressed. For this, I'm attaching a handler to the currently active scene:
scene.setOnKeyPressed(event ->
System.out.println("KEY PRESSED: " + event.getCode())
)
However, while most keys like letters, ctrl and tab are always detected, space and arrow keys only work while no element has focus. That is, as soo as I click anywhere in the window, some element gains focus and pressing space or arrows doesn't fire the event anymore. Through CSS styling I found out that once the scene has been clicked, there is always a focused element present, and above keys stop working in this case.
My guess is that focused nodes listen to key events and refuse to bubble them up if it's space or an arrow. My questions are:
- Why are those keys captured? I don't see a necessity for arbitrary UI components to always handle arrow and space key events.
- Can I prevent nodes from stealing key events, i.e. make them always bubble those? If not, is there another approach to detect key presses?