You are here

Learn Something New Everyday

Development is going well and if I'm lucky I'll have something playable for Mega LAN XXI. I had an interesting experience while attempting to enable keyboard repeat for GUI widgets though, and I thought for the sake of maybe saving someone else some aggravation I would post about it here.

Keyboard repeat in SDL appears very simple. Just call SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); and voila, you should get SDL_Events as long as you hold down a key. Unfortunately it wasn't working for me.

It turns out the problem was that I put the call in my main loop, and adjusted the parameters depending on whether a GUI element had focus or I was just capturing game keypresses (which don't need keyboard repeat and could be adversely affected by it). My theory based on what I was able to find on Google (and which could be confirmed by looking at the SDL code if I were so inclined), is that the timer SDL uses to space out the repeated keyboard events gets reset every time you call SDL_EnableKeyRepeat, so my constantly calling it in a loop was not allowing the timer to ever fire off more events. The simple solution was to keep track of whether keyboard repeat was enabled and only make the function call when the state needed to be changed. After I did that everything worked swimmingly.