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…')
 
m (Added category tag.)
 
(8 intermediate revisions by one other user not shown)
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:
+
== Synopsis ==
 +
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.  The needed function and variable declarations are as follows:
  
[code]const unsigned short* keyboard_register = (unsigned short*)0xA44B0000;
+
=== Definition ===
unsigned short*lastkey = malloc(sizeof(unsigned short)*8);
 
unsigned short*holdkey = malloc(sizeof(unsigned short)*8);
 
  
void keyupdate(void) {
+
  const unsigned short* keyboard_register = (unsigned short*)0xA44B0000;
  memcpy(holdkey, lastkey, sizeof(unsigned short)*8);
+
  unsigned short lastkey[8];
  lholdkey = llastkey;
+
  unsigned short holdkey[8];
  memcpy(lastkey, keyboard_register, sizeof(unsigned short)*8);
+
 
  llastkey = PRGM_GetKey();
+
  void keyupdate(void) {
}
+
    memcpy(holdkey, lastkey, sizeof(unsigned short)*8);
int keydownlast(int basic_keycode) {
+
    memcpy(lastkey, keyboard_register, sizeof(unsigned short)*8);
  int row, col, word, bit;  
+
  }
  row = basic_keycode%10;  
+
  int keydownlast(int basic_keycode) {
  col = basic_keycode/10-1;  
+
    int row, col, word, bit;  
  word = row>>1;  
+
    row = basic_keycode%10;  
  bit = col + 8*(row&1);  
+
    col = basic_keycode/10-1;  
  return (0 != (lastkey[word] & 1<<bit));  
+
    word = row>>1;  
}
+
    bit = col + 8*(row&1);  
int keydownhold(int basic_keycode) {
+
    return (0 != (lastkey[word] & 1<<bit));  
  int row, col, word, bit;  
+
  }
  row = basic_keycode%10;  
+
  int keydownhold(int basic_keycode) {
  col = basic_keycode/10-1;  
+
    int row, col, word, bit;  
  word = row>>1;  
+
    row = basic_keycode%10;  
  bit = col + 8*(row&1);  
+
    col = basic_keycode/10-1;  
  return (0 != (holdkey[word] & 1<<bit));  
+
    word = row>>1;  
}[/code]
+
    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:
 
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) {
+
  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
 +
    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))
 +
  }
  
  ...
+
=== Inputs ===
 +
keyupdate: None
  
  // checking to see if the up key has been held down for a while
+
keydownlast: int (KEY_PRGM_<key> keycode)
  if(keydownlast(KEY_PRGM_UP) && keydownhold(KEY_PRGM_UP))
 
  
  // checking to see if the up key has not been held down for a while
+
keydownhold: int (KEY_PRGM_<key> keycode)
  if(!keydownlast(KEY_PRGM_UP) && !keydownhold(KEY_PRGM_UP))
 
  
  // checking to see if the up key has just been pressed
+
=== Outputs ===
  if(keydownlast(KEY_PRGM_UP) && !keydownhold(KEY_PRGM_UP))
+
keyupdate: None
  
  // checking to see if the up key has just been released
+
keydownlast: 1 if key has been pressed during last keyupdate() call, 0 otherwise
  if(!keydownlast(KEY_PRGM_UP) && keydownhold(KEY_PRGM_UP))
+
 
}[/code]
+
keydownhold: 1 if key has been pressed two keyupdate() calls ago, 0 otherwise
 +
 
 +
== Comments ==
 +
It's a bit more wordy to use than its simple PRGM GetKey sibling, but it's also very flexible and can even be used to check for pseudo keydown/keyup key events.  It also reads directly from keyboard hardware ports, so it's very fast and doesn't rely on the OS's control.
 +
[[Category: Useful Routines]]

Latest revision as of 17:12, 18 April 2014

Synopsis

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. The needed function and variable declarations are as follows:

Definition

 const unsigned short* keyboard_register = (unsigned short*)0xA44B0000;
 unsigned short lastkey[8];
 unsigned short holdkey[8];
 
 void keyupdate(void) {
    memcpy(holdkey, lastkey, sizeof(unsigned short)*8);
    memcpy(lastkey, keyboard_register, sizeof(unsigned short)*8);
 }
 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:

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

Inputs

keyupdate: None

keydownlast: int (KEY_PRGM_<key> keycode)

keydownhold: int (KEY_PRGM_<key> keycode)

Outputs

keyupdate: None

keydownlast: 1 if key has been pressed during last keyupdate() call, 0 otherwise

keydownhold: 1 if key has been pressed two keyupdate() calls ago, 0 otherwise

Comments

It's a bit more wordy to use than its simple PRGM GetKey sibling, but it's also very flexible and can even be used to check for pseudo keydown/keyup key events. It also reads directly from keyboard hardware ports, so it's very fast and doesn't rely on the OS's control.