Solved

How do I use the right mouse key event

Posted on 2001-06-08
7
779 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate example issues from command prompt 10 85
How to convert from xls to xlsx using java 7 159
add projects t working set in maven 2 71
Running JavaFX on the Raspberry Pi 27 292
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

751 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