ScalaFX: Not catching cursor keypresses when button is added


#1

In my scalafx.scene, i catch key events this way:

scene = new Scene(300, 300) {
    val btn_startstop = new Button("Start")
    onKeyPressed = (ev:KeyEvent) => {
        if( ev.code == KeyCode.LEFT ) {
        }
    }
    content = List(btn_startstop)
}

This works with “normal” keys like the a-z keys, but not with the cursor keys. These seem to be catched away by the button. When I remove the button, cursor key presses are then also catched by the scene.

How can i prevent the button to capture any key presses? It shall react only to mouse clicks.


#2

You may want to also ask about this in a JavaFX community – IIRC, ScalaFX is a fairly thin shell around JavaFX, so this is probably an issue in the underlying library.


#3

You can control who is handling which events using event filters. There is more on that in JavaFX documentation:
https://docs.oracle.com/javase/8/javafx/events-tutorial/filters.htm#BCFDCCEJ

A ScalaFX example of doing that is here:

A good way to ask questions about ScalaFX is to post them on StackOverflow using ScalaFX tag:


A JavaFX tag can be added, as needed, to reach that community too.