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()
}
}
tornadofx响应键盘点击事件
点赞
收藏