Solved

How do I use the right mouse key event

Posted on 2001-06-08
7
778 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
table example 4 32
MySQL  on Tomcat 8 71
by zero exception 10 52
Delphi Firemonkey: if the Sms contain special characters it won't send it 3 45
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…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

820 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