BUTTON1_MASK和BUTTON1_DOWN_MASK之间的区别是什么?

BUTTON1_MASK和BUTTON1_DOWN_MASK之间的区别是什么?

问题描述:

来自java网站:

BUTTON1_DOWN_MASK = The Mouse Button1 extended modifier constant.
BUTTON1_MASK = The Mouse Button1 modifier constant.

我甚至不确定修饰符常量是什么。更不用说扩展了一个。
但我确实理解 BUTTON1_MASK 只是点击鼠标左键时的整数表示。

I'm not even sure what a "modifier constant" is. Let alone an extended one. I do understand however that BUTTON1_MASK is just the integer representation for when the left mouse button is clicked.

BUTTON1_MASK 是指示来自按钮1的事件的掩码。 BUTTON1_DOWN_MASK 在概念上类似,但是该常量的扩展版本。

BUTTON1_MASK is the mask indicating an event came from button 1. BUTTON1_DOWN_MASK is conceptually similar, but is the extended version of that constant.

有两种方法可以返回这些常量集: InputEvent#getModifiers() InputEvent#getModifiersEx(),它们将返回修饰符常量,或扩展修饰符常量。

There are two methods that return such sets of constants: InputEvent#getModifiers() and InputEvent#getModifiersEx(), and they will return modifier constants, or extended modifier constants, respectively.

来自文档(粗体是我的)


返回的按钮掩码InputEvent.getModifiers()仅反映
更改状态的按钮,而不反映所有按钮
当前状态。 ..要获取所有按钮和修饰符
键的状态,请使用InputEvent.getModifiersEx()。

The button mask returned by InputEvent.getModifiers() reflects only the button that changed state, not the current state of all buttons ... To get the state of all buttons and modifier keys, use InputEvent.getModifiersEx().

并且(粗体是我的)


扩展修饰符表示所有模态键的状态,例如ALT,
CTRL,META和鼠标按钮 事件发生后

Extended modifiers represent the state of all modal keys, such as ALT, CTRL, META, and the mouse buttons just after the event occurred

例如,如果用户按下按钮1,然后按下按钮2,然后
以相同的顺序发布它们,生成以下事件序列

For example, if the user presses button 1 followed by button 2, and then releases them in the same order, the following sequence of events is generated:

MOUSE_PRESSED:  BUTTON1_DOWN_MASK
MOUSE_PRESSED:  BUTTON1_DOWN_MASK | BUTTON2_DOWN_MASK
MOUSE_RELEASED: BUTTON2_DOWN_MASK
MOUSE_CLICKED:  BUTTON2_DOWN_MASK
MOUSE_RELEASED:
MOUSE_CLICKED:


如果你想要的只是检测按钮1(通常是左)点击,那么其中任何一个都应该有效:

If all you want is to detect a button 1 (normally, left) click, then either of these should work:

if ((e.getModifiers() & MouseEvent.BUTTON1_MASK) != 0) {
    System.out.println("BUTTON1_MASK");
}

if ((e.getModifiersEx() & MouseEvent.BUTTON1_DOWN_MASK) != 0) {
    System.out.println("BUTTON1_DOWN_MASK");
}

另外,你可以查看 InputEvent 有更多有用的评论,并显示内部发生的事情