Scala MouseEvent - How to know which button was pressed?

大城市里の小女人 提交于 2020-01-02 02:31:06

问题


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.

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.

In the scala documentation there is a MouseButtonEvent, can this be used for anything? The documentation says nothing about what it does.


回答1:


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.



来源:https://stackoverflow.com/questions/3700730/scala-mouseevent-how-to-know-which-button-was-pressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!