Scala MouseEvent - How to know which button was pressed?
Asked Answered
P

1

8

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.

Python answered 13/9, 2010 at 13:16 Comment(0)
N
5

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.

Nucleoside answered 13/9, 2010 at 13:22 Comment(3)
Eclipse with the scala plugin won't compile, when i have this in my code: e.peer.BUTTON1 from what i've read, i should be able to access static fields in my scala code. Am i doing something wrong, or should i get another IDE?Python
You have to access them via the class, not via the instance: if (e.peer.getButton == java.awt.event.MouseEvent.BUTTON1), as an example.Nucleoside
Thank you... i had completely forgotten that.Python

© 2022 - 2024 — McMap. All rights reserved.