Check if a key is pressed?

Is there a way to determine if a key is currently in JavaScript?

I know about the "keydown" event, but that is not what I need. Some time AFTER a key is pressed, I want to determine if it is still pressed.

R. S. The biggest problem is that after a period of time the key begins to repeat, discarding key events and key events such as the devil. I hope that there is a simple function isKeyDown (key), but if not, then this problem will need to be overcome / circumvented.

+60
javascript input keyboard
Dec 01 '09 at 20:17
source share
9 answers

Is there a way to determine if a key is currently in JavaScript?

Nope. The only way is to keep track of each keyup and keydown and remember.

after a period of time, the key begins to repeat, causing the keys to turn off and key events, such as the devil.

Do not do it. You will definitely get a keypress repetition, and in many browsers you will also get a duplicate keydown , but if keyup repeats, this is a mistake.

Unfortunately, this is not a completely unheard-of error: in Linux, Chromium and Firefox (when it runs under GTK +, which is found in popular distributions such as Ubuntu) it generates duplicate sequences of keyup-keypress-keydown for which it is impossible to distinguish from one that is fast clogs the key.

+48
Dec 01 '09 at 20:47
source share

In addition to the keyup and keydown to track when a key is reset and backed up, there are actually some properties that tell you if some keys are omitted.

 window.onmousemove = function (e) { if (!e) e = window.event; if (e.shiftKey) {/*shift is down*/} if (e.altKey) {/*alt is down*/} if (e.ctrlKey) {/*ctrl is down*/} if (e.metaKey) {/*cmd is down*/} } 

This is available for all event objects created by the browser, such as keydown , keyup and keypress , so you do not need to use mousemove.

I tried to create my own event objects with document.createEvent('KeyboardEvent') and document.createEvent('KeyboardEvent') and search for e.shiftKey etc., but I had no luck.

I am using Chrome 17 on Mac

+84
Jan 16 2018-12-12T00:
source share

My decision:

 var keys = []; window.onkeyup = function(e) {keys[e.keyCode]=false;} window.onkeydown = function(e) {keys[e.keyCode]=true;} 

Now I can check if any key is pressed anywhere in the script by checking

 keys["code of the key"] 

If true, the key is pressed.

+16
Jan 26 '16 at
source share

I do not believe that there is something like the isKeyDown function, but you can write your own.

Basically, create an array whose length is the number of keys that you want to control. Then, using the keyUp and keyDown documents / pages / controls, update the array with this key state.

Then write a function that checks if a particular key is disabled and returns bool.

 var keyEnum = { W_Key:0, A_Key:1, S_Key:2, D_Key:3 }; var keyArray = new Array(4); function onKeyDown() { // Detect which key was pressed if( key == 'w' ) keyArray[keyEnum.W_Key] = true; // Repeat for each key you care about... } function onKeyUp() { // Detect which key was released if( key == 'w' ) keyArray[keyEnum.W_Key] = false; // Repeat for each key you care about... } function isKeyDown(key) { return keyArray[key]; } 

This should do what you want.

+10
Dec 01 '09 at 20:27
source share

Other people have asked this question before (although I do not see any obvious fraud here).

I think the answer is that the keydown event (and its twin keyup ) is all the information you receive. The repetition is rather firmly connected to the operating system, and the application program does not get much opportunity to request the BIOS for the actual state of the key.

What you can do, and may have to, if you need it to work, is a key waiver. Essentially, you can evaluate keydown and keyup , but ignore the keyup event if it happens too quickly after the last keydown ... or essentially you should defer your response to keyup long enough to make sure there is no other keydown event. following something like a 0.25 second keyup .

This involves using timer activity and recording millisecond times for previous events. I canโ€™t say that this is a very attractive solution, but ...

+1
Dec 01 '09 at 20:24
source share
 /* Tracks what keys are currently down on the keyboard */ function keyboard_module(onUpdate){ var kb = {}; var unicode_mapping = {}; document.onkeydown = function(e){ var unicode=e.charCode? e.charCode : e.keyCode var key = getKey(unicode); kb[key] = true; if(onUpdate){ onUpdate(kb); } } document.onkeyup = function(e){ var unicode=e.charCode? e.charCode : e.keyCode var key = getKey(unicode); delete kb[key]; if(onUpdate){ onUpdate(kb); } } function getKey(unicode){ if(unicode_mapping[unicode]){ var key = unicode_mapping[unicode]; }else{ var key= unicode_mapping[unicode] = String.fromCharCode(unicode); } return key; } return kb; } function testing(kb){ console.log('These are the down keys', kb); } var keyboard = keyboard_module(testing); .... //somewhere else in the code if(keyboard['K']){/*do something special */} 
+1
Mar 11
source share

The following code is what I use:

 var altKeyDownCount = 0; window.onkeydown = function (e) { if (!e) e = window.event; if (e.altKey) { altKeyDownCount++; if (30 < altKeyDownCount) { $('.key').removeClass('hidden'); altKeyDownCount = 0; } return false; } } window.onkeyup = function (e) { if (!e) e = window.event; altKeyDownCount = 0; $('.key').addClass('hidden'); } 

When the user holds the Alt key for some time (about 2 seconds), a group of labels (class = 'hidden) appears. When the Alt key is released, the labels disappear. Used jQuery and Bootstrap.

+1
Aug 25 '15 at 5:31
source share

Take a look at this answer and use onkeyup and onkeydown . Here is more specific information about these events.

0
Dec 01 '09 at 20:21
source share
 $('#mytextbox').keydown(function (e) { if (e.keyCode == 13) { if (e.altKey) { alert("alt is pressed"); } } }); 

if you press alt + enter you will see a warning.

-one
Jul 24 '11 at 17:28
source share



All Articles