How can I catch 2+ key presses at once?

Neta Meta

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 really want to create a game. rather i am curious on how things work / how can i create one.

I have several questions:

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

  2. I tried making a small game of something walking in a rectangular. By binding keyup to the window and checking the event.which to get the key that was pressed. I realized that if i clicked on 2 buttons same time only 1 of them is being registered. how can i overcome that?

    $(window).keyup(function(event){
         globalEvent = event.which;
    
    });
    
itdoesntwork

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:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to catch key presses in editable QTableWidgetItem?

From Dev

How can I emulate key presses on Vim startup?

From Dev

How can I store key presses into variables to refactor this code?

From Dev

How can I map repeated key presses to specific?

From Dev

How can I prevent key presses from changing the value of a scrollbar control?

From Dev

How can I print a string when the user presses the enter key whilst playing my guessing game?

From Dev

Boost Signals2: How do I register events such as mouse clicks and key presses?

From Dev

how to proxy key presses

From Dev

How to detect global key presses

From Dev

How can I play a sound when a user presses a button in a MessageBox?

From Dev

How can I iterate each time the user presses a button?

From Dev

How can I catch errors and exceptions in Symfony2/Silex?

From Dev

How can I make my trigger button show once I hit the escape key on a dialog box?

From Dev

How we can stop mouse clicks and key presses from being transfered to loaded document in WebBrowser?

From Dev

How to get realtime key presses in Assembly?

From Dev

How to detect 3 key presses in SWT KeyListener

From Dev

How to check for variable key presses in pygame

From Dev

How do determine key presses in C++

From Dev

How do I guarantee that a Java Swing JDialog will maintain focus and register all key presses in game?

From Dev

How do I make an applescript that presses the delete key using command down

From Dev

How can I "protect" my public key in my .NET code once deployed?

From Dev

How can I validate user input once they click tab key or click on another field?

From Dev

How can I delete a Firebase document when user presses the Power button

From Dev

How can I wire up a button on my page so that it is clicked when a user presses enter?

From Dev

How can i query a Primary key objects in django 2 template

From Dev

How can I replace legend key in ggplot2?

From Java

How can I catch specific exceptions?

From Dev

How can I catch the gestures over UICollectionView?

From Dev

HttpHostConnectException - How can I catch it right?

Related Related

  1. 1

    How to catch key presses in editable QTableWidgetItem?

  2. 2

    How can I emulate key presses on Vim startup?

  3. 3

    How can I store key presses into variables to refactor this code?

  4. 4

    How can I map repeated key presses to specific?

  5. 5

    How can I prevent key presses from changing the value of a scrollbar control?

  6. 6

    How can I print a string when the user presses the enter key whilst playing my guessing game?

  7. 7

    Boost Signals2: How do I register events such as mouse clicks and key presses?

  8. 8

    how to proxy key presses

  9. 9

    How to detect global key presses

  10. 10

    How can I play a sound when a user presses a button in a MessageBox?

  11. 11

    How can I iterate each time the user presses a button?

  12. 12

    How can I catch errors and exceptions in Symfony2/Silex?

  13. 13

    How can I make my trigger button show once I hit the escape key on a dialog box?

  14. 14

    How we can stop mouse clicks and key presses from being transfered to loaded document in WebBrowser?

  15. 15

    How to get realtime key presses in Assembly?

  16. 16

    How to detect 3 key presses in SWT KeyListener

  17. 17

    How to check for variable key presses in pygame

  18. 18

    How do determine key presses in C++

  19. 19

    How do I guarantee that a Java Swing JDialog will maintain focus and register all key presses in game?

  20. 20

    How do I make an applescript that presses the delete key using command down

  21. 21

    How can I "protect" my public key in my .NET code once deployed?

  22. 22

    How can I validate user input once they click tab key or click on another field?

  23. 23

    How can I delete a Firebase document when user presses the Power button

  24. 24

    How can I wire up a button on my page so that it is clicked when a user presses enter?

  25. 25

    How can i query a Primary key objects in django 2 template

  26. 26

    How can I replace legend key in ggplot2?

  27. 27

    How can I catch specific exceptions?

  28. 28

    How can I catch the gestures over UICollectionView?

  29. 29

    HttpHostConnectException - How can I catch it right?

HotTag

Archive