Skip to main content
GameDev.net gamedev.net
🔒 Locked

SDL Multiple Key Press Events

Started by itsdbest Jun 23, 2007 at 12:30 AM 2 replies 9.4k views
Original Post
itsdbest
itsdbest
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
itsdbest
itsdbest
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
A dam
A dam
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;		}
itsdbest
itsdbest
Hey Thanks....

I think your way will work too.

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

Cheers,

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.