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 91211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:59:40+00:00 2026-05-10T22:59:40+00:00

Is there a way to detect if a mouse button is currently down in

  • 0

Is there a way to detect if a mouse button is currently down in JavaScript?

I know about the ‘mousedown’ event, but that’s not what I need. Some time AFTER the mouse button is pressed, I want to be able to detect if it is still pressed down.

Is this possible?

  • 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. 2026-05-10T22:59:40+00:00Added an answer on May 10, 2026 at 10:59 pm

    Regarding Pax’ solution: it doesn’t work if user clicks more than one button intentionally or accidentally. Don’t ask me how I know :-(.

    The correct code should be like that:

    var mouseDown = 0; document.body.onmousedown = function() {    ++mouseDown; } document.body.onmouseup = function() {   --mouseDown; } 

    With the test like this:

    if(mouseDown){   // crikey! isn't she a beauty? } 

    If you want to know what button is pressed, be prepared to make mouseDown an array of counters and count them separately for separate buttons:

    // let's pretend that a mouse doesn't have more than 9 buttons var mouseDown = [0, 0, 0, 0, 0, 0, 0, 0, 0],     mouseDownCount = 0; document.body.onmousedown = function(evt) {    ++mouseDown[evt.button];   ++mouseDownCount; } document.body.onmouseup = function(evt) {   --mouseDown[evt.button];   --mouseDownCount; } 

    Now you can check what buttons were pressed exactly:

    if(mouseDownCount){   // alright, let's lift the little bugger up!   for(var i = 0; i < mouseDown.length; ++i){     if(mouseDown[i]){       // we found it right there!     }   } } 

    Now be warned that the code above would work only for standard-compliant browsers that pass you a button number starting from 0 and up. IE uses a bit mask of currently pressed buttons:

    • 0 for "nothing is pressed"
    • 1 for left
    • 2 for right
    • 4 for middle
    • and any combination of above, e.g., 5 for left + middle

    So adjust your code accordingly! I leave it as an exercise.

    And remember: IE uses a global event object called … "event".

    Incidentally IE has a feature useful in your case: when other browsers send "button" only for mouse button events (onclick, onmousedown, and onmouseup), IE sends it with onmousemove too. So you can start listening for onmousemove when you need to know the button state, and check for evt.button as soon as you got it — now you know what mouse buttons were pressed:

    // for IE only! document.body.onmousemove = function(){   if(event.button){     // aha! we caught a feisty little sheila!   } }; 

    Of course you get nothing if she plays dead and not moving.

    Relevant links:

    • MouseEvent’s button (DOM 2)
    • MSDN’s button

    Update #1: I don’t know why I carried over the document.body-style of code. It will be better to attach event handlers directly to the document.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 165k
  • Answers 165k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Categories extend the original class, but they don't subclass it,… May 12, 2026 at 12:54 pm
  • Editorial Team
    Editorial Team added an answer Haven't tested this, but it's something like: RewriteRule \.php$ -… May 12, 2026 at 12:54 pm
  • Editorial Team
    Editorial Team added an answer "0:0:0:0:0:0:0:1" is the IPv6 loopback address as defined in RFC… May 12, 2026 at 12:54 pm

Related Questions

Is there a way to detect when a specific device is plugged into a
I have a JPanel, which I would like to detect the following events (1)
I'm writing a small tray application that needs to detect the last time a
It seems to me there is no way to detect whether a drag operation

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.