Re: if(keyUp[KEY_3] == 1) ??
Well here is one I wrote a while ago. I edited it a bit to make it more convenient (just a function call). It uses a kind of cheap way of locking the function so it can be only called once per frame (it requires the GetKeys function to be called every frame). It was considerably better now, but it still has room for improvement. Keys that have been released have a value of -1.
- Code: Select all
char*
GetKeys ()
{
const int key_reg[] = {KEY_RIGHT, KEY_LEFT, -1}; // must end with -1
// list of keys that can have keyup states
// that way it doesn't need to loop through every key
// because rarely do people want to catch gp2x keys
// static variables are only initialized once
static int protect = 0; // used to lock the function (only once per frame)
static char key_prev[500] = {0}; // or however long GetKeyState is
static char* key; // function remembers last key state
if(frame%2==protect) // only update once per frame
{
int i;
key=GetKeyState();
for(i=0; key_reg[i]!=-1; i++) // loop through registered keys
{
if(key_prev[key_reg[i]]==1 && key[key_reg[i]]==0)
{ // if the key was pressed, but isn't anymore
key_prev[key_reg[i]]=key[key_reg[i]]; // set key previous
key[key_reg[i]]=-1; // set to keyup
}
else
{
key_prev[key_reg[i]]=key[key_reg[i]]; // set key previous
}
}
protect=protect==0; // lock until next frame
}
return key;
}