最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Is it possible to get mouse buttons 4, 5, etc? - Stack Overflow

matteradmin8PV0评论

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?

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?

Share Improve this question edited Apr 11, 2016 at 15:57 Zakaria Acharki 67.5k15 gold badges78 silver badges106 bronze badges asked Apr 11, 2016 at 15:45 Gregory SimsGregory Sims 5611 gold badge8 silver badges19 bronze badges 2
  • 4 developer.mozilla/en-US/docs/Web/API/MouseEvent/button – Groben Commented Apr 11, 2016 at 15:51
  • 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/TR/DOM-Level-3-Events/#events-mouseevents – Korgrue Commented Apr 11, 2016 at 15:57
Add a ment  | 

2 Answers 2

Reset to default 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/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>

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);
        }
    }
}

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far