gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

Beginners > About KeyPad,Help!

#11963 - valkyrie_pf - Sat Oct 25, 2003 4:35 am

My code:
void QueryKey()
{ if(F_CTRLINPUT_A_PRESSED)
{
.........
}
}
void vblFunc()
{ QueryKey();
..........
}

when i press A(the KeyPad),it input many A.
I want only an A when I press A(the KeyPad) once.
How Can I Do ?

#11965 - sajiimori - Sat Oct 25, 2003 6:16 am

After processing input, save the keypad state. On the next frame, respond only to the keys that have changed.

#11984 - Gopher - Sat Oct 25, 2003 7:31 pm

IF you're only checking input at one point in your program, you can just create a global u16 PrevKeys. After your code that tests for key events, just set PrevKeys to the current key register values.

Then you can use the xor operator ( ^ ) to find bits that have just changed.

Here's some simple code to demonstrate this

Code:

//define the keystate register
#define KEYSTATE (*(volatile unsigned short *)0x04000130)

//make a global to hold previous keystate, initialize to no keys pressed
unsigned short PrevKeyState=0x3ff;

//this macro will test if a key is pressed
#define KEYDOWN(key) (!(KEYSTATE & (key)))

//this one will test if a key has CHANGED
#define KEYCHANGED(key) (!((KEYSTATE^PrevKeyState)&key)

int main()
{
    //initialize anything that needs initing
    Init();

    //main loop 
    while(1)
    {
        //if A is pressed and it just changed
         if (KEYCHANGED(KEY_A) && KEYPRESSED(KEY_A))
         {
             DoSomething();
         }
    }

  //never happens but the compiler insists it be here   
   return 0;
}


Haven't actually tested it but that should work.
_________________
"Only two things are infinite: the universe, and human stupidity. The first is debatable." -Albert Einstein

#11992 - valkyrie_pf - Sun Oct 26, 2003 4:29 am

Thanks