Slowing down the key events
Hello All,
I am working on learning OpenGL and I am coding a classic style RPG, Similar to the old NES ones. I am having a problem with using the arror keys. I would like the arrow keys to move my background by one tile per keystroak, but I also want to be able to hold the key down and have it move smoothly. Currently the keys move the background way too fast. Is there a way to force it to wait a set time before repeating the key event when the key is held down?
Any help is greatly appriciated.
SweetJ
PS. my key code is below
void Keyboard_Input() // Keyboard input
{
if(GetKeyState(VK_UP) & 0x800)
{
MapY -= .25;
}
if(GetKeyState(VK_DOWN) & 0x800)
{
MapY += .25;
}
if(GetKeyState(VK_LEFT) & 0x800)
{
MapX += .25;
}
if(GetKeyState(VK_RIGHT) & 0x800)
{
MapX -= .25;
}
}
The events always happen when the happen, so you have to change the way your program responds to the events. What you do is- keep excepting the events but when you have the player move, start a timer going for the amount of time it should take to move one space. When you get any keypress events, have your program check the timer. If the timer is still counting up, the program does nothing. If the timer is already beyond the target time, then allow the player to move again and restart your timer.
Thank you for such a fast reply.
I will see if I can figure out how to make a timer.
Thanks again,
SweetJ
I will see if I can figure out how to make a timer.
Thanks again,
SweetJ
what you need is some way of mintoring the state changes
rather than the actual state
ie. only move the map up/left/right/down if the LAST keyboard
state was DOWN and the CURRENT state is up (ie. Key was pressed and then released)
hope you see what i mean, a sample implementation (off the top
of my head, so it might not work :)
definitions:
implementation:
rather than the actual state
ie. only move the map up/left/right/down if the LAST keyboard
state was DOWN and the CURRENT state is up (ie. Key was pressed and then released)
hope you see what i mean, a sample implementation (off the top
of my head, so it might not work :)
definitions:
enum { KEY_UP = 0, KEY_DOWN, KEY_LEFT, KEY_RIGHT,};struct keyState{ int lastState[4]; // can only be set to 1 or 0 int currState[4]; // can only be set to 1 or 0} keyboard;
implementation:
void keyboardPreStateChange(){ int i=0; keyState *kb = keyboard[keyId]; // move currState to lastState for(i=0; i<4; i++) { kb->lastState = kb->currState; kb->currState = 0; }}int keyboardKeyUp( int keyId ){ keyState *kb = keyboard[keyId]; if( kb->lastState[keyId] && !kb->currState[keyId] ) return 1; else { return 0; }}void keyboardUpdate() { #define UPDATE_STATE(x) if(GetKeyState(VK_##x)&0x800) keyboard.currState[KEY_##x] = 1; keyboardPreStateChange(); UPDATE_STATE( UP ); UPDATE_STATE( LEFT ); UPDATE_STATE( RIGHT ); UPDATE_STATE( DOWN );}void keyboardInput() // Keyboard input{ keyboardUpdate(); if( keyboardKeyUp( KEY_UP ) ) { // do something } // etc etc}
If I use the change of state method, wont that force the user to press and release the key for each step. If that is true it takes away the ability to hold the key down and move more then once. I think that would be really annoying since the player will be walking around most of the game.
I think the timer would still work better for what I need. Perhaps I am missing something.
Thanks,
SweetJ
I think the timer would still work better for what I need. Perhaps I am missing something.
Thanks,
SweetJ
(edit: refering to silvermace's previous post)
Actually yes, the way that silvermace describes is better. If you use the timer, it may create annoying instances in which the program misses some input.
[Edited by - Kelly G on August 20, 2004 12:36:18 AM]
Actually yes, the way that silvermace describes is better. If you use the timer, it may create annoying instances in which the program misses some input.
[Edited by - Kelly G on August 20, 2004 12:36:18 AM]
the beauty of the state change system, is that its flexible
if you want to test that the user is holding down the key, its
simple:
also, to make it 'wait' just have a counter that tracks how
long the key has been down, once the counter reaches X value,
continuously move the player and reset the counter when the key
is released
if you want to test that the user is holding down the key, its
simple:
if( keyboard.currState[key] && keyboard.lastState[key] ) movePlayer();
also, to make it 'wait' just have a counter that tracks how
long the key has been down, once the counter reaches X value,
continuously move the player and reset the counter when the key
is released
Is there a way to still have the player move when they hold the key down? because otherwise they have to press and release the key for each step. If you needed to move to the other side of the map that would be difficult.
Thanks,
SweetJ
Thanks,
SweetJ
Quote:Original post by Kelly G
Actually yes, the way that silvermace describes is better. If you use the timer, it may create annoying instances in which the program misses some input.
I don't know what kind of timer, but if you were to use sleep, it seems to keep the keypresses in a buffer (at least it did when i was using conio.h to get the keypresses, cause i needed to wait 8 seconds between actions, and when you pressed the key while it was sleeping it would detect the keypress as soon as it returned.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement