是否可以获取鼠标按钮4、5等? [英] Is it possible to get mouse buttons 4, 5, etc?

查看:109
本文介绍了是否可以获取鼠标按钮4、5等?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单地说,有什么方法可以检测JavaScript中的其他鼠标按钮按下情况吗?其余的鼠标输入中没有记录,所以我想它不在标准实现中.

Simply put, is there any way to detect additional mouse button presses in JavaScript? It's not documented with the rest of the mouse input, so I guess it's not in standard implementation.

有没有其他方法(例如库)可以启用额外的鼠标按钮?

Is there any way, such as a library, which can enable extra mouse buttons?

推荐答案

是的,您可以执行

Yes you could do this, check MouseEvent.button, see example below that will detect 3 and 4 buttons click.

某些定点设备提供或模拟更多按钮.为了表示这样的按钮,必须将每个连续按钮的值(在二进制序列8、16、32,...中)加倍,如

Some pointing devices provide or simulate more buttons. To represent such buttons, the value must be doubled for each successive button (in the binary series 8, 16, 32, ... ) as mentioned in https://www.w3.org/TR/DOM-Level-3-Events/#events-mouseevents

var whichButton = function (e) {
    // Handle different event models
    var e = e || window.event;
    var btnCode;

    if ('object' === typeof e) {
        btnCode = e.button;

        switch (btnCode) {
            case 3:
                console.log('Browser Back button clicked.');
            break;

            case 4:
                console.log('Browser Forward button clicked.');
            break;

            default:
                console.log('Unexpected code: ' + btnCode);
        }
    }
}

<button onmouseup="whichButton(event);" oncontextmenu="event.preventDefault();">Click with mouse...</button>

这篇关于是否可以获取鼠标按钮4、5等?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆