Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
140
rated 0 times [  147] [ 7]  / answers: 1 / hits: 64041  / 14 Years ago, thu, february 10, 2011, 12:00:00

Is there a way for jQuery to detect that more than one key was pressed at the same time?



Is there any alternative that allows for pressing two keys at the same time to be detected?


More From » jquery

 Answers
36

In order to detect multiple keys being held down, use the keydown and keyup events.



var keys = {};

$(document).keydown(function (e) {
keys[e.which] = true;
});

$(document).keyup(function (e) {
delete keys[e.which];
});


I've put together a demo here: http://jsfiddle.net/gFcuU/. It's kind of fun, though I noticed my keyboard is only able to detect at most 6 keys.


[#93801] Tuesday, February 8, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jameson

Total Points: 534
Total Questions: 103
Total Answers: 102

Location: Lithuania
Member since Fri, Sep 4, 2020
4 Years ago
;