Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 248
Gregory Sims Avatar asked Apr 11 '16 15:04

Gregory Sims


People also ask

Which mouse button is mouse 4 and 5?

Mouse 5 is forward (upper side key), Mouse 4 is backwards (lower side key). It's the same as in other brand mouses.

How many types of mouse buttons are there?

Many standard mice have two buttons: a left button and a right button. If you are right handed, the left mouse button will be directly under your index finger when you place your hand on the mouse.

What is mouse button 5 called?

Typing 5 (with the numeric keypad) is equivalent to clicking the selected button. By default, the selected button is the primary button (nominally under index finger, left button for most right-handed people and right button for most left-handed people).


1 Answers

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>
like image 174
Zakaria Acharki Avatar answered Oct 09 '22 07:10

Zakaria Acharki