How to fetch the middle mouse button in java?

做~自己de王妃 提交于 2020-03-26 04:27:17

问题


I use public boolean mouseDown(Event ev, int x, int y) to detect a click of the mouse.
I can distinguish between the right mouse button (ev.metaDown() is true) and the left and middle.

How can i differentiate the left from the middle button? Or if it is impossible with mouseDown, what should i use?


回答1:


Try using ALT_MASK:

This flag indicates that the Alt key was down when the event occurred. For mouse events, this flag indicates that the middle mouse button was pressed or released.

So your code might be:

if (ev.modifiers & Event.ALT_MASK != 0) {
    // middle button was pressed
}

Of course, all this is assuming you have a very good reason to use mouseDown in the first place, since it is deprecated. You should (probably) be using processMouseEvent instead, which gives you a MouseEvent to play with.




回答2:


mouseDown is deprecated. All you need is accessible by the MouseEvent.getButton. Track BUTTON3.




回答3:


This might do it:

http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/SwingUtilities.html#isMiddleMouseButton(java.awt.event.MouseEvent)

Haven't tried it myself.



来源:https://stackoverflow.com/questions/195451/how-to-fetch-the-middle-mouse-button-in-java

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