Firefox Mousemove event.which Firefox Mousemove event.which javascript javascript

Firefox Mousemove event.which


In short, you can't. MDN, for example, explains:

When a mouse button event occurs, a number of additional properties are available to determine which mouse buttons were pressed and the location of the mouse pointer. The event's button property can be used to determine which button was pressed, where possible values are 0 for the left button, 1 for the middle button and 2 for the right button. If you've configured your mouse differently, these values may be different.

...

The button and detail properties only apply to the mouse button related events, not mouse movement events. For the mousemove event, for example, both properties will be set to 0.

If you want, you can set global mousedown and mouseup handlers that set flags appropriately and then at any given point in time you can with relative degree of accuracy determine if/which mouse button is pressed.


You should use a variable to hold the state of the mousedown.When you mousedown on the canvas set it to true, and when you mouseup on the document set it to false..

This way it will be cleared wherever you click..

Something like this

var isMouseDown = false;var draw = document.getElementById('draw');document.addEventListener('mouseup',                          function(){                              isMouseDown = false;                          },                          false);draw.addEventListener('mousedown',                      function(e){                          e.preventDefault(); // cancel element drag..                          isMouseDown = true;                      },                      false);draw.addEventListener('mousemove',                      function(e){                          if (isMouseDown){                              // do the drawing ..                          }                      },                      false);

Demo at http://jsfiddle.net/gaby/8JbaX/


Actually, you can check event.buttons on Firefox which tells you if left button, right button or none are pressed. You will need an if-statement to differentiate with Chrome which doesn't have event.buttons.