I actually use this setup:
Code:
As you can see, it's a lot more code, and more code to see if values are met. But it's by far the fastest routine (doesn't even go through the OS at all), allows for multiple keypresses to be checked at a time, allows for detection of psuedo-ONKEYUP/ONKEYDOWN events, etc.
EDIT: based on PierottLL's original routine, I added the pseudo-event checking and some other things.
Code:
static const unsigned short* keyboard_register = (unsigned short*)0xA44B0000;
int main(void) {
...
unsigned short*lastkey = malloc(sizeof(unsigned short)*8);
unsigned short*holdkey = malloc(sizeof(unsigned short)*8);
int llastkey = KEY_PRGM_NONE;
int lholdkey = KEY_PRGM_NONE;
void keyupdate(void) {
memcpy(holdkey, lastkey, sizeof(unsigned short)*8);
lholdkey = llastkey;
memcpy(lastkey, keyboard_register, sizeof(unsigned short)*8);
llastkey = PRGM_GetKey();
}
int keydownlast(int basic_keycode) {
int row, col, word, bit;
row = basic_keycode%10;
col = basic_keycode/10-1;
word = row>>1;
bit = col + 8*(row&1);
return (0 != (lastkey[word] & 1<<bit));
}
int keydownhold(int basic_keycode) {
int row, col, word, bit;
row = basic_keycode%10;
col = basic_keycode/10-1;
word = row>>1;
bit = col + 8*(row&1);
return (0 != (holdkey[word] & 1<<bit));
}
void keymenu(void) {
int key = KEY_PRGM_MENU;
GetKey(&key);
Bdisp_EnableColor(1);
DrawFrame(COLOR_BLACK);
}
...
_Bool running = true;
while(running) {
keyupdate();
...
// check if shift key is held:
if(keydownlast(KEY_PRGM_SHIFT)) { ...}
/// check if shift key was just pressed:
if(keydownlast(KEY_PRGM_SHIFT) && !keydownhold(KEY_PRGM_HOLD)) { ...}
// check if shift key was just released:
if(!keydownlast(KEY_PRGM_SHIFT) && keydownhold(KEY_PRGM_HOLD)) { ...}
...
}
As you can see, it's a lot more code, and more code to see if values are met. But it's by far the fastest routine (doesn't even go through the OS at all), allows for multiple keypresses to be checked at a time, allows for detection of psuedo-ONKEYUP/ONKEYDOWN events, etc.
EDIT: based on PierottLL's original routine, I added the pseudo-event checking and some other things.