Key event handling on the Pandora
Had a bit of a struggle recently attempting to get key event polling working correctly on the Pandora. Figured I'd post my victory in the "event" anyone else has the same bit of woe.
Using SDL for key event polling, I had code that ran fine on my x86 based Linux laptop. However, when I pushed it over to the arm based Pandora and compiled, everything was way off. The behavior would manifest itself as false positives (ghosted) for unpressed buttons (in the current event pass), causing repetitive motion. Initially, my code maintained state for keyevents (down and up) for fluid key repeating behavior. This was necessary as certain animation states would cycle depending on state transitions...However, on the Pandora, it constantly seemed to receive false positives, causing, for example, the player to constantly jump up and down. Also, I would see a d-pad up kick off the jumping behavior, when only an SDL_PAGEUP should.
Originally I was using the keystates array, but the system didn't seem to always give me the right values. I first changed the code to use keysym and it worked perfect on the PC's, but not the Pandora. This initial pass was a mixed solution (I didn't completely refactor everything to this), which turned out to be incorrect. Needed to be homogeneous. Here's what works:
- grab a pointer to the key array like so:
Uint8 *keys = SDL_GetKeyState(NULL); Do this just once, outside of the render loop. SDL takes care of updating this array.
- Poll your events every event loop:
SDL_PollEvent(&event); This internally pumps events and handles tracking the array of keystates.
- Next, implement your keystate manager to track previous key values vs new to know when things changed. This allows different animation states depending on what's occurring relative to what did occur. So, make sure to check the array every cycle for every key you care about like so:
int value = keys[SDLK_RIGHT]; You can aggregate these to look for more complex event patterns.
Hopefully this may help some other pandy developer.