Steven Lu Steven Lu - 1 year ago 54
Javascript Question

Browser JavaScript mousedown event: To detect right mouse button: Do I look at "button" or "which"?

In the (good?) old days jQuery normalized

to 1, 2, and 3 for left, middle and right mouse buttons respectively.

The MDN page indicates that
is standard, producing values of 0, 1, and 2.

On the desktop, Chrome, Safari, Firefox, IE11, and Edge all provide both
, so either one seems like will work.

(I am writing raw JS by the way. No JS library dependencies. I'm listening for

Which one is the one that we want to use moving forward?

Answer Source

Which one is the one that we want to use moving forward?

Well, the specs tells about the button property, but which.

So this should answer the question, but you can use which as long as it is supported by the browsers independently from W3C specs.

Scott showed that bitmap of 1, 2, 4

Which is also in the current spec under buttons

The buttons being pressed when the mouse event was fired: Left button=1, Right button=2, Middle (wheel) button=4, 4th button (typically, "Browser Back" button)=8, 5th button (typically, "Browser Forward" button)=16. If two or more buttons are pressed, returns the logical sum of the values. E.g., if Left button and Right button are pressed, returns 3 (=1 | 2).

Maybe interesting for some drawing applications, with multiple gestures types..