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
Use
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 ctrl-click example because it checks whether e.getModifiers() & 0x4 is non-zero. But the flag used for "command" is also 0x4.
So it will report cmd-click as a right-click but won't report ctrl-click as one. Worse yet, cmd-click will also return true to SwingUtilities.isLeftMouseButton(). If your code is written to handle left-clicks one way and right-clicks another, and you use a second if rather than an else if, you're in for a nasty surprise when both execute.
For those who are interested, these are the complete getModifiers() and getModifiersEx() values for single-modifier clicks.
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