gpt4 book ai didi

scala - 如何在 Scala react 中使用关键事件

转载 作者:行者123 更新时间:2023-12-04 18:15:39 25 4
gpt4 key购买 nike

假设有一个“ react ” block ,例如

this.reactions += {
case KeyReleased(src, key, mod, _) => {
// do some stuff
// but how to consume this KeyEvent??
}
}

在点“一些东西”之后,我想使用 KeyEvent,但我不知道如何。查看 Component.scala 上的源代码,我发现 KeyReleased 事件是如何构造的:
object keys extends Publisher {
peer.addKeyListener(new KeyListener {
def keyPressed(e: java.awt.event.KeyEvent) { publish(new KeyPressed(e)) }
def keyReleased(e: java.awt.event.KeyEvent) { publish(new KeyReleased(e)) }
def keyTyped(e: java.awt.event.KeyEvent) { publish(new KeyTyped(e)) }
})
}

java.awt.event.KeyEvent 用作构造函数参数,但 KeyReleased 案例类有这个签名
case class KeyReleased(val source: Component, key: Key.Value, val modifiers: Key.Modifiers, 
location: Key.Location.Value)
(val peer: java.awt.event.KeyEvent) extends KeyEvent {
def this(e: java.awt.event.KeyEvent) =
this(UIElement.cachedWrapper[Component](e.getSource.asInstanceOf[JComponent]),
Key(e.getKeyCode), e.getModifiersEx, Key.Location(e.getKeyLocation))(e)
}

因此我无法访问 KeyEvent 参数。

最佳答案

您不必在 case 语句 react 中解压缩 KeyReleased 事件。只需匹配 Event 本身,您就可以调用它的消费。

reactions += {
case e: KeyReleased => {
println(e.source, e.key, e.modifiers)
e.consume
}
}

关于scala - 如何在 Scala react 中使用关键事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11767417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com