I had a number of problems using dbKeyState to take the user's keybord input for menus and selecting in general. The problems I had were these:
1. When the user pressed down or up, the menus would scroll WAY too fast.
2. I fixed the fast scrolling by adding dbWait and/or dbSleep in my main loop and/or after called the key functions. The problem now was that if the user pressed the key while the wait+sleep functions were in progress, the keys were ignored. Also, my animations were slowed down by this method.
3. I tried using the dbTimer command to find the amount of time since the last key press. If the time was less than 200ms or so, the key functions were ignored, thereby stopping the fast scrolling. This didn't work because quickly pressed keys were ignored, it made scrolling feel unnatural, and it made my code messy.
I found what seems to be the perfect solution to these problems. I've been helped a few times on this forum and I'd like to give back by posting an example of my code that can be copied and pasted:
keys.h
#pragma once
bool keydownup1=false;
//This makes sure the key was pressed and let up before the f() is true
bool key1( void ){
if (!dbKeyState(2)){
if (keydownup1){
keydownup1=false;
return true;
}
}
if (dbKeyState(2)){
keydownup1=true;
return false;
}
else {return false;
}
}
That will work for key 1. If anyone can improve it, let us know!
I make the same kind of games as any Chemisty major would.