Is it possible to get mouse buttons 4, 5, etc?
Asked Answered
W

2

8

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?

Wellborn answered 11/4, 2016 at 15:45 Comment(2)
developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttonSmarm
The working draft of the spec is found at the link below - the mozilla implementation of detecting auxiliary button clicks as described in the link provided by @Briggy should work cross browser. w3.org/TR/DOM-Level-3-Events/#events-mouseeventsUnceasing
I
4

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

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>
Icterus answered 11/4, 2016 at 15:56 Comment(3)
To represent such buttons, the value must be doubled for each successive button (in the binary series 8, 16, 32, ... ) check my update.Icterus
This doesn't seem to actually work, for me, using Chrome, IE, Edge, Firefox or Opera.Wellborn
As implied by the switch statement's contents, mouse buttons 3 and 4 are already binded to the browser's back and forward actions in Chrome, Firefox, and Edge (and I presume more). I have created this CodePen to add an e.preventDefault(); under the var e declaration to suppress the binded browser action. I have verified this works in Chrome 71, but unfortunately in both FireFox 65 and Edge 42, e.preventDefault() does not suppress the binded browser action. Does anyone know a way to suppress it in all browsers?Mislead
A
-1
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);
        }
    }
}
Awoke answered 25/11, 2020 at 0:58 Comment(1)
Always include the test to explain your code's logic, otherwise, it may not help otherOnceover

© 2022 - 2024 — McMap. All rights reserved.