Flangy > Software Development > GBA Homebrew > Key Handling
Download: gba_keys.zip
These are some functions and macros to handle GBA key input. They can poll state, state changes, and do key repeat handling.
You'll want to include gba.h for the key bit definitions, and add this for the keys register:
#define REG_KEYS (*(volatile u16*)0x4000130)
(It's just an alias for the P1 register in the official dev docs.)
Call init_keys() along with your initialization code, and call do_keys() every frame.
In all of these functions, flag is one if the defined key bits. You can OR bits together to test for multiple keys with all functions EXCEPT key_triggered(flag), which must be passed a single bit only.
key_down(flag)
key_up(flag)
True if the given keys are all down or all up.
key_pressed(flag)
key_released(flag)
True if the given keys have just been pressed or released, which is the first frame that a key is down or up.
key_triggered(flag)
This function is used to support key repeat handling. This function is true the first frame that a key is down (the same as key_pressed), true again "first delay" frames later, and true every "next delay" frames after that if the key is still down.
This is useful for moving the cursor around in menus as long as a key is held. Just using key_down, you'd have to put in your own repeat delay code anyway (since moving the cursor each frame would likely be too fast.)
Don't pass an OR'd value into this function, just a single key bit.
To change the delay times edit this line in gba_keys.c with the first/next frame counts:
KeyRepeatRate default_repeat_rate = {16, 4};
key_held(flag)
key_unheld(flag)
key_held(flag) is true if a key is down for frames after it is initially pressed. key_unheld(flag) is true if a key is up for frames after it is initially released. I don't think these functions are that helpful.
key_same(flag)
key_changed(flag)
True if the given keys are in the same state as / different state from last frame. Not particularly useful by themselves, but key_changed is used as part of key_released.