Advertisement

Getting multiple key presses in SDL...

Started by January 10, 2005 08:06 PM
5 comments, last by Koshmaar 19 years, 8 months ago
Anyone know an easy way to do this? Im doing a simple pong game, but only one person can move at a time.
---"What? What is it? Is it the monkey in the pants? It's the monkey in the pants, isn't it. Don't give me that look."
I believe you can querey the keyboard with SDL, but a much better way todo it with SDL's key events. If you only look at key down events then you'll have this:

Player 1 pushes: Up
Player 2 pushes: Up (over riding player 1's up)

What you need todo is more like this:

Player 1 pushes: Up
Set player1_up = true;
Player 2 pushes: Up
Set player2_up = true; (which keeps player1's up var is still valid!)

Player 2 lets go of up:
Set player2_up = false;

etc

The idea is that you set a bool when a key is pressed, and make it false when the key is released and in your game loop you check those bool values. This allows you to hold down heaps of keys at once and have them all register. A simple array of 4 bools should do (up and down for both players), and the code to set and check the values should be pretty simple.

Edit: This alos makes AI work nicly as it can just set these variables to simulate keypresses, instead of having to actually move the paddle manually.
Advertisement
But why would you go through all that, setting bools and the like, when you could just use SDL_GetKeyState?
Kylotan: There was disscussion about SDL_GetKeyState some time ago on the SDL mailing list; I encountered stupid problem when using it and mailed for help; I get many responses that told me to get away with SDL_GetKeyState and switch for translating events. Ie.

Bob Pendelton:
Quote:
Since the key vector shows you the state at the time you check it, it is
pretty easy to miss key presses and shift presses. People don't press
them at the same time so you might get wrong results at times. The only
way around that problem is to use a form of input that doesn't let you
miss key presses/releases.


Sam Lantinga:
Quote:
As was mentioned elsewhere, if you rely on the current state of the keyboard,
you'll miss keys that were pressed and then released before you polled the
keyboard state.

Since most operating systems handle keyboard and mouse input in an input queue,
and you only have a snapshot of the keyboard state at any given time, the only
way that I know of to catch keyboard state changes as they occur is to process
events and handle them appropriately.

Basically SDL_GetKeyState() gives you the current state after all events
have been processed, so if a key or button has been pressed and released
before you process events, then the pressed state will never show up in
the getstate calls.

Well, yes, but that won't solve your problem. The SDL keysyms only correspond
to unmodified keys on the keyboard. For example, the French keyboard
doesn't actually have number keys. :) To get '1' on a French keyboard, you
have to press shift and another key (I forget which), so you'll never get a
keyboard event with SDLK_1 in it on a French keyboard. If you enable UNICODE
translation though, you'll get an event with '1' in the unicode field when the
appropriate key combination is pressed.


Since then I switched to events and have no problems whatsover :-/ I can share some code if anyone wants.
Yeah, I suppose it depends what you need; I think I misunderstood the original question.
SDL_GetKeyState is just same as using GetDeviceState on a keyboard in DirectX instead of the buffered GetDeviceData. Its unlikely you'll loose keypresses unless a player presses a key for less than one frame.
Advertisement
Quote: Its unlikely you'll loose keypresses unless a player presses a key for less than one frame.


That's exactly what Sam Lantinga said in the second paragraph; if you have ie. 60 frames per second, that gives you 1000/60 = 16,6 miliseconds for keypress; there are chances that some keypresess will be lost if you use GetKeyState.

This topic is closed to new replies.

Advertisement