是否可以获得鼠标按钮4,5等?

时间:2016-04-11 15:45:36

标签: javascript mouselistener

简单地说,有没有办法在JavaScript中检测其他鼠标按键?其余的鼠标输入没有记录,所以我猜它不是标准实现。

有没有办法,比如图书馆,可以启用额外的鼠标按钮?

2 个答案:

答案 0 :(得分:1)

是的,你可以这样做,检查 MouseEvent.button ,请参阅下面的示例,它会检测3和4按钮点击。

  

某些指点设备提供或模拟更多按钮。要表示此类按钮,每个连续按钮(在二进制系列8,16,32,...中)的值必须加倍,如 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>

答案 1 :(得分:0)

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();
            break;

            case 4:
                console.log();
            break;

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