Debouncing Multiple-Keypress PRGM GetKey

From WikiPrizm
Revision as of 18:52, 10 February 2012 by Ashbad (talk | contribs) (Created page with 'The below code is not a single function, but rather a group of functions for fulfilling a single purpose: providing key debouncing and multi-keypress detection to the PRGM GetKey…')
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

The below code is not a single function, but rather a group of functions for fulfilling a single purpose: providing key debouncing and multi-keypress detection to the PRGM GetKey function. It's a bit more wordy to use, but it's also very flexible and can even be used to check for pseudo keydown/keyup key events. The needed function and variable declarations are as follows:

[code]const unsigned short* keyboard_register = (unsigned short*)0xA44B0000; unsigned short*lastkey = malloc(sizeof(unsigned short)*8); unsigned short*holdkey = malloc(sizeof(unsigned short)*8);

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)); 

}[/code]


These functions are used to check the current state of a key in the key array during the current loop runthrough, or check the value from the last loop. These use the KEY_PRGM_<key> key codes, and should be used in something like a game loop to provide clean debouncing. Here is an example of these functions at work in an example game loop:

[code]while(running) {

 keyupdate(); // update both key arrays
 ...
 // checking to see if the up key has been held down for a while
 if(keydownlast(KEY_PRGM_UP) && keydownhold(KEY_PRGM_UP))
 // checking to see if the up key has not been held down for a while
 if(!keydownlast(KEY_PRGM_UP) && !keydownhold(KEY_PRGM_UP))
 // checking to see if the up key has just been pressed
 if(keydownlast(KEY_PRGM_UP) && !keydownhold(KEY_PRGM_UP))
 // checking to see if the up key has just been released
 if(!keydownlast(KEY_PRGM_UP) && keydownhold(KEY_PRGM_UP))

}[/code]