如何在 Swing 应用程序中使用后退和前进鼠标按钮?

发布于 2024-12-07 22:44:05 字数 150 浏览 0 评论 0原文

问题很简单。我找不到很多关于这个问题的链接,而且我找到的链接似乎并没有回避真正的问题。我的应用程序必须处理后退和前进鼠标按钮的鼠标按下/释放事件。我该如何处理这个问题?

编辑:这是使用JDK 1.6

The question is pretty simple. I couldn't find many links regarding this issue, and the ones I found didn't seemed to avoid the real question. My application must handle the mouse pressed/released events for the back and forward mouse buttons. How can I handle this?

EDIT: This is using JDK 1.6.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

┊风居住的梦幻卍 2024-12-14 22:44:05

通过调用检查是否检测到其他鼠标按钮:

MouseInfo.getNumberOfButtons();

检查当您单击这些附加按钮时是否会触发 MouseEvents。如果是这样,MouseInfo.html 是什么意思? getButton() 返回?

根据 MouseInfo.getButton() 的 javadocs

如果安装了具有五个按钮的鼠标,此方法可能会返回
以下值:

<前><代码>* 0(无按钮)
* 1(按钮1)
* 2(按钮2)
* 3(按钮3)
* 4
* 5

Check if additional mouse buttons are detected by calling:

MouseInfo.getNumberOfButtons();

Check if MouseEvents are fired when you click those additional buttons. If so, what does MouseInfo.getButton() return?

According to the javadocs for MouseInfo.getButton():

If a mouse with five buttons is installed, this method may return the
following values:

* 0 (NOBUTTON)
* 1 (BUTTON1)
* 2 (BUTTON2)
* 3 (BUTTON3)
* 4
* 5
情释 2024-12-14 22:44:05

功劳属于原始响应者,只需添加一个用于全局后退/前进按钮检测的现成代码示例,以防对其他人有帮助(JDK 1.8)

if (Toolkit.getDefaultToolkit().areExtraMouseButtonsEnabled() && MouseInfo.getNumberOfButtons() > 3) {
    Toolkit.getDefaultToolkit().addAWTEventListener(event -> {
        if (event instanceof MouseEvent) {
            MouseEvent mouseEvent = (MouseEvent) event;
            if (mouseEvent.getID() == MouseEvent.MOUSE_RELEASED && mouseEvent.getButton() > 3) {
                if (mouseEvent.getButton() == 4) {
                    // back
                } else if (mouseEvent.getButton() == 5) {
                    // forward
                }
            }
        }
    }, AWTEvent.MOUSE_EVENT_MASK);
}

Credit belongs to the original responders, just adding a ready-to-use code sample for global back-/forward-button detection in case it helps anyone else (JDK 1.8)

if (Toolkit.getDefaultToolkit().areExtraMouseButtonsEnabled() && MouseInfo.getNumberOfButtons() > 3) {
    Toolkit.getDefaultToolkit().addAWTEventListener(event -> {
        if (event instanceof MouseEvent) {
            MouseEvent mouseEvent = (MouseEvent) event;
            if (mouseEvent.getID() == MouseEvent.MOUSE_RELEASED && mouseEvent.getButton() > 3) {
                if (mouseEvent.getButton() == 4) {
                    // back
                } else if (mouseEvent.getButton() == 5) {
                    // forward
                }
            }
        }
    }, AWTEvent.MOUSE_EVENT_MASK);
}
§对你不离不弃 2024-12-14 22:44:05

我们如何区分“后退”和“前进”按钮?我们能确定按钮 4 是后退而 5 是前进吗?

我不使用 JDK7,也从未听说过后退/前进按钮。不过我确实知道 SwingUtilities 类有方法:

isRightMouseButton(MouseEvent)
isLeftMouseButton(MouseEvent) 
isMiddleMouseButton(MouseEvent) 

如果现在支持后退/前进,那么我猜他们已经添加了:

isBackMouseButton(MouseEvent)
isForwardMouseButton(MouseEvent) 

how can we distinguish between "back" and "forward" buttons? Can we be sure that button 4 is back and 5 is forward?

I don't use JDK7 and have never heard of back/forward buttons. However I do know that the SwingUtilities class has methods:

isRightMouseButton(MouseEvent)
isLeftMouseButton(MouseEvent) 
isMiddleMouseButton(MouseEvent) 

If back/forward are now supported then I would guess they have added:

isBackMouseButton(MouseEvent)
isForwardMouseButton(MouseEvent) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文