Jump to content
  • Advertisement
Sign in to follow this  
itsdbest

SDL Multiple Key Press Events

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey Guys, Have a small issue with SDL. I am using SDL Keyboard events in my game. The player can move with the arrow keys, and shoot with the spacebar. If i keep the left arrow pressed, the player moves to the left, but if I press the right arrow next, the player stops moving and I need to press the right arrow again for the player to start moving to the right. Also for instance I want to keep moving forward and press the shooting key. But only one action will take place and not the other So what I am looking for is how to handle multiple key press in SDL? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Hey,
Managed to solve the issue by using keystates. Found a post on the sdl mailing list for this.

For your reference:
http://lists.libsdl.org/pipermail/sdl-libsdl.org/2007-June/061361.html

Share this post


Link to post
Share on other sites
what you're going to want to do is make an array of all the keys and set the status of each key to "down" when they're pressed and "up" when they're released. I'm not sure if that's worded quite right (it IS 1:28am)

here's a little code though to show you what i mean
it's from an SDL tutorial i found awhile ago i found awhile ago

SDL_Event event;
bool gameRunning = true;
bool keysHeld[323] = {false}; // everything will be initialized to false
while (gameRunning)
{
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT) //If Player Closes Window
{gameRunning = false;}
if (event.type == SDL_KEYDOWN) //If Key is pressed down mark key as "pressed"
{keysHeld[event.key.keysym.sym] = true; }
if (event.type == SDL_KEYUP)
{keysHeld[event.key.keysym.sym] = false;}//Unmark "pressed" keys if they are up
}

if ( keysHeld[SDLK_ESCAPE] )
{
gameRunning = false;
}

Share this post


Link to post
Share on other sites
Hey Thanks....

I think your way will work too.

The sample code i received from the previous link does the job as well.

Cheers,

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

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!