macos 如何检测 Mac OS 的右键单击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2972512/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:58:51  来源:igfitidea点击:

How to detect right-click event for Mac OS

javamacos

提问by Sunil Kumar Sahoo

For windows and linux I am able to detect right click. But for mac I donot know how to detect right-click.

对于 windows 和 linux,我能够检测到右键单击。但是对于 mac 我不知道如何检测右键单击。

How to write java program to detect right click for Mac OS

如何编写Java程序来检测Mac OS的右键单击

Thanks Sunil KUmar Sahoo

感谢 Sunil KUmar Sahoo

回答by camickr

Instead of using MouseEvent.BUTTON3, a bettter self docummenting approach is to use

而不是使用 MouseEvent.BUTTON3,更好的自我记录方法是使用

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.

此外,如果您使用此代码来显示弹出菜单,那么您不应该使用这种方法,因为每个操作系统都有不同的按键来调用弹出菜单。阅读 Swing 教程中有关创建弹出菜单的部分

回答by hbw

It's the same as detecting a right-click on Windows or Linux—you call your given MouseEvent's getButton()method to check if BUTTON3was clicked. For example, take a look at the following snippet of an example MouseListener:

这与在 Windows 或 Linux 上检测右键单击相同——您调用给定的 MouseEventgetButton()方法来检查是否BUTTON3被单击。例如,请看以下示例 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.

但是,这仅在用户实际使用两键鼠标时检测右键单击。由于不久前大多数 Mac 鼠标只有一个按钮,因此您可能还需要考虑检测 Control-click(这是当时右键单击的常用习惯用法)。如果您决定这样做,则实现非常简单:只需添加另一个检查是否event.isControlDown()返回 true。

回答by null

Control-click supportneeds 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.

需要添加Control-click 支持,因为 Mac 用户可能不会使用带有第二个按钮的鼠标 - 例如,触控板没有鼠标右键。

@Override
public void mouseClicked(MouseEvent e) {
   // Mac often uses control-click - isControlDown()
   if (SwingUtilities.isRightMouseButton(e) || e.isControlDown()) {
      // do something

回答by pgoldste

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() & 0x4is non-zero. But the flag used for "command" is also 0x4.

SwingUtilities.isRightMouseButton()不管用。对于 Mac ctrl-click 示例,它被错误地实现,因为它检查是否e.getModifiers() & 0x4非零。但是用于“命令”的标志也是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 trueto SwingUtilities.isLeftMouseButton(). If your code is written to handle left-clicks one way and right-clicks another, and you use a second ifrather than an else if, you're in for a nasty surprise when both execute.

因此,它会将 cmd-click 报告为右键单击,但不会将 ctrl-click 报告为一个。更糟糕的是,CMD-点击也将返回trueSwingUtilities.isLeftMouseButton()。如果您的代码被编写为以一种方式处理左键单击并右键单击另一种方式,并且您使用的是第二个if而不是else if,那么当两者都执行时,您会遇到令人讨厌的惊喜。

For those who are interested, these are the complete getModifiers()and getModifiersEx()values for single-modifier clicks.

对于那些感兴趣的人,这些是单修饰符点击的完整getModifiers()getModifiersEx()值。

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