Sign in to follow this  

SDL_PollEvent with multiple key presses

This topic is 3447 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

Hello. Right now I'm having problems with the Input class of my application, as certain combinations of keys are not properly registered. Here's the basic code, which works pretty well for individual key strokes:
void D_Input::Update()
{
	while(SDL_PollEvent(&userInput))
	{
		switch(userInput.type)
		{
			case SDL_KEYDOWN:
			{
				KeyPress(userInput);
				break;
			}
			case SDL_KEYUP:
			{
				KeyRelease(userInput);
				break;
			}
		}
	}
}

void D_Input::KeyPress(SDL_Event userInput)
{
	if(userInput.key.keysym.sym == SDLK_UP)
			keyUp = 1;
	if(userInput.key.keysym.sym == SDLK_DOWN)
			keyDown = 1;
	if(userInput.key.keysym.sym == SDLK_LEFT)
			keyLeft = 1;
	if(userInput.key.keysym.sym == SDLK_RIGHT)
			keyRight = 1;
	if(userInput.key.keysym.sym == SDLK_z)
			keyJump = 1;
	if(userInput.key.keysym.sym == SDLK_x)
			keyAttack = 1;
}


The idea is that this code is executed on every game loop and polls all events, turning key flags on or off as needed. The thing is, it simply doesn't register some keys under certain combinations, like right-down-z (where z doesn't get registered). Is that a hardware problem? maybe a bug in the library? I need to produce efficient input for this application, as it is an action-based game. Any responses are greatly appreciated.

Share this post


Link to post
Share on other sites
You're only calling KeyPress() for two keys, but in that function you are testing for many keys. Besides, there's no need for the switch statement at all. You can just do this:

void D_Input::Update()
{
while(SDL_PollEvent(&userInput))
{
KeyPress(userInput);
}
}

void D_Input::KeyPress(SDL_Event userInput)
{
if(userInput.key.keysym.sym == SDLK_UP)
keyUp = 1;
if(userInput.key.keysym.sym == SDLK_DOWN)
keyDown = 1;
if(userInput.key.keysym.sym == SDLK_LEFT)
keyLeft = 1;
if(userInput.key.keysym.sym == SDLK_RIGHT)
keyRight = 1;
if(userInput.key.keysym.sym == SDLK_z)
keyJump = 1;
if(userInput.key.keysym.sym == SDLK_x)
keyAttack = 1;
}



Quote:
The thing is, it simply doesn't register some keys under certain combinations, like right-down-z (where z doesn't get registered).


I think this is a common problem where some keyboards can't handle 3 simultaneous key presses, in which case it has nothing to do with your code, but I could be wrong.

Share this post


Link to post
Share on other sites
There are additional functions in my code that take care of other input types, such as mouse movement or key release. I simply didn't include them because they are basically a rewrite of KeyPress(), and not that relevant to the problem.

If this is such a hardware issue, how could I find a workaround? other games such as CaveStory can perform many actions at a time (in this case firing downwards while jumping and running), so I'm not exactly sure if it is a hardware limitation.

EDIT: After doing some testing it appears I cannot perform such actions in CaveStory, after all. Still, any suggestions on how to minimize this would be very appreciated.

[Edited by - Methius on July 5, 2008 5:03:26 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by Methius
EDIT: After doing some testing it appears I cannot perform such actions in CaveStory, after all. Still, any suggestions on how to minimize this would be very appreciated.


One idea is to use the keyboard for only jumping and running, and use the mouse for shooting. This way you don't need to press 3 keys at once.

Share this post


Link to post
Share on other sites

This topic is 3447 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this