Difference between revisions of "Debouncing Multiple-Keypress PRGM GetKey"

From WikiPrizm
Jump to navigationJump to search
(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…')
 
Line 1: Line 1:
 
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:
 
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;
+
  const unsigned short* keyboard_register = (unsigned short*)0xA44B0000;
unsigned short*lastkey = malloc(sizeof(unsigned short)*8);
+
  unsigned short*lastkey = malloc(sizeof(unsigned short)*8);
unsigned short*holdkey = malloc(sizeof(unsigned short)*8);
+
  unsigned short*holdkey = malloc(sizeof(unsigned short)*8);
 
+
 
void keyupdate(void) {
+
  void keyupdate(void) {
  memcpy(holdkey, lastkey, sizeof(unsigned short)*8);
+
    memcpy(holdkey, lastkey, sizeof(unsigned short)*8);
  lholdkey = llastkey;
+
    lholdkey = llastkey;
  memcpy(lastkey, keyboard_register, sizeof(unsigned short)*8);
+
    memcpy(lastkey, keyboard_register, sizeof(unsigned short)*8);
  llastkey = PRGM_GetKey();
+
    llastkey = PRGM_GetKey();
}
+
  }
int keydownlast(int basic_keycode) {
+
  int keydownlast(int basic_keycode) {
  int row, col, word, bit;  
+
    int row, col, word, bit;  
  row = basic_keycode%10;  
+
    row = basic_keycode%10;  
  col = basic_keycode/10-1;  
+
    col = basic_keycode/10-1;  
  word = row>>1;  
+
    word = row>>1;  
  bit = col + 8*(row&1);  
+
    bit = col + 8*(row&1);  
  return (0 != (lastkey[word] & 1<<bit));  
+
    return (0 != (lastkey[word] & 1<<bit));  
}
+
  }
int keydownhold(int basic_keycode) {
+
  int keydownhold(int basic_keycode) {
  int row, col, word, bit;  
+
    int row, col, word, bit;  
  row = basic_keycode%10;  
+
    row = basic_keycode%10;  
  col = basic_keycode/10-1;  
+
    col = basic_keycode/10-1;  
  word = row>>1;  
+
    word = row>>1;  
  bit = col + 8*(row&1);  
+
    bit = col + 8*(row&1);  
  return (0 != (holdkey[word] & 1<<bit));  
+
    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:
 
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) {
+
[code] while(running) {
  keyupdate(); // update both key arrays
+
    keyupdate(); // update both key arrays
 
+
 
  ...
+
    ...
 
+
 
  // checking to see if the up key has been held down for a while
+
    // checking to see if the up key has been held down for a while
  if(keydownlast(KEY_PRGM_UP) && keydownhold(KEY_PRGM_UP))
+
    if(keydownlast(KEY_PRGM_UP) && keydownhold(KEY_PRGM_UP))
 
+
 
  // checking to see if the up key has not been held down for a while
+
    // checking to see if the up key has not been held down for a while
  if(!keydownlast(KEY_PRGM_UP) && !keydownhold(KEY_PRGM_UP))
+
    if(!keydownlast(KEY_PRGM_UP) && !keydownhold(KEY_PRGM_UP))
 
+
 
  // checking to see if the up key has just been pressed
+
    // checking to see if the up key has just been pressed
  if(keydownlast(KEY_PRGM_UP) && !keydownhold(KEY_PRGM_UP))
+
    if(keydownlast(KEY_PRGM_UP) && !keydownhold(KEY_PRGM_UP))
 
+
 
  // checking to see if the up key has just been released
+
    // checking to see if the up key has just been released
  if(!keydownlast(KEY_PRGM_UP) && keydownhold(KEY_PRGM_UP))
+
    if(!keydownlast(KEY_PRGM_UP) && keydownhold(KEY_PRGM_UP))
}[/code]
+
  }

Revision as of 17:53, 10 February 2012

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:

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


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