简体   繁体   English

确定是否按下了鼠标中键而不是Alt键

[英]Determine wether the middle mouse button is pressed but not the alt key

I experimented with java.awt.event.MouseEvent and mouse buttons and modifier keys. 我尝试了java.awt.event.MouseEvent和鼠标按钮以及修饰键。 Finally I came to a point, where I was confused by its behavior. 最终我到了一个地步,被它的行为所迷惑。

Normally I use SwingUtilities.isLeftMouseButton(...) etc. to detect, which mouse button is pressed and <MouseEvent>.isControlDown() etc. to detect, which modifier key is pressed. 通常我使用SwingUtilities.isLeftMouseButton(...)等来检测按下哪个鼠标按钮,并使用<MouseEvent>.isControlDown()等来检测按下哪个修改键。

But if I press the middle mouse button, the <MouseEvent>.isAltDown() -method seems to be always true, no matter the Alt-key is pressed or not (by the way same for right mouse button and meta key). 但是,如果我按下鼠标中键,则无论是否按下Alt键, <MouseEvent>.isAltDown() -方法似乎总是正确的(通过鼠标右键和meta键的方式)。

It seems some keys on keyboard share the same event flags as some mouse buttons. 似乎键盘上的某些键与某些鼠标按钮共享相同的事件标志。 How to fetch the middle mouse button in java? 如何在Java中获取鼠标中键? seems to confirm my assumption. 似乎证实了我的假设。

So my question: Is there a way to detect which mouse button is pressed and which modifiers are really pressed? 所以我的问题是:有没有办法检测按下哪个鼠标按钮以及真正按下哪个修饰符? Or is it better to use only Ctrl and Shift modifier keys for conditional mouse events? 还是仅对条件鼠标事件使用Ctrl和Shift修饰键?

OS: Windows 8, java version "1.7.0_09" 作业系统:Windows 8,Java版本“ 1.7.0_09”

Thank you in advance 先感谢您

In regards to your issue with the Middle mouse button... http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6495530 , it would appear that Sun/Oracle has known about this issue since 2006... 关于您的鼠标中键问题... http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6495530 ,似乎Sun / Oracle自2006年以来就知道此问题...

For other cases (at least simple ones) I do the following. 对于其他情况(至少是简单的情况),请执行以下操作。

@Override
public void mouseClicked(MouseEvent e) {
     if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON2){
         //Do some stuff...
         if (e.isControlDown()) {
             //Do something if control is down
         }else{
             //Something different if it is not down.
         }
     }
}

There is a similar helper for alt (which does not work with the middle button, its always true), shift and meta (is that the OS X key?). 对于alt(与中间按钮不起作用,始终为true),shift和meta(这是OS X键?)有类似的帮助器。

I have noticed odd behavior if you want to handle double clicks, and single clicks separately as Java seems to honor a double click, but handles the single click as well. 我注意到如果您要处理双击和单击是分开的情况,这是奇怪的行为,因为Java似乎可以接受双击,但也可以处理单击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM