Search code examples
kotlintornadofx

TornadoFX key press listener issues


When I run the following code

fun main(args: Array<String>) {
    Application.launch(HelloWorldApp::class.java, *args)
}

class HelloWorldApp : App(HelloWorld::class)

class HelloWorld : View() {
    override val root = hbox {
        addEventFilter(KeyEvent.ANY) { event ->
            println("pressed:"+event.character)
        }
    }
}

When I press any keys on my keyboard the println() is never called. Am I missing something?


Solution

  • Looking for a similar problem I came up with this, which looks simpler, but I do not yet understand any possible subtle differences between using the keyboard control versus explicit focus requesting.

    import javafx.scene.input.KeyEvent
    import tornadofx.*
    
    fun main(args: Array<String>) {
        launch<HelloWorldApp>(args)
    }
    
    class HelloWorldApp : App(HelloWorld::class)
    
    class HelloWorld : View() {
        override val root = hbox {
            keyboard {
                addEventHandler(KeyEvent.KEY_PRESSED) { println(it.code) }
            }
        }
    }