Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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 <code>mousemove</code> event.</p> <p>To keep track of the state of the LMB, bind an event to document level for <code>mousedown</code> and <code>mouseup</code> and check for <code>e.which</code> to set or clear the flag.</p> <p>The pre-processing is done by the <code>tweakMouseMoveEvent()</code> function in my code. To support IE versions &lt; 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 <code>e.which</code> was originally 1 (no button or LMB) and the current state of the left button is not pressed, just set <code>e.which</code> to <code>0</code>, and use that in the rest of your <code>mousemove</code> event to check for no buttons pressed.</p> <p>The <code>mousemove</code> handler in my example just calls the tweak function passing the current event variable through, then outputs the value of <code>e.which</code>.</p> <pre><code>$(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 &lt; 9 if ($.browser.msie &amp;&amp; !e.button &amp;&amp; !(document.documentMode &gt;= 9)) { leftButtonDown = false; } // If left button is not set, set which to 0 // This indicates no buttons pressed if(e.which === 1 &amp;&amp; !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); }); }); </code></pre> <p>Try a live demo here: <a href="http://jsfiddle.net/G5Xr2/">http://jsfiddle.net/G5Xr2/</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload