What is the difference between BUTTON1_MASK and BUTTON1_DOWN_MASK?

On java website:

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

I'm not even sure what a "modifier constant" is. Not to mention extended. However, I understand that BUTTON1_MASK is just an integer representation when the left mouse button is pressed.

+8
java input
source share
2 answers

BUTTON1_MASK is a mask indicating that an event has occurred from button 1. BUTTON1_DOWN_MASK conceptually similar, but is a version of this extended one .

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

From the documents (bold - mine) :

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

and also (bold - mine) :

Advanced modifiers represent the state of all modal keys such as ALT, CTRL, META and mouse buttons after an event has occurred.

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

 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: 

If you only want to find button 1 (usually on the left), then any of them 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"); } 

Alternatively, you can check out this open source version of InputEvent , which contains some more useful comments and shows what happens inside

+8
source share

As the state of docs, BUTTON1_MASK and BUTTON1_DOWN_MASK are modifier constants, that is, they are used in conjunction with MouseEvent#getModifiers . They are not expanded, but are used as mask values, for example

 @Override public void mousePressed(MouseEvent me) { if ((me.getModifiers() & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) { System.out.println("Left button pressed."); } } 

BUTTON1_DOWN_MASK used to determine the state of the mouse button, while BUTTON1_MASK simply helps determine which button is pressed.

+3
source share

All Articles