Change mouse keycode - jquery

11,447

Solution 1

With jQuery, 2 will represent the mouse wheel or the «middle click button». You cannot change these values, but you also do not have to, because if you get something other than 2 on f.which, it's most likely not the mousewheel that was downed.

There are two mouse event models, one by the W3C and one by Microsoft. Browsers (also including IE7 and IE8) implement the W3C standard.

UPDATE

In Javascript, the mouse event button is not normalized because of the two different standards. Actually, jQuery normalizes the which on the mouse event:

// Add which for click: 1 === left; 2 === middle; 3 === right
// Note: button is not normalized, so don't use it
if ( !event.which && button !== undefined ) {
  event.which = ( button & 1 ? 1 : ( button & 2 ? 3 : ( button & 4 ? 2 : 0 ) ) );
}

Solution 2

According to W3C its values should be:

  • Left button – 0
  • Middle button – 1
  • Right button – 2

According to Microsoft its values should be:

  • Left button – 1
  • Middle button – 4
  • Right button – 2

But you don't have to think about it (cross browser issue), leave it on jQuery, in every browser which will return the same result

$('a').mousedown(function(e) {
    if( (e.which == 1) ) {
        alert("left button");
    }if( (e.which == 3) ) {
        alert("right button");
    }else if( (e.which == 2) ) {
        alert("middle button"); 
    }
})​;

DEMO. (Tested in IE, Chrome and FF)

Share:
11,447
menislici
Author by

menislici

Updated on June 04, 2022

Comments

  • menislici
    menislici almost 2 years

    As I have heard, different browsers assign different keycode values to mouse buttons on mousedown event. I have tried this in Chrome and it worked:

    $('a').mousedown(function(f) {
        if (f.which == 2) {
            alert("You clicked the wheel")
        }
    })​
    

    However, I'm afraid some browsers will fail to recognize the mousewheel's keycode being 2 and will confuse it with the other mouse buttons, thus crashing the code. My question is if there is any way to reset these values so I can use the new ones I provide so all the browsers will know what the numbers represent exactly.

  • menislici
    menislici almost 12 years
    @MaxArt Have you seen my jsfiddle? In my chrome browser it represents the middle button.
  • Beat Richartz
    Beat Richartz almost 12 years
    @menislici I updated my answer, jQuery normalizes the event. Just had to learn that the hard way :)
  • menislici
    menislici almost 12 years
    @Beat Richartz so are you telling me that what I want to do has already been taken care of thanks to event.which?
  • MaxArt
    MaxArt almost 12 years
    It's incredible how a smart person like John Resig didn't endorse Microsoft's approach to event.button, but created yet another encoding instead, making it even more confusing. But then again, this is a disaster that had to be fixed long ago by W3C.
  • Beat Richartz
    Beat Richartz almost 12 years
    Quote from this link: «which is an old Netscape property. Left button gives a value of 1, middle button (mouse wheel) gives 2, right button gives 3. No problems, except its meager support (and the fact that it’s also used for key detection).» The weakness of the W3C approach is that combinations of the left button with another cannot be detected, the netscape model does not have this weakness. Apart from that, with the microsoft model, you have to ask yourself: 1,4 and 2... what the hell? Sounds like they wanted to much.