tags:

views:

68

answers:

2

Hi all, For windows and linux I am able to detect right click. But for mac I donot know how to detect right-click.

How to write java program to detect right click for Mac OS

Thanks Sunil KUmar Sahoo

+2  A: 

It's the same as detecting a right-click on Windows or Linux—you call your given MouseEvent's getButton() method to check if BUTTON3 was clicked. For example, take a look at the following snippet of an example MouseListener:

public class MyListener implements MouseListener
{
    // ... code ...

    public void mouseClicked(MouseEvent event)
    {
        if (event.getButton() == MouseButton.BUTTON3)
        {
            // Right-click happened
        }
    }
}

However, this only detects right-clicks if the user actually has a two-button mouse. Since most Mac mice had only one button as of not-too-long-ago, you might want to consider detecting Control-clicks as well (which was the common idiom for right-clicking back then). If you decide to do so, the implementation is pretty trivial: just add another check for if event.isControlDown() returns true.

htw
Thank you for your help, Really It helped alot.
Deepak
+1  A: 

Instead of using MouseEvent.BUTTON3, a bettter self docummenting approach is to use

if (SwingUtilities.isRightMouseButton(event))
   // do something

Also, if you are using this code to display a popup menu, then you should not be using this approach since each OS has different key strokes to inovoke the popup menu. Read the section from the Swing tutorial on Bringing Up a Popup Menu.

camickr