How can I immediately press 2 keystrokes?

Recently, I have become interested in creating JS games. (not an area in which I have experience, but it interests me).

I know that there are several game engines for JS, but I really do not want to create a game. rather, I wonder how everything works / how I can create it.

I have a few questions:

  • Anyone with suggestions on where I can read about this? Prerequisite (what knowledge is needed).

  • I tried to make a little game of something going into a rectangle. By linking the key to the window and checking event.whichto get the key pressed. I realized that if I pressed 2 buttons, only one of them would be registered. how can i overcome this?

    $(window).keyup(function(event){
         globalEvent = event.which;
    
    });
    
+4
2

.

:

var keyPressed = {};

$(window).keydown(function(e) {
    keyPressed[e.which] = true;
}).keyup(function(e) {
    keyPressed[e.which] = false;
});

keyPressed, , :

// wherever
var key1 = 65, key2 = 66; // A and B
if (keyPressed[key1] && keyPressed[key2]) {
    // A and B are both being pressed.
}
+5

, .

var keys = {};

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

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

All Articles