1

Is it possible to count how many keys are pressed at the same time? I've come quite far but I get some unwanted behavior when tabbing away from my browser window. If I hold down x number of keys when tabbing away from the browser window and then release the keys in a different window the numKeys array stays the same when I tab back to the browser window. Maybe you can check how many keys are pressed when returning to the browser window or something?

My code so far:

var numKeys = []; $(document).keydown(function(e){ if(numKeys.indexOf(e.keyCode) == -1){ numKeys.push(e.keyCode); } document.getElementById('test').innerHTML = numKeys.length; }); $(document).keyup(function(e){ if(numKeys.indexOf(e.keyCode) > -1){ var index = numKeys.indexOf(e.keyCode); if(index > -1){ numKeys.splice(index, 1); } } document.getElementById('test').innerHTML = numKeys.length; }); 
5
  • You could just reset all the keys if the user tabbed out like in the code here. There isn't really a way to know what the user does in another window Commented Jul 24, 2014 at 10:15
  • I'm grateful to SO for bringing all these crazy ideas ;) Check this out jsbin.com/xemegudu/1/edit - 'cause I'm not sure about your goal, and I'm finding so hard to press several keys at (exactly) the same time. Commented Jul 24, 2014 at 10:29
  • 2
    ... and I expect +1 from you guys at least because I managed to lock up my keyboard trying to make your crazy ideas came true ;)) Commented Jul 24, 2014 at 10:51
  • @SomeGuy your answer helped me solve my issue. If you put it up as an answer I'll accept it. Commented Jul 24, 2014 at 12:03
  • @DanielTovesson Added it as an answer Commented Jul 24, 2014 at 12:08

4 Answers 4

3

try this (UPDATE):

var cpt = 0; var codes = ""; $(function () { $(document).keydown(function (e) { if (codes.indexOf(";" + e.keyCode + ";") == -1) { cpt++; codes += ";" + e.keyCode + ";"; } }); $(document).keyup(function (e) { var tmp = ";" + e.keyCode + ";"; if (codes.indexOf(tmp) != -1) { cpt--; var part1 = codes.substring(0,codes.indexOf(tmp)); var part2 = codes.substring(codes.indexOf(tmp)+tmp.length ); codes=part1+part2; } }); }); 

The new Working ;) link : FIDDLE

Sign up to request clarification or add additional context in comments.

4 Comments

But it just tells how many have been pressed, not the current number. If I press A, S and D and release D, it would still show 3.
Your jsFiddle isn't related to your snippet :)
My keyboard is beeping like crazy; +1 even I can't test it with more than four keys (just as I couldn't using my snippet)
normal keyboards dosn't support holding more than 3 or 4 keys if you have to hold down more consider to buy an gamer keyboard
2

A simple solution to your problem would be to clear the keys pressed when the user leaves the tab. You can use the window.onblur event like this:

window.onblur = function () { numKeys = []; }; 

Demo.

Note that this solution will not be aware of keys you press in another window. (For example, you press "up" in another window, and then select this window again)

Comments

0

Let's keep it very simple. Idea is to set the keys with the object being event.keyCode to true on keydown, and delete on keyup. Then count the instances of true in keys,

var keys = []; $(function () { $(document).keydown(function (event) { keys[event.keyCode] = true; var count = 0; for (var i = 0; i < keys.length; i++) { if (keys[i]) count++; } console.log(count); }); $(document).keyup(function (e) { delete keys[e.keyCode]; }); }); 

Comments

0

I decided to share my snippet mentioned and linked just a few minutes before in the comments above. But, just to make it clear, I'm finding hard to press / accept by the keyboard more than four, maybe five keys at the same time and that's why all these attempts might be useless :)

window.onload = function(){ var a = []; var log = document.getElementById("keys"); document.body.onkeydown = function(event){ var keyCode = ('which' in event) ? event.which : event.keyCode; if(a.indexOf(keyCode) == -1) a.push(keyCode); log.innerHTML = a.length + " | " + a.join(); return false; }; document.body.onkeyup = function(event){ a = []; log.innerHTML = a.join(); }; }; 

Working jsBin

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.