Jump to content
  • Advertisement
Sign in to follow this  
4th_dimention

SDL Input Trouble

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

I am having trouble with SDL keyboard inputs. I know how the event polling works (key presses and releases only happen once), so I chose to keep track of key states in a vector of bools, each key has its own index 1 means the key is being held, 0 means the key is not held. And that all works as it is suppose to, no errors, no glitches no bugs. But when I started carefuly testing my game, I found that if I held certain key combos it would prevent other key presses from being detected. (It started when I held the left key and Z and tried to jump... up key was not detected.) So I debugged as deeply as I could, starting with the game code, then the keystate tracker I described above, and found no problems. Where I did find the problem was in the sdl event polling loop. I simply am not getting these missing key presses from sdl.

Here is my event polling loop:

Keyboard::PreUpdate();
Mouse::PreUpdate();
SDL_Event ev;
while(SDL_PollEvent(&ev)){
std::cout << ev.type << " " << ev.key.keysym.sym;
switch(ev.type){
case SDL_KEYUP: case SDL_KEYDOWN:
Keyboard::Update(&ev);
break;
case SDL_MOUSEMOTION: case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONDOWN:
Mouse::Update(&ev);
break;
case SDL_QUIT:
// exit won't cut it, work on later
break;
}
EventMaster::Update(&ev);
}


I threw in the std::cout as a final measure for seeing if I was misshandling incoming events, but I simply did not get events for some of the key presses.

Is this an SDL limitation? Is this my hardware? Am I doing something wrong? Is there a work around? Information would be greatly appreciated.

Share this post


Link to post
Share on other sites
Advertisement
It is not a SDL limitation, it is a hardware limitation and has to do with how the keyboard is wired, some keyboards are significantly worse than others in this area. (The better gaming keyboards can handle 10 keypresses at the same time, cheap ones can fail at anything above 2 keys (I havn't seen any keyboard that can't handle 2, but those might exist aswell)

The easiest workaround is to use modifier keys for actions as far as possible, (Rather than Z, use left CTRL for example) as these tend to be wired separatly even on cheap keyboards.

Share this post


Link to post
Share on other sites
Yeah, hardware limitation. Moreover, which keys can be pressed completely depends on which combination you're doing - e.g. up + left will instantaneously kill the spacebar, but Z, X and C still work.

There is no workaround for this other than buying a new keyboard, period. Try using those combinations in games and you'll see how they fail to detect those presses as well. The best you can do is detect well-known problematic situations and avoid them for the default control set if possible.

(I havn't seen any keyboard that can't handle 2, but those might exist aswell)

Doubt those exist at all on PCs, but some cheap mobile devices with keyboards may have that limitation.

The easiest workaround is to use modifier keys for actions as far as possible, (Rather than Z, use left CTRL for example) as these tend to be wired separatly even on cheap keyboards.

The biggest problem is that then you're very prone to press the Windows key by accident...

The best solution is of course to let the user change the controls as needed - and if you can add support for controllers (optionally, of course), even better since they never have these issues.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!