Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5976625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:15:19+00:00 2026-05-22T21:15:19+00:00

I tried to detect which mouse button -if any- is the user pressing during

  • 0

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 pressed:      e.which = 1   e.button = 0
left button pressed:    e.which = 1   e.button = 0
middle button pressed:  e.which = 2   e.button = 1
right button pressed:   e.which = 3   e.button = 2

Code:

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>

<input id="whichkey" value="type something">
<div id="log"></div>
<script>$('#whichkey').bind('mousemove',function(e){ 
  $('#log').html(e.which + ' : ' +  e.button );
});  </script>

</body>
</html>

How can I tell the difference between left mouse button pressed and no button at all?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:15:20+00:00Added an answer on May 22, 2026 at 9:15 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Tried examples from 'php.net' but don't understand what's the problem. Any suggestions? <?php $_SESSION['test']
I'm trying to detect which version .NET is installed using WiX. I've tried: <Condition
Tried to map it from Preferences -> Settings -> Keyboard, but the key combo
Tried a bunch of things but I can't get it to work consistently amid
tried to find the answer by googling and in MSDN but with no luck.
Tried to install some program, it gives above message requiring 1.1.4322. BUT as the
tried to research on that but sometimes I seem to lack some googling skills...
I'm trying to detect which controller I'm using or what controller I'm on and
Possible Duplicate: Best way to detect integer overflow in C/C++ i have tried to
I am trying to detect when an item is checked, and which item is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.