[SDL] Keyboard movement: Can't use two keys at once?

Peter Mackay mackay.pete+gmane at gmail.com
Fri Sep 15 02:05:05 PDT 2006


If a user presses two keys at once, you will get two "key down" messages 
(one for each key), so depending on how you're handling your events you can 
handle multiple keypresses there, or you can check the key state array.

If you do that, you could do something like:

    if (keystate[SDLK_A] && keystate[SDLK_B])
    {
        // Both keys are pressed.
    }

Note that due to keyboard hardware limitations, some key combinations (i.e. 
pressing several keys at once) will not register. Sometimes your PC will 
beep in this case. There's probably a good doc on the web somewhere about 
this, but I've just settled for never requiring several keys to be pressed 
at once.

Most noticably, this problem shows up if you want to have 2 players share a 
keyboard for example. One player's keypresses can effectively block the 
other player's.

Peter

"DARKGuy ." <dark.guy.2008 at gmail.com> wrote in message 
news:9f8ddbb20609141058p184ecf66v97f612f463f1dfcf at mail.gmail.com...
> Whoops! my bad then :( I think the only way is using IFs :P
>
> On 9/14/06, Peter Mulholland <darkmatter at freeuk.com> wrote:
>>
>> Hello DARKGuy,
>>
>> Thursday, September 14, 2006, 5:58:31 PM, you wrote:
>>
>> > I think it would be something like...
>>
>> > Uint8 *keystate = NULL;
>> > switch (keystate = SDL_GetKeyState(NULL)){
>> >     case SDLK_UP:
>> >         move_up=true;
>> >         break;
>> >     case SDLK_DOWN:
>> >         move_down=true;
>> >         break;
>> > }
>>
>> > I'm not at home, but that should do it I guess.
>>
>> No - the return value from SDL_GetKeyState() is a pointer. This would
>> not do what you want.
>>
>> You cannot check an array using switch/case.
>>
>> --
>> Best regards,
>> Peter                            mailto:darkmatter at freeuk.com
>>
>>
>> _______________________________________________
>> SDL mailing list
>> SDL at libsdl.org
>> http://www.libsdl.org/mailman/listinfo/sdl
>>
>


--------------------------------------------------------------------------------


> _______________________________________________
> SDL mailing list
> SDL at libsdl.org
> http://www.libsdl.org/mailman/listinfo/sdl
> 







More information about the SDL mailing list