jQuery: Detecting pressed mouse button during mousemove event

前端 未结 7 1489
心在旅途
心在旅途 2020-11-30 05:22

I tried to detect which mouse button -if any- is the user pressing during a mousemove event under jQuery, but I\'m getting ambiguous results:

no button press         


        
相关标签:
7条回答
  • 2020-11-30 05:51

    Most browsers (except Safari) *) now support the MouseEvent.buttons property (note: plural "buttons"), which is 1 when the left mouse button is pressed:

    $('#whichkey').bind('mousemove', function(e) { 
        $('#log').html('Pressed: ' + e.buttons);
    });
    

    https://jsfiddle.net/pdz2nzon/2

    *) The world is moving forward:
    https://webkit.org/blog/8016/release-notes-for-safari-technology-preview-43/
    https://trac.webkit.org/changeset/223264/webkit/
    https://bugs.webkit.org/show_bug.cgi?id=178214

    0 讨论(0)
  • 2020-11-30 05:54

    jQuery normalizes the which value so it will work across all browsers. I bet if you run your script you will find different e.button values.

    0 讨论(0)
  • 2020-11-30 05:56

    You can write a bit of code to keep track of the state of the left mouse button, and with a little function you can pre-process the event variable in the mousemove event.

    To keep track of the state of the LMB, bind an event to document level for mousedown and mouseup and check for e.which to set or clear the flag.

    The pre-processing is done by the tweakMouseMoveEvent() function in my code. To support IE versions < 9, you have to check if mouse buttons were released outside the window and clear the flag if so. Then you can change the passed event variable. If e.which was originally 1 (no button or LMB) and the current state of the left button is not pressed, just set e.which to 0, and use that in the rest of your mousemove event to check for no buttons pressed.

    The mousemove handler in my example just calls the tweak function passing the current event variable through, then outputs the value of e.which.

    $(function() {
        var leftButtonDown = false;
        $(document).mousedown(function(e){
            // Left mouse button was pressed, set flag
            if(e.which === 1) leftButtonDown = true;
        });
        $(document).mouseup(function(e){
            // Left mouse button was released, clear flag
            if(e.which === 1) leftButtonDown = false;
        });
    
        function tweakMouseMoveEvent(e){
            // Check from jQuery UI for IE versions < 9
            if ($.browser.msie && !e.button && !(document.documentMode >= 9)) {
                leftButtonDown = false;
            }
    
            // If left button is not set, set which to 0
            // This indicates no buttons pressed
            if(e.which === 1 && !leftButtonDown) e.which = 0;
        }
    
        $(document).mousemove(function(e) {
            // Call the tweak function to check for LMB and set correct e.which
            tweakMouseMoveEvent(e);
    
            $('body').text('which: ' + e.which);
        });
    });
    

    Try a live demo here: http://jsfiddle.net/G5Xr2/

    0 讨论(0)
  • 2020-11-30 05:58

    For some reason, when binding to mousemove event, the event.which property is set to 1 if left button or none is pressed.

    I changed to mousedown event and it worked Ok:

    • left: 1
    • middle: 2
    • right: 3

    Here's a working example: http://jsfiddle.net/marcosfromero/RrXbX/

    The jQuery code is:

    $('p').mousedown(function(event) {
        console.log(event.which);
        $('#button').text(event.which);
    });
    
    0 讨论(0)
  • 2020-11-30 05:59

    As of the date, the following works on Firefox 47, Chrome 54 and Safari 10.

    $('#whichkey').bind('mousemove',function(e){
        if (e.buttons & 1 || (e.buttons === undefined && e.which == 1)) {
            $('#log').html('left button pressed');
        } else if (e.buttons & 2 || (e.buttons === undefined && e.which == 3)) {
            $('#log').html('right button pressed');
        } else {
            $('#log').html('no button pressed');
        }
    });
    
    0 讨论(0)
  • 2020-11-30 05:59

    For users looking for similar solution but without the use of JQuery, here is a way of how I solved my problem:

        canvas.addEventListener("mousemove", updatePosition_mouseNtouch);
    
        function updatePosition_mouseNtouch (evt) {
          // IF mouse is down THEN set button press flag
          if(evt.which === 1)
            leftButtonDown = true;
          // If you need to detect other buttons, then make this here
          // ... else if(evt.which === 2) middleButtonDown = true;
          // ... else if(evt.which === 3) rightButtonDown = true;
          // IF no mouse button is pressed THEN reset press flag(s)
          else
            leftButtonDown = false;
    
          if (leftButtonDown) {
            /* do some stuff */
          }
        }
    

    I hope this is usefull to someone seeking an answer.

    0 讨论(0)
提交回复
热议问题