mouseDragged not returning appropriate button down
Asked Answered
K

4

13

How can I know the button that was pressed from within a mouseDragged event?

I'm having an issue in mouseDragged() because the received MouseEvent returns 0 for getButton(). I have no problem with the mouse location, or even detecting mouse clicks. The mouseClicked() event returns the appropriate button for getButton().

Any suggestions on how I can do this? I assume I could do a work-around using mouseClicked, or mousePressed, but I would prefer to keep this all within mouseDragged.

Thanks for your time and answers.

Karlee answered 3/7, 2013 at 6:21 Comment(2)
Can you drag with right mouse button, i only dragged with the left mouse buttonHellenist
I tested both buttons, and it sent 0 for both buttons.Karlee
P
25

As pointed out in comments and other answers, SwingUtilities provides three methods for cases like this, which should work for all MouseEvents:

SwingUtilities.isLeftMouseButton(aMouseEvent);
SwingUtilities.isRightMouseButton(aMouseEvent);
SwingUtilities.isMiddleMouseButton(aMouseEvent);

As for what the problem with your approach is, the javadoc of getButton() says:

Returns which, if any, of the mouse buttons has changed state.

Since the state of the button doesn't change while it is being held down, getButton() will usually return NO_BUTTON in mouseDragged. To check the state of buttons and modifiers like Ctrl, Alt, etc. in mouseDragged, you can use getModifiersEx(). As an example, the below code checks that BUTTON1 is down but BUTTON2 is not:

int b1 = MouseEvent.BUTTON1_DOWN_MASK;
int b2 = MouseEvent.BUTTON2_DOWN_MASK;
if ((e.getModifiersEx() & (b1 | b2)) == b1) {
    // ...
}
Phosphoresce answered 11/8, 2013 at 13:20 Comment(2)
You're right concerning the first part (javadoc), but I think using SwingUtilities.isLeftMouseButton() is even better (see other answer)Condole
@Condole Since this is already the accepted answer I added the SwingUtilities approach for better visibility (and expanded on why you might choose getModifiersEx instead).Phosphoresce
A
7

Jacob's right that getButton() doesn't get you the button by design. However, I found a cleaner solution than bit operations on getModifiersEx(), that you can also use within mouseDragged:

if (SwingUtilities.isLeftMouseButton(theMouseEvent)) {
    //do something
}

Similar methods exist for the middle button and the right button.

Accipitrine answered 19/6, 2014 at 19:5 Comment(2)
You're right. And I guess this is also better for portability than getModifiersEx() (MacOS, Linux, ...)Condole
@Condole In terms of portability, getModifiersEx() is exactly the same (very portable), because the isLeftMouseButton(..) &co. methods are implemented in terms of it. See here. These are simply more readable convenience methods.Alurd
P
2
int currentMouseButton = -1;
@Override
public void mousePressed(MouseEvent e) {
    currentMouseButton = e.getButton();
}

@Override
public void mouseReleased(MouseEvent e) {
    currentMouseButton = -1;
}

@Override
public void mouseDragged(MouseEvent e) {
    if (currentMouseButton == 3) {
        System.out.println("right button");
    }
}
Pusillanimity answered 27/12, 2013 at 1:0 Comment(0)
S
0

This could be possibly a problem of your java sandbox.

The following code works well all the time (almost, as you can see).

@Override
public void mouseDragged(MouseEvent e) {
    e.getButton();
}

Please try your code on a different machine.

Sputnik answered 3/7, 2013 at 6:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.