Hi again.

I'm trying to get Java to recognize the action of pressing both mouse buttons in the same time. I did some research and I found that

Event.getModifiersEx() can help me do what I want, but I can't figure out how to implement it the correct way.

Event.BUTTON1_DOWN_MASK is the left mouse button
Event.BUTTON3_DOWN_MASK is the right mouse button

but how do I check that both of these are being held down at the same time using getModifiersEx()?

I tried everything, but I cant figure it out...

Recommended Answers

All 5 Replies

In the mousePressed() method, use the following

int bothMask = MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON3_DOWN_MASK;
    if ((evt.getModifiersEx() & bothMask) == bothMask){
        // do your thing
        System.out.println("Both down");
    }

Sorry for the late reply..

It works, but I have to press left THEN right or right THEN left... If I press them the same exact time, I get nothin which is what I need to do the action...

Any more suggestions?

EDIT: alright, I got it.. For those wondering,

public void mousePressed(MouseEvent me){
     int onMask = MouseEvent.BUTTON1_DOWN_MASK & MouseEvent.BUTTON3_DOWN_MASK;
     int offMask = MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON3_DOWN_MASK;
     if ((me.getModifiersEx() & (onMask | offMask)) == onMask){
           System.out.println("Both Down!");
     }
}

(this is the mousePressed() method for the class implementing MouseListener)


Thanks once again Ezzaral... You're awesome :)

Glad you got it working the way you want.

Bah.. I was mistaken..... This:

public void mousePressed(MouseEvent me){
     int onMask = MouseEvent.BUTTON1_DOWN_MASK & MouseEvent.BUTTON3_DOWN_MASK;
     int offMask = MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON3_DOWN_MASK;
     if ((me.getModifiersEx() & (onMask | offMask)) == onMask){
           System.out.println("Both Down!");
     }
}

is actually a way to detect the MIDDLE mouse clicks... I was programming on my laptop the whole time which uses left+right as middle click since there is no middle mouse on the touchpad.... so i though it was working...

int bothMask = MouseEvent.BUTTON1_DOWN_MASK | MouseEvent.BUTTON3_DOWN_MASK;
    if ((evt.getModifiersEx() & bothMask) == bothMask){
        System.out.println("Both down");
    }

which is what you recommended Ezzaral, says "Both down" for Left click only, right click only, and says it twice for left+right click....

I'm trying all possible combinations but I can't figure this out... There has to be a way to detect both left+right click as just 1 action...

I don't believe there is any facility to report "both mouse buttons clicked" because the buttons are separate. I doubt the mouse can even report "simultaneous" clicks on multiple buttons from the hardware. The nearest approximation you can capture is "one button clicked soon after another while the other is still held down". That code I posted above only reports "Both down" when both buttons are pressed down on my machine here, so I can't speak to why it's behaving differently for you.

Perhaps you should put some debug statements in to print the values of the modifiers and the masks, or if you are using an IDE debugger, set a breakpoint and examine the values.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.