Okay 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
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
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ā¦
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ā¦
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;
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:
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!
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;
thanks a bunch!
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 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
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
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 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 I think the only way is using IFs
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