SDL_PollEvent with multiple key presses

Started by
2 comments, last by Gage64 15 years, 9 months ago
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.
Advertisement
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.
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]
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.

This topic is closed to new replies.

Advertisement