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 pressed simultaneously).
My listener looks like this:
addEventListener('keydown', function(e){
move = false;
x = false;
y = false;
var keycode;
if (window.event) keycode = window.event.keyCode;
else if (e) keycode = e.which;
switch(keycode){
case 37:
move = true;
x = 'negative';
//prevent page scroll
e.preventDefault()
break;
case 38:
move = true;
y = 'negative'
//prevent page scroll
e.preventDefault()
break;
case 39:
move = true;
x = 'positive'
//prevent page scroll
e.preventDefault()
break;
case 40:
move = true;
y = 'positive'
//prevent page scroll
e.preventDefault()
break;
}
if(move){
animation.move(x,y);
}
return false;
})
The idea was that if the user presses an arrow key, it sets x
and y
to either negative
or positive
, and fires off the move() function, which will move the element a preset number of pixels in the desired direction, and that if two keys were pressed, a second event would fire... I also hope to be able to have the user seemlessly change directions by releasing and pressing keys rapidly Neither of these are happening, however, if the user presses another direction key, they seem to need to wait a momment for movement to happen, unless they completely release the key and then press another one, and it won't respond to the second key at all until the first is released.
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.)
Fiddle: http://jsfiddle.net/ATUEx/
Create a temporary cache to remember your key strokes.
An implementation of handling two keys would follow this pattern:
<keydown>
If yes, and valid combination:
-
Delete all cached key codes-
Execute function for this combinationelse
-
Delete all cached key codes-
Store the new key code-
Set a time out to clear the keycodes (see below), with a reasonable delayA reasonable delay: Experiment to know which timeout is sufficient for you. When the delay is too short, the next initiated event will not find a previously entered key code.
When the delay is too long, the key strokes will stack when you don't want it.
Code
I have created an efficient function, keeping your code in mind. You should be able to implement it very easily.
At the end of the anonymous function, the
keydown
event listener is added. This event is fired only once (when the key is pressed down). When a second key is pressed fast enough, the code recognises two key strokes after each other, and callskeyCombi()
.I have designed
keyCombi
to be intelligent, and only callanimation.move(x,y)
when the values are changed. Also, I've implemented the possiblity to deal with two directions at a time.Note: I have contained the functions within an anonymous function wrapper, so that the variables are not defined in the global (
window
) scope. If you don't care about scoping, feel free to remove the first and last line.Logically this sounds rather simple: