Firefox Mousemove event.which

2019-02-05 19:50发布

I'm writing an HTML5 canvas drawing app, and I need to be able to tell whether or not the left mouse button is pressed down during a mousemove event. On Chrome, this works:

if (event.which == 1) { <do stuff> }

But in FireFox, event.which always is 1, no matter whether or not the button is pressed.

How can I detect whether the mouse button is pressed during a mousemove event?

4条回答
我想做一个坏孩纸
2楼-- · 2019-02-05 20:38

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.

查看更多
我命由我不由天
3楼-- · 2019-02-05 20:52

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.

查看更多
女痞
4楼-- · 2019-02-05 20:56

I use this pseudo-polyfill to avoid FireFox mouseMove problems:

function fixWhich(e) {
    if (!e.which && e.button) { // if no which, but button (IE8-)
        if (e.button & 1)
            e.which = 1; // left
        else if (e.button & 4)
            e.which = 2; // middle
        else if (e.button & 2)
            e.which = 3; // right

    }
    if (e.buttons) { //if there's buttons, i set new option buttondown (FF)
        if (e.buttons & 1)
            e.buttondown = 1; // left
        else if (e.buttons & 4)
            e.buttondown = 2; // middle
        else if (e.buttons & 2)
            e.buttondown = 3; // right

    }
}

I call it "pseudo" because, it forces me to check the button down while moving this way:

if (e.buttondown === 1) {
        //do anything
    }

Instead of normal:

if (e.which === 1) {
       //do anything
    }
查看更多
我只想做你的唯一
5楼-- · 2019-02-05 20:57

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/

查看更多
登录 后发表回答