How to detect a right-click event for Mac OS

For windows and linux, I can detect right click. But for mac, I don't know how to detect a right click.

How to write a right-click java program for Mac OS

Thanks Sunil Kumar Sahoo

+6
java macos
Jun 04 '10 at 8:22
source share
4 answers

Just like detecting a right-click on Windows or Linux, you call your given MouseEvent getButton() method to check if BUTTON3 pressed. For example, consider the following excerpt from the MouseListener example:

 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 only had one button not too long ago, you might also want to consider detecting control clicks (which was the usual idiom for right-clicking). If you decide to do this, the implementation is pretty trivial: add one more check if event.isControlDown() returns true.

+7
Jun 04 '10 at 8:51
source share

Instead of using MouseEvent.BUTTON3, the self-learning approach in bettter is to use

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

Also, if you use this code to display a pop-up menu, you should not use this approach, since each OS has different keystrokes for the pop-up menu. Read the section in the Swing tutorial on Enabling the pop-up menu .

+15
Jun 04 '10 at 17:02
source share

Mouse click support should be added since Mac users may not use a mouse with a second button - for example, the trackpad does not have a right mouse button.

 @Override public void mouseClicked(MouseEvent e) { // Mac often uses control-click - isControlDown() if (SwingUtilities.isRightMouseButton(e) || e.isControlDown()) { // do something 
+2
Sep 29 '13 at 3:25
source share

Using

 private static boolean isRightClick(MouseEvent e) { return (e.getButton()==MouseEvent.BUTTON3 || (System.getProperty("os.name").contains("Mac OS X") && (e.getModifiers() & InputEvent.BUTTON1_MASK) != 0 && (e.getModifiers() & InputEvent.CTRL_MASK) != 0)); } 

SwingUtilities.isRightMouseButton() will not work. It is incorrectly implemented for the Mac example ctrl-click because it checks if e.getModifiers() & 0x4 non-zero. But the flag used for the β€œteam” is also 0x4 .

This way it will report cmd-click as a right-click, but it will not report ctrl-click as one. Even worse, cmd-click will also return true in SwingUtilities.isLeftMouseButton() . If your code is written to process the left click one way and right-click the other, and you use the second if , rather than else if , you have an unpleasant surprise when both are executed.

For those interested, these are the full values ​​of getModifiers() and getModifiersEx() for clicks with a single modifier.

 Left click: (button 1) Basic: 0000010000 0000000000 16 0 Shift: 0000010001 0001000000 17 64 Ctrl: 0000010010 0010000000 18 128 Cmd: 0000010100 0100000000 20 256 Opt: 0000011000 1000000000 24 512 Mid click: (button 2) Basic: 0000001000 1000000000 8 512 Shift: 0000001001 0001000000 9 64 Ctrl: 0000001010 0010000000 10 128 Cmd: 0000001100 0100000000 12 256 Opt: 0000001000 1000000000 8 512 Right click: (button 3) Basic: 0000000100 0100000000 4 256 Shift: 0000000101 0001000000 5 64 Ctrl: 0000000110 0010000000 6 128 Cmd: 0000010100 0100000000 20 256 Opt: 0000001100 1000000000 12 512 
+1
Jun 19 '15 at 15:09
source share



All Articles