Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I use the right mouse key event

Posted on 2001-06-08
7
Medium Priority
?
782 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 600 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

670 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