
1import javafx.application.Application 2import javafx.beans.property.SimpleStringProperty 3import javafx.event.EventHandler 4import javafx.geometry.Pos 5import javafx.scene.input.KeyEvent 6import javafx.scene.paint.Color 7import javafx.scene.text.FontWeight 8import tornadofx.* 9 10val keyPressedName = SimpleStringProperty("a") 11fun main() = Application.launch(TestApp::class.java) 12class TestApp : App(TestView::class) 13class TestView : View("Learn") { 14 15 override val root = vbox(10) { 16 prefHeight = 400.0 17 prefWidth = 400.0 18 alignment = Pos.CENTER 19 label(keyPressedName) { 20 style { 21 fontSize = 50.px 22 fontWeight = FontWeight.EXTRA_BOLD 23 textFill = Color.RED 24 } 25 } 26 // 必须要有一个button才会响应键盘点击事件,不清楚是什么原因 27 button("test") 28 this.onKeyPressed = KeyEventHandler1() 29 this.onKeyReleased = KeyEventHandler1() 30 } 31} 32 33class KeyEventHandler1 : EventHandler<KeyEvent> { 34 override fun handle(event: KeyEvent) { 35 keyPressedName.value = event.code.getName() 36 } 37}