0

I would like to make a game in html and javascript that uses the arrow keys to move. I have already tried

window.onkeydown = function(e) { if (e.which == 37) { doleftarrowkeystuff(); } if (e.which == 39) { dorightarrowkeystuff(); } }; 

but that stops one key when the other is pressed.

Any help would be appreciated.

2
  • 1
    possible duplicate stackoverflow.com/questions/5203407/… Commented Nov 13, 2014 at 23:02
  • I meant up and left arrow, but you get the point. Any two keys and I could probably figure it out. Commented Nov 13, 2014 at 23:03

2 Answers 2

1

exist onkeyup as well, you can check every keypressdown and store it, but when the onkeyup is triggered check if it is the same key in order to remove from store.

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

1 Comment

Sorry, I was in the middle of posting an answer and didn't see yours, so this is the accepted answer.
1

Actually, I figured it out. I can just look for the onkeyup event. For example, I can use

var right = 0; var left = 0; window.onkeydown = function(e) { if (e.which == 37) { left = 1; } if (e.which == 39) { right = 1; } }; window.onkeyup = function(e) { if (e.which == 37) { left == 0; } if (e.which == 39) { right == 0; } }; 

(please don't tell me if I mix left and right up, I'll figure it out)

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.