I'm trying to develop a screen locker for the Prizm (this time, I'll finish this project, I promise!). I have a few problems regarding the keyboard input.

The first is well known: the keyboard is too sensitive and key debouncing is needed. My difficulty is: I searched the forum and I could not find a definitive answer to key debouncing with non-blocking input (i.e. using GetKeyWait_OS). Can someone do the favor of explaining in a simple way, and preferably with code examples, how to do key debouncing? Smile

The other question is related to the values GetKeyWait_OS returns.
I'm using the following to detect a pressed key on the keyboard:

Code:
GetKeyWait_OS(&kcol,&krow,KEYWAIT_HALTOFF_TIMEROFF,0,1,&key)


If a key is pressed the function will return something other than zero, and kcol will contain the "column" of the key, and krow the "row". The 1 before &key is to block the Menu key from actually showing the menu (so the calculator is "locked").
What is the value of kcol and krow for the various keys of the keyboard? I know kcol 4 and krow 9 are the "coordinates" for the Menu key. But the "columns" and "rows" system doesn't look like it follows the order of the keys in the keyboard, or else I'm very dumb and not seeing it.

Anticipated regards Smile
Here's a chunk of code I use:


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);
   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. It supports the best way, multiple key detection, with all of this. Here's how it would be used in the 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))
}


Pretty simple to work with, and pretty flexible. It's basically what I put together for Raptor since I needed multiple key detection and bullet-button debouncing, and it's very flexible and works very very well.
I don't understand what mean "key debouncing", but you can use this function to read keyboard (non-blocking)


Code:
int keydown(int basic_keycode)
{
   const unsigned short* keyboard_register = (unsigned short*)0xA44B0000;
   int row, col, word, bit;
   row = basic_keycode%10;
   col = basic_keycode/10-1;
   word = row>>1;
   bit = col + 8*(row&1);
   return (0 != (keyboard_register[word] & 1<<bit));
}
Ashbad's code solved my problem. What I meant was that I did not want a key to be recognized as pressed twice (e.g. on two GetKey results) while it had never been released.

By other words: pressing and holding a key counts as a single press (at least for a certain amount of time), so that it doesn't repeat a certain keypress like crazy.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement