How Can I Catch 2+ Key Presses At Once?
Well lately i got interested in creating JS games. (not an area i have experience with but it interests me). i know there are several gaming engines for JS out there but i dont rea
Solution 1:
To directly answer your second question.
Here is one way:
var keyPressed = {};
$(window).keydown(function(e) {
keyPressed[e.which] = true;
}).keyup(function(e) {
keyPressed[e.which] = false;
});
Now you can use keyPressed
whenever you want to determine if a key is down:
// wherevervar key1 = 65, key2 = 66; // A and Bif (keyPressed[key1] && keyPressed[key2]) {
// A and B are both being pressed.
}
Solution 2:
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];
});
Post a Comment for "How Can I Catch 2+ Key Presses At Once?"