aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Is it RightClick ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Is it RightClick ?" Watch "Is it RightClick ?" New topic
Author

Is it RightClick ?

Manikandan Adaikkalavan
Ranch Hand

Joined: May 17, 2002
Posts: 35
Hi,
how to detect whether itz Mouse's RightClick or LeftClick with the help of mouse event ??
I need to find out the right click ?

Thanks in advance,
Manikandan
Manikandan Adaikkalavan
Ranch Hand

Joined: May 17, 2002
Posts: 35
i got it.
Answer is
if ((e.getModifiers() & InputEvent.BUTTON3_MASK) != 0)
System.out.println("RIGHT CLICK");
else System.out.println("LEFT CLICK");
Matt Senecal
Ranch Hand

Joined: Dec 01, 2000
Posts: 255
This should work:


There's nothing more dangerous than a resourceful idiot. ---Dilbert
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Perhaps the documentation on MouseEvent will give you an idea...


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Ah yes, the old three-fer response... and you answered your own question in one of the responses - not bad, not bad.
Here's an example another strategy you could use:
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Note that your example code would consider a click from the middle mouse button (if it exists) as a click from the left mouse button.
Manikandan Adaikkalavan
Ranch Hand

Joined: May 17, 2002
Posts: 35
whether the following condition fails
if ((e.getModifiers() & InputEvent.BUTTON3_MASK) != 0)
{}
......
if there is a middle button in MOUSE ?
Manikandan Adaikkalavan
Ranch Hand

Joined: May 17, 2002
Posts: 35
I tried this example...but giving compilation error
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
another solution i used to do myself was to use the methods: isMetaDown() and isControlDown()
if one of them returns true (the first i think) the the right button is pressed if the second returns true the middle button is pressed.
(could be the other way around , just check it).
this , in my opinion is a pretty good solution even before jdk 1.4 (before the getButton() method) because it works on macintoshes who only have 1 button mouse. so pressing control while pressing the one button emulates right button like it should.
Manikandan Adaikkalavan
Ranch Hand

Joined: May 17, 2002
Posts: 35
Hi,
Sorry .....I totally got confused...
i need to have a check for rightclick whether the mouse has one button or two buttons or three buttons ...
whether the following code works fine for all cases ???
if ((e.getModifiers() & InputEvent.BUTTON3_MASK) != 0)
{
// Itz Right Click
}

I have two buttons in my Mouse and this works great....for other two ....i don't know....
Will somebody please help me out ???

Thanks,
Manikandan
Manikandan Adaikkalavan
Ranch Hand

Joined: May 17, 2002
Posts: 35
Hi,
i got another solution....
There are three methods in
javax.swing.SwingUtilities class
public static boolean isLeftMouseButton(MouseEvent anEvent)
public static boolean isRightMouseButton(MouseEvent anEvent)
public static boolean isMiddleMouseButton(MouseEvent anEvent)
which can be used to find out which button is clicked!

- Manikandan
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
well done!
i was looking for those!
couldnt remember where i saw them.
 
Don't get me started about those stupid light bulbs.
 
subject: Is it RightClick ?