Is it possible to get mouse buttons 4, 5, etc.?

Simply put, is there a way to detect extra mouse clicks in JavaScript? It is not documented with the rest of the mouse input, so I assume this is not a standard implementation.

Is there a way, for example, a library that can include additional mouse buttons?

+4
source share
1 answer

Yes, you can do this, check MouseEvent.button , see the example below, in which buttons 3 and 4 will be detected, click the button.

. ( 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>
Hide result
0

All Articles