Public Group

# Multiple keypresses with SDL

This topic is 4605 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Up until now, my tilesbased RPG has only had four directions of movement. I wanted to add so that you could walk diagonally by pressing two arrow keys at the same time. The code seems to be working, the only potential error I could find were where I take the actual input. Is it able to check two keystates at once with SDL? Heres what it looks like:
Uint8 *keystate = SDL_GetKeyState(NULL);	// check the keyboard

// 7 0 1
// 6   2
// 5 4 3
if(!keystate[SDLK_UP] && !keystate[SDLK_RIGHT] && !keystate[SDLK_DOWN] && !keystate[SDLK_LEFT]) {
Wizard.SwitchAnimation("idle");
Wizard.MoveFlag(false);	// standing still
}
else {
Wizard.SwitchAnimation("walking");
Wizard.MoveFlag(true);

if(keystate[SDLK_UP])
Wizard.SetDirection(0);
else if(keystate[SDLK_RIGHT] && keystate[SDLK_UP])
Wizard.SetDirection(1);
else if(keystate[SDLK_RIGHT])
Wizard.SetDirection(2);
else if(keystate[SDLK_RIGHT] && keystate[SDLK_DOWN])
Wizard.SetDirection(3);
else if(keystate[SDLK_DOWN])
Wizard.SetDirection(4);
else if(keystate[SDLK_DOWN] && keystate[SDLK_LEFT])
Wizard.SetDirection(5);
else if(keystate[SDLK_LEFT])
Wizard.SetDirection(6);
else if(keystate[SDLK_LEFT] && keystate[SDLK_UP])
Wizard.SetDirection(7);
}


Up, down, left and right works ok. But not the diagonal stuff. I placed a break point on the "SetDirection(1)" just below when I test if UP and RIGHT were pressed at the same time. I started the program and pressed those keys but nothing happened, breakpoint never reached. Instead I switch the statements so that pressing just RIGHT had the same effect as pressing UP and RIGHT at the same time. And my little wizard character started to move diagonally, so I dare to say the rest of the code is working. How should I take multiple key presses?

##### Share on other sites
what you do:

if up is on then Wizard.SetDirection(0) but if not then check if right and up are one if so Wizard.SetDirection(1) ...

see ...

what you want would be something like this:

if( up ){    if( right ) // check if right is pressed as well    {       ...    }    else if( left) // let's see if left is pressed as well    {       ....    }    else // left and right are NOT pressed    {       ....    }}else if ( down ) // here we know that up is not pressed!{    if( right ) // check if right is pressed as well    {       ...    }    else if( left) // let's see if left is pressed as well    {       ....    }    else // left and right are NOT pressed    {       ....    }}else if( right ) // here we know that up and down are not pressed!{}else if( left ) // only left is left :D{ ....}

or you could simply remove your else statements like so:

if( up )   set direction for upif( up and right)    set direction for up and right  // this will override the above call to set direction...

##### Share on other sites
Thanks a bunch. It seems to work now. :)

1. 1
2. 2
3. 3
4. 4
frob
12
5. 5

• 16
• 13
• 20
• 12
• 19
• ### Forum Statistics

• Total Topics
632172
• Total Posts
3004561

×

## Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!