views:

11244

answers:

7

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  A: 

Well, you can't check if it's down after the event, but you can check if it's Up... If it's up.. it means that no longer is down :P lol

So the user presses the button down (onMouseDown event) ... and after that, you check if is up (onMouseUp). While it's not up, you can do what you need.

rogeriopvl
Isn't onMouseUp an event too? How will you check the "property" onMouseup? Or whose property, rather?
Rohit
@Rohit: you don't check property, you manage a flag indicating the event has happened... The mouse button is either up or down.
PhiLho
+2  A: 

The following snippet will attempt to execute the "doStuff" function 2 seconds after the mouseDown event occurs in document.body. If the user lifts up the button, the mouseUp event will occur and cancel the delayed execution.

I'd advise using some method for cross-browser event attachment - setting the mousedown and mouseup properties explicitly was done to simplify the example.

function doStuff() {
  // does something when mouse is down in body for longer than 2 seconds
}

var mousedownTimeout;

document.body.onmousedown = function() { 
  mousedownTimeout = window.setTimeout(doStuff, 2000);
}

document.body.onmouseup = function() {
  window.clearTimeout(mousedownTimeout);
}
Thanks, but this isn't quite the behavior I was looking for (although I can see how my question indicates that this is what I am trying to do).
TM
+5  A: 

I think the best approach to this is to keep your own record of the mouse button state, as follows:

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

and then, later in your code:

if (mouseDown == 1) {
    // the mouse is down, do what you have to do.
}
paxdiablo
+1, and thanks. I thought I might have to resort to this, but I was hoping there was some feature that let you simply check the state directly.
TM
A: 

You can combine @Pax and my answers to also get the duration that the mouse has been down for:

var mousedownTimeout,
    mousedown = 0;

document.body.onmousedown = function() {
  mousedown = 0; 
  window.clearInterval(mousedownTimeout);
  mousedownTimeout = window.setInterval(function() { mousedown += 200 }, 200);
}

document.body.onmouseup = function() {
  mousedown = 0;
  window.clearInterval(mousedownTimeout);
}

Then later:

if (mousedown >= 2000) {
  // do something if the mousebutton has been down for at least 2 seconds
}
That's not a bad idea, Jake. Do you need to clearInterval() in onmouseup() before setting mousedown to 0? I'm wary of the timer function firing between setting it to 0 and stopping the timer, leaving the timeout at 200? Similarly in onmousedown.
paxdiablo
The order of the clearIntervals isn't going to make a difference as the current thread of execution will finish before any events (including timers) fire.
Nathaniel Reinhart
+10  A: 
Eugene Lazutkin
Great post, thanks! The last bit about catching the button onmousemove is perfect, since I only need this info to resolve a problem that happens in IE only (certain scriptaculous callbacks fire while dragging, when they should only fire when dragging stops, and I needed a way to stop them)
TM
After testing, it appears that evt.button does in fact not exist onmousemove with IE7...
TM
It always pays to qualify your question as precisely as possible. I just added the last bit as the fun fact. Now I am glad I did it because it turned out to be useful for you.
Eugene Lazutkin
Hmm, the documentation says it is supported from IE4 to IE8.
Eugene Lazutkin
My bad, it is a copy-n-paste error. Let me correct my code.
Eugene Lazutkin
document.body.someevent doesn't work in IE7. With the removal of the "event" parameter, event.button does work correctly. Sadly, this doesn't quite work for my workaround, since on at least one of the many firings of "onmousemove", it slips through as a zero. Thanks for the information anyway.
TM
A: 

You need to handle the MouseDown and MouseUp and set some flag or something to track it "later down the road"... :(

Thomas Hansen
A: 

This is a stupid limitation of Javascript. Why is it that unlike Java the brilliant geniuses at W3C can't shoot straight and give us an API that doesn't force us to keep reinventing the wheel?