Observing keydown events in JavaScript when two keys are pressed simultaneously -
i'm developing browser game using javascript player can move avatar in 4 directions.
in order register simultaneous key presses observe keydown
, keyup
events , keep track of keys pressed , released. way can move avatar in diagonal direction if example cursor up , cursor right pressed @ same time.
it's same principle reply here on stackoverflow in javascript, how tell if user pressing 2 keys @ same time? demonstration http://jsbin.com/iloxi.
however there's flaw in solution can reproduced in demo. example press cursor down, start pressing cursor right. box moves bottom right. release cursor right while still pressing cursor down. box stops moving. should happen box keeps moving down.
the problem javascript stops sending cursor down keydown
events cursor right pressed. once cursor right released there's no way of knowing cursor down still being pressed.
is there solution problem?
update: problem demo box moved either in keydown
or keyup
event listeners. once second key released no more keydown
events fired first key. i'm wondering if there's solution or if standard javascript behavior?
to fix demo code have call move()
function in timeout iteration (window.settimeout()
).
maybe i'm misunderstanding you, shouldn't wait keyup
event on cursor down? i.e. cursor down being pressed until keyup
- shouldn't relying on repeated keydown
events being sent.
Comments
Post a Comment