[SDL] Multiple key pushes in SDL

Ray Kelm rhk at newimage.com
Mon Aug 13 16:46:01 PDT 2001


On Monday 13 August 2001 19:24, you wrote:
> Hello All,
>
> I've got a small problem concerning SDL and multiple key pushes.
> Atm I'm writing a programm (emulator) which requires multiple key pushes.
> For instance, I'm playing an racing game. I want to throttle using the UP
> button (SDLK_UP) and steer using right and left.
>
> Only when I push up and then try to steer then the up button is
> not active anymore (whilst it is pushed)! I need to push the up
> button again to make speed. I used the examples from the Key event but
> somehow I just can't get it working.
>
> How can I make sure that the button pushes stays in the event queue
> when other buttons are pressed?
>
> If somebody has a idea, please give me your knowledge.

First, be aware that certain key combinations cannot be registered by PC 
keyboards, due to hardware limitations in the keyboard itself.

In a game that is based on holding keys down, it is probably best to merely 
set a flag in the event handler when a keypress is received. Some other code 
which runs in your main loop should act on those flags. When the key is 
released (another event), clear the flag.

So if the user presses the UP key, it might set the 'up_pressed' flag, and if 
the user presses the LEFT key, it might set the 'left_pressed' flag. The game 
logic can then act on both of those flags to determine what happens.

-Ray




More information about the SDL mailing list