如何检查是否以及在 Swing 中按下了哪个鼠标按钮

问题描述:

如何检查当前是否按下了任何鼠标按钮,如果按下,是哪个按钮?

How can I check if currently any mouse button is pressed and if so, which one it is?

问题是我需要在MouseListener.mouseEntered() 中使用这种信息.我检查了 MouseEvent 但我找不到对我有帮助的方法.

The thing is that I need to use this kind of information in MouseListener.mouseEntered(). I checked MouseEvent but I could not find the method which would help me.

getButton() 方法似乎只有在按钮状态发生变化时才返回值.

The getButton() method seems to only return value if there has been change in buttons' state.

有没有办法在不手动跟踪这个情况下通过 MouseListener.mousePressed()/mouseReleased() 方法找到这个.

Is there a way to find this out without manually keeping track of this somehow vie MouseListener.mousePressed()/mouseReleased() methods.

如何检查当前是否按下了任何鼠标按钮,如果按下,是哪个按钮?

How can I check if currently any mouse button is pressed and if so, which one it is?

大概您想根据按下的按钮调用特定代码,以便您可以执行以下操作:

Presumably you want to invoke specific code depending on the button pressed so you can do something like:

if (SwingUtilities.isLeftMouseButton(...))
   // do something