Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect right-click event for Mac OS

Tags:

java

macos

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

like image 282
Sunil Kumar Sahoo Avatar asked Jun 04 '10 08:06

Sunil Kumar Sahoo


2 Answers

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.

like image 131
camickr Avatar answered Sep 22 '22 15:09

camickr


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.

like image 44
hbw Avatar answered Sep 23 '22 15:09

hbw