Solved

How do I use the right mouse key event

Posted on 2001-06-08
7
774 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

706 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

19 Experts available now in Live!

Get 1:1 Help Now