Issue with _keyDwn when switching between arrow keys while playing a game
3 days 19 hours ago - 3 days 19 hours ago #287
by ludojoey
Hi!
Just a quick question:
I'm currently working on a very small game that uses the keyboard (left and right arrow keys).
Here's my problem:
I hold down the Right Arrow key, and _keyDwn correctly returns 59.
While still holding it down, I press and hold the Left Arrow key, and _keyDwn correctly returns 57.
Then I release the Left Arrow key.
I would expect _keyDwn to return 59, since I'm still holding down the Right Arrow key.
Instead, _keyDwn returns 0.
This is a bit of a problem when playing the game, as you can move quickly and sometimes press one key before releasing the other.
Similarly, and even more annoying, if I press the Right Arrow key and then release it very quickly while pressing the Left Arrow key, _keyDwn returns 0.
So perhaps I'm doing something wrong...
Any ideas?
Here's my code:
if (_keyDwn == 59) { // Right
playerRight();
}
if (_keyDwn == 57) { // Left
playerLeft();
}
Just a quick question:
I'm currently working on a very small game that uses the keyboard (left and right arrow keys).
Here's my problem:
I hold down the Right Arrow key, and _keyDwn correctly returns 59.
While still holding it down, I press and hold the Left Arrow key, and _keyDwn correctly returns 57.
Then I release the Left Arrow key.
I would expect _keyDwn to return 59, since I'm still holding down the Right Arrow key.
Instead, _keyDwn returns 0.
This is a bit of a problem when playing the game, as you can move quickly and sometimes press one key before releasing the other.
Similarly, and even more annoying, if I press the Right Arrow key and then release it very quickly while pressing the Left Arrow key, _keyDwn returns 0.
So perhaps I'm doing something wrong...
Any ideas?
Here's my code:
if (_keyDwn == 59) { // Right
playerRight();
}
if (_keyDwn == 57) { // Left
playerLeft();
}
Last edit: 3 days 19 hours ago by ludojoey.
Please Log in or Create an account to join the conversation.
2 days 23 hours ago - 2 days 23 hours ago #288
by Franck
Replied by Franck on topic Issue with _keyDwn when switching between arrow keys while playing a game
_keyDwn and _keyUp only store the last key press/release. I see there is an error in the manual description for the _keyDwn by the way. It says "current" but should read "last", like it is for the _keyUp description.
To keep track of several key states, you should use the getKeyDwn() and getKeyUp() functions. You can see how polyphony in the demo/organ is acheived as an example.
Edit: If you don't need simultaneous keypress info, you can simply read them and use them in each loop iteration, they are trigger by interrupts and buffered in a queue (so several can be registered even if you quickly hit keys inside your loop). See how moving in the zoom window of the Image Editor buffers the keypresses when you move faster than the display refresh. That makes it work in diagonals too because both presses are stores in the queue.
So basically:
Also you can use else for exclusives like left and right, but keep both ifs for non exclusives like left and up (diagonals).
To keep track of several key states, you should use the getKeyDwn() and getKeyUp() functions. You can see how polyphony in the demo/organ is acheived as an example.
Edit: If you don't need simultaneous keypress info, you can simply read them and use them in each loop iteration, they are trigger by interrupts and buffered in a queue (so several can be registered even if you quickly hit keys inside your loop). See how moving in the zoom window of the Image Editor buffers the keypresses when you move faster than the display refresh. That makes it work in diagonals too because both presses are stores in the queue.
So basically:
Code:
byte keyDwn=getKeyDwn();
byte keyUp=getKeyUp();
if (keyDwn == 59) playerRight();
else if (keyDwn == 57) playerLeft();
Also you can use else for exclusives like left and right, but keep both ifs for non exclusives like left and up (diagonals).
Last edit: 2 days 23 hours ago by Franck.
The following user(s) said Thank You: ludojoey
Please Log in or Create an account to join the conversation.
2 days 22 hours ago #289
by ludojoey
Replied by ludojoey on topic Issue with _keyDwn when switching between arrow keys while playing a game
Thanks.
However, since there is no key repeat, this doesn't work as is for a game.
I did manage to achieve what I wanted by keeping track of states such as goRight and goLeft, using _getKeyUp() and _keyDwn among others.
Anyway, it works in the end!
However, since there is no key repeat, this doesn't work as is for a game.
I did manage to achieve what I wanted by keeping track of states such as goRight and goLeft, using _getKeyUp() and _keyDwn among others.
Anyway, it works in the end!
The following user(s) said Thank You: Franck
Please Log in or Create an account to join the conversation.
Time to create page: 0.183 seconds