Is there a way to detect if a key is currently down in JavaScript?
I know about the "keydown" event, but that's not what I need. Some time AFTER the key is pressed, I want to be able to detect if it is still pressed down.
P. S. The biggest issue seems to be that after some period of time the key begins to repeat, firing off keydown and keyup events like a fiend. Hopefully there is just a simple isKeyDown(key) function, but if not then this issue will need to be overcome / worked around.
Nope. The only possibility is monitoring each
keyup
andkeydown
and remembering.It shouldn't. You'll definitely get
keypress
repeating, and in many browsers you'll also get repeatedkeydown
, but ifkeyup
repeats, it's a bug.Unfortunately it is not a completely unheard-of bug: on Linux, Chromium, and Firefox (when it is being run under GTK+, which it is in popular distros such as Ubuntu) both generate repeating keyup-keypress-keydown sequences for held keys, which are impossible to distinguish from someone hammering the key really fast.
I know this is very old question, however there is a very lightweight (~.5Kb) JavaScript library that effectively "patches" the inconsistent firing of keyboard event handlers when using the DOM API.
The library is Keydrown.
Here's the operative code sample that has worked well for my purposes by just changing the key on which to set the listener:
I've incorporated Keydrown into my client-side JavaScript for a proper pause animation in a Red Light Green Light game I'm writing. You can view the entire game here. (Note: If you're reading this in the future, the game should be code complete and playable :-D!)
I hope this helps.
In addition to using
keyup
andkeydown
listeners to track when is key goes down and back up, there are actually some properties that tell you if certain keys are down.This are available on all browser generated event objects, such as those from
keydown
,keyup
, andkeypress
, so you don't have to use mousemove.I tried generating my own event objects with
document.createEvent('KeyboardEvent')
anddocument.createEvent('KeyboardEvent')
and looking fore.shiftKey
and such, but I had no luck.I'm using Chrome 17 on Mac
Other people have asked this kind of question before (though I don't see any obvious dupes here right now).
I think the answer is that the
keydown
event (and its twinkeyup
) are all the info you get. Repeating is wired pretty firmly into the operating system, and an application program doesn't get much of an opportunity to query the BIOS for the actual state of the key.What you can do, and perhaps have to if you need to get this working, is to programmatically de-bounce the key. Essentially, you can evaluate
keydown
andkeyup
yourself but ignore akeyup
event if it takes place too quickly after the lastkeydown
... or essentially, you should delay your response tokeyup
long enough to be sure there's not anotherkeydown
event following with something like 0.25 seconds of thekeyup
.This would involve using a timer activity, and recording the millisecond times for previous events. I can't say it's a very appealing solution, but...
I don't believe there is anything like an isKeyDown function, but you could write your own.
Basically, create an array whose length is the number of keys you want to monitor. Then using the documents/pages/controls keyUp and keyDown events, update the array with that key's state.
Then write a function that checks if a certain key is down and returns a bool.
That should accomplish what you want.
Ended up here to check if there was something builtin to the browser already, but it seems there isn't. This is my solution (very similar to Robert's answer):
You can then check if a key is pressed with
is_key_down('ArrowLeft')
.