Listen to multiple keydowns

后端 未结 3 465
暗喜
暗喜 2020-12-03 12:42

I\'m trying to let a user move an element on the page using the arrow keys. So far, I have movement working for up/down/left/right, but not for diagonal (two arrow keys pres

3条回答
  •  暖寄归人
    2020-12-03 13:15

    Here is your code for you, slightly revised ... http://jsfiddle.net/w8uNz/ It just WORKS. Moves element across the parent element.

    Rob W is right, you should have a dynamic cache of keys being pressed down at a period of time. But for what ? if you are making a dynamic game - there must be much higher abstraction for that. And overall, you need to improve your level of coding if you want to do stuff like that. (Or it will just generate headaches for you.)

提交回复
热议问题