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