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
pgoldste Jun 19 '15 at 15:09 2015-06-19 15:09
source share