Solved

How do I use the right mouse key event

Posted on 2001-06-08
7
776 Views
Last Modified: 2007-12-19
Does Java provide the functionality to capture the right mouse key event?  Please provide an example.

Thanks!
0
Comment
Question by:fairfax
7 Comments
 
LVL 2

Accepted Solution

by:
venkat2000120699 earned 150 total points
ID: 6169307
There is one method in MouseEvent class, "int getModifiers()".  It returns the state of all the modifier keys and mouse buttons when the event was fired. You can use this method to determine which mouse button was pressed (or newly released) when a mouse event was fired. The InputEvent class defines these constants for use with the getModifiers method: ALT_MASK, BUTTON1_MASK, BUTTON2__MASK, BUTTON3_MASK, CTRL_MASK, META_MASK, and SHIFT_MASK. For example, the following expression is true if the right button was pressed:
(mouseEvent.getModifiers() & InputEvent.BUTTON3_MASK)
== InputEvent.BUTTON3_MASK

Following is a fully working example.

Test.java
~~~~~~~~~

import java.awt.*;
import java.awt.event.*;

public class Test extends Frame {
    int i;
   
    public Test() {
        super("Test Frame");
        addMouseListener(new MouseHandler());
        setSize(300,300);
        setVisible(true);
    }
   
    public static void main(String args[]) {
        new Test();
    }
   
    private class MouseHandler extends MouseAdapter {
        public void mouseClicked(MouseEvent me) {
            System.out.println("Mouse Clicked: " + i++);
            if((me.getModifiers() & me.BUTTON1_MASK) == me.BUTTON1_MASK) {
                 System.out.println("Left button of the mouse is clicked");
            } else if((me.getModifiers() & me.BUTTON2_MASK) == me.BUTTON2_MASK) {
                 System.out.println("Middle button of the mouse is clicked");
            } else if((me.getModifiers() & me.BUTTON3_MASK) == me.BUTTON3_MASK) {
                 System.out.println("Right button of the mouse is clicked");
            }
           
        }
    }
}

Hope that this answers ur query.
0
 

Expert Comment

by:smileyxx
ID: 6169327
I'm not sure how you tell which button is the right, 2 or 3? It is platform and hardware dependent.

However, something like this should tell you

import java.awt.event.*;
import
Component.addMouseListener(new MouseListener() {
  public void mouseClicked(MouseEvent e) {
    int modifiers = e.getModifiers();
    // check for button 3
    if((modifiers & InputEvent.BUTTON3_MASK)!=0) { }
    // check for button 2
    if((modifiers & InputEvent.BUTTON2_MASK)!=0) { }
  }
});
0
 
LVL 92

Expert Comment

by:objects
ID: 6172425
And on the Mac, it's button 1 :-)

Finding which button number is pressed is simple as pointed out, but knowing whether its the right button is a different question. (Try running the 'proposed solution' with a two button mouse).

If your looking for a cross-platform solution then you're probably better off using the isPopupTrigger() method in MouseEvent. It returns true if the mouse event is the popup-menu trigger event for the specific platform. Generally this is the right mouse button.


0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:venkat2000120699
ID: 6172730
I think that program will work fine on Mac too.  I don't have a scope to test it on Mac.  It is working fine on windows platform.  I have visited the following links for finding out a solution for this, but I didn't find anyone of them mentioning about platform dependency.

http://www.jguru.com/faq/view.jsp?EID=4147
http://java.sun.com/docs/books/tutorial/uiswing/events/mouselistener.html
http://www.informatik.fh-wiesbaden.de/~turau/java/faq_c.html - See Question number 39.

http://www.bit.umkc.edu/vu/course/CS349/lectures/GUI-Components/gui-components.html  -  Search for "Right and Left Mouse Buttons" on the page.

There references may clarify your doubt.  Otherwise, the best way is to test it on different platforms.
0
 
LVL 2

Expert Comment

by:venkat2000120699
ID: 6172733
The last link specified that Mac mouses will have only one button, and he gave a similar example, as that for windows.
0
 
LVL 92

Expert Comment

by:objects
ID: 6172758
From one of your references:

"The mouse listener methods don't distinguish between a right and left mouse click. Detecting right and left mouse clicks isn't an integral part of the API. This isn't unreasonable since right and left mouse clicks are not supported on all platforms."

The code you provided will work fine most of the time, and is the 'standard' answer to the question. But it's not truly platform independant, for example left handers and Mac users will find it either annoying or unusable.
Though this is more an issue regarding interface design than with your code :)
0
 

Author Comment

by:fairfax
ID: 6188204
Excellent answer!! Thanks for your help
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now