[SDL] Re: SDL Input (basic question)

Jason Farmer jason at cerebral-bicycle.co.uk
Thu Jul 25 23:57:00 PDT 2002

If you were to press the left button repeatedly, you would see the message for 
each button press.

To get around this, I use an array where I store the state of the keys.
So when a Keydown message occurs, I set a bit to 1 in the array for the 
corresponding key and when a Keyup message occurs, I set it to 0.

I put all this into a function so that I can check the state of a particular 
key at any time.

I've not used SDL_EnableKeyRepeats but It's probably best leaving alone unless 
you're writing something that needs text input.

If you are stuck, I'd reccomend you read John R. Hall's book 'Programming 
Linux Games'.



> On Thu, Jul 25, 2002 at 09:14:53PM -0700, Chris Thielen wrote:
> > I have the following code segment in a function, and that function is
> > called often. I have the value of SDLK_LEFT stored as an SDLKey in the
> > "keys" structure. As a test, whenever the left arrow key is pressed, I
> > have it print "rotating left!". This is constantly being called in a
> > loop, yet if I press and hold the left arrow key down, it only prints
> > "rotating left!" once. What's wrong below? (I'd like it to constantly
> > print "rotating left!")
> KEYDOWN event is basically "a button went down". And in your case, the
> button goes down once, and thus generates only one event. One solution
> would be to update a flag (key_is_down) on KEYDOWN _and_ on
> KEYUP. Another would be to SDL_PumpEvents on regular intervals and
> checking the keys with SDL_GetKeyState. Others, how does enabling key
> repeat work here?

More information about the SDL mailing list