How to detect right-click event for Mac OS
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
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.
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.
Control-click support needs to be added as Mac users might not be using a mouse with second button - e.g. A trackpad doesn't 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
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
精彩评论