tornadofx響應鍵盤點擊事件

import javafx.application.Application
import javafx.beans.property.SimpleStringProperty
import javafx.event.EventHandler
import javafx.geometry.Pos
import javafx.scene.input.KeyEvent
import javafx.scene.paint.Color
import javafx.scene.text.FontWeight
import tornadofx.*

val keyPressedName = SimpleStringProperty("a")
fun main() = Application.launch(TestApp::class.java)
class TestApp : App(TestView::class)
class TestView : View("Learn") {

    override val root = vbox(10) {
        prefHeight = 400.0
        prefWidth = 400.0
        alignment = Pos.CENTER
        label(keyPressedName) {
            style {
                fontSize = 50.px
                fontWeight = FontWeight.EXTRA_BOLD
                textFill = Color.RED
            }
        }
        // 必需要有一個button纔會響應鍵盤點擊事件,不清楚是什麼緣由
        button("test")
        this.onKeyPressed = KeyEventHandler1()
        this.onKeyReleased = KeyEventHandler1()
    }
}

class KeyEventHandler1 : EventHandler<KeyEvent> {
    override fun handle(event: KeyEvent) {
        keyPressedName.value = event.code.getName()
    }
}
相關文章
相關標籤/搜索