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

Okay :stuck_out_tongue: I know there’s a problem since computers were born (or well… I did
xD) that you couldn’t press more than three keys at once. That still exists
in these new systems (no matter what OS you use) and we all can (and do)
live with it. However, there’s this little thing that’s bugging me now…
I’m using this for moving my spaceship -v

    SDL_PollEvent(&event);
    if (event.type == SDL_QUIT){ salir = 1; }
    if (event.type == SDL_KEYDOWN){
        if(event.key.keysym.sym == SDLK_UP){ Player1->py -= 3; }
        if(event.key.keysym.sym == SDLK_DOWN){ Player1->py += 3; }
        if(event.key.keysym.sym == SDLK_LEFT){ Player1->px -= 3; }
        if(event.key.keysym.sym == SDLK_RIGHT){ Player1->px += 3; }
    }

It works, but only if I press the key once, release it and then hold it
pressed, like a tap (which is not the idea) and also, it doesn’t let me
press two keys at once. Say, for moving diagonally, I could press both up
and right keys, but the game doesn’t get both keys, just one of them, and
that’s not good :frowning:

I tried following a tutorial in this site http://gpwiki.org/index.php/SDL ,
both the keyboard basics and the practical movement one, but I don’t see how
SDL_KEYUP would help me in this case… maybe with the having-to-tap-the-key
problem, but not with the diagonal movement…

Any suggestions? :slight_smile:

Thanks in advance :slight_smile:

  • DARKGuy

When you receive a SDL_KEYDOWN event for ‘up arrow’ you can set a
’Ship Moving Up’ flag, and later when you receive a SDL_KEYUP event
for ‘up arrow’ you can clear it.

Alternatively you can use the SDL_GetKeyState() function to get a
snapshot of the entire keyboard state. Then you can have logic like:

“if up-arrow is pressed then move ship up”

etc…On 9/14/06, DARKGuy . <dark.guy.2008 at gmail.com> wrote:

[snip]
but I don’t see how
SDL_KEYUP would help me in this case…


Regards,
Rasmus Neckelmann

YAY! that worked like a charm! :smiley:

Uint8 *keystate = NULL;
keystate = SDL_GetKeyState(NULL);
if ( keystate[SDLK_UP] ) Player1->py -= 3;
if ( keystate[SDLK_DOWN] ) Player1->py += 3;
if ( keystate[SDLK_LEFT] ) Player1->px -= 3;
if ( keystate[SDLK_RIGHT] ) Player1->px += 3;

:slight_smile: thanks a bunch! :DOn 9/14/06, Rasmus Neckelmann wrote:

On 9/14/06, DARKGuy . <@DARKGuy> wrote:

[snip]
but I don’t see how
SDL_KEYUP would help me in this case…

When you receive a SDL_KEYDOWN event for ‘up arrow’ you can set a
’Ship Moving Up’ flag, and later when you receive a SDL_KEYUP event
for ‘up arrow’ you can clear it.

Alternatively you can use the SDL_GetKeyState() function to get a
snapshot of the entire keyboard state. Then you can have logic like:

“if up-arrow is pressed then move ship up”

etc…


Regards,
Rasmus Neckelmann


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

so how would you do that with switch/case?On 9/14/06, DARKGuy . <dark.guy.2008 at gmail.com> wrote:

YAY! that worked like a charm! :smiley:

Uint8 *keystate = NULL;
keystate = SDL_GetKeyState(NULL);
if ( keystate[SDLK_UP] ) Player1->py -= 3;
if ( keystate[SDLK_DOWN] ) Player1->py += 3;
if ( keystate[SDLK_LEFT] ) Player1->px -= 3;
if ( keystate[SDLK_RIGHT] ) Player1->px += 3;

:slight_smile: thanks a bunch! :smiley:

On 9/14/06, Rasmus Neckelmann wrote:

On 9/14/06, DARKGuy . < dark.guy.2008 at gmail.com> wrote:

[snip]
but I don’t see how
SDL_KEYUP would help me in this case…

When you receive a SDL_KEYDOWN event for ‘up arrow’ you can set a
’Ship Moving Up’ flag, and later when you receive a SDL_KEYUP event
for ‘up arrow’ you can clear it.

Alternatively you can use the SDL_GetKeyState() function to get a
snapshot of the entire keyboard state. Then you can have logic like:

“if up-arrow is pressed then move ship up”

etc…


Regards,
Rasmus Neckelmann


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


Sahan Chandrasekara

Get a free sony psp at http://psps.freepay.com/?r=30890215 (not a scam)

For instance:

switch(Event.type) {
case SDL_KEYDOWN:
if(Event.key.keysym.sym == SDLK_UP)
moving_up = true;
break;
case SDL_KEYUP:
if(Event.key.keysym.sym == SDLK_UP)
moving_up = false;
break;
}

if(moving_up)
move_up();

or something like that.On 9/14/06, Sahan Chandrasekara wrote:

so how would you do that with switch/case?


Regards,
Rasmus Neckelmann

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.

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:@Peter_Mulholland

Whoops! my bad then :frowning: I think the only way is using IFs :POn 9/14/06, Peter Mulholland 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

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 :frowning: I think the only way is using IFs :stuck_out_tongue:

On 9/14/06, Peter Mulholland 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

Hehe, yup! that used to happen a lot when I used to play some DOS/Windows
games with my sister sharing the same keyboard. It beeped :stuck_out_tongue: but thanks for
the code suggestion, I’ll keep it for the case when I’ll need to use it,
thanks! :DOn 9/15/06, Peter Mackay <mackay.pete+gmane at gmail.com> wrote:

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 .” <@DARKGuy> wrote in message
news:9f8ddbb20609141058p184ecf66v97f612f463f1dfcf at mail.gmail.com

Whoops! my bad then :frowning: I think the only way is using IFs :stuck_out_tongue:

On 9/14/06, Peter Mulholland 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


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