Scala MouseEvent - 如何知道按下了哪个按钮? [英] Scala MouseEvent - How to know which button was pressed?
问题描述
我正在使用 Scala Swing 编写一个 Scala 应用程序.我可以监听 MouseClicked
以在鼠标被点击时得到通知,但我怎么知道按下了哪个按钮.文档很糟糕,所以我查看了 MouseEvent
的 Java 文档,它说可以从修饰符字段中检索键,所以我尝试输出 scala MouseClicked
事件,结果是一个整数,0 表示左键单击,鼠标按钮 3 和鼠标按钮 4 以及 256 表示右键单击.
I'm writing a scala application using scala swing. I can listen for MouseClicked
to get notified whenever the mouse is clicked, but how do i know which button was pressed. The documentation is pretty bad, so i have looked in the Java documentation for MouseEvent
, which says that the key can be retrieved from the modifiers field, so i tried to output the modifiers field of the scala MouseClicked
event, which turned out to be an integer, 0 for left click, mouse button 3 and mouse button 4 and 256 for right click.
如果我只需要知道左键单击和右键单击之间的区别,它看起来会起作用,但是我可以确定所有修饰符都以这种方式起作用吗?文档什么也没说,据我所知,修改器可能会受到其他因素的影响,而不仅仅是按下了哪个鼠标按钮.我还想知道左键单击和鼠标按钮 3 和 4 之间的区别.我认为用户不希望它们做与左键单击相同的事情.
It looks like it will work if i only need to know the difference between left and right click, but can i be sure the modifiers all ways work this way? The documentation says nothing, and for what i know modifiers could be affected by other things than just which mouse button was pressed. I would also like to know the difference between left click and mouse button 3 and 4. I don't think users expect them to do the same thing as left click.
scala 文档中有一个 MouseButtonEvent
,它可以用于任何事情吗?文档没有说明它的作用.
In the scala documentation there is a MouseButtonEvent
, can this be used for anything? The documentation says nothing about what it does.
推荐答案
您始终可以使用 x.peer.getButton
来获取 Java 映射(其中 x
是您的Scala MouseClicked
事件).peer
几乎总是包含与 Scala 类等价的 Java——在这种情况下确实如此.
You can always use x.peer.getButton
to get the Java mapping (where x
is your Scala MouseClicked
event). peer
almost always contains a Java equivalent to the Scala class--and it does in this case.
这篇关于Scala MouseEvent - 如何知道按下了哪个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!