SDL Input (basic question)

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!”)

SDL_Event event;
SDL_keysym keysym;

SDL_PollEvent(&event);

switch(event.type) {
	case SDL_QUIT:
		return (0);
	break;
	case SDL_KEYDOWN:
		keysym = event.key.keysym;
		if (keysym.sym == SDLK_ESCAPE) {
			return (0);
		}
		if (keysym.sym == keys.rotate_left) {
			fprintf(stdout, "rotating left!\n");
			fprintf(stdout, "keysym.sym       = %d\n", keysym.sym);
			fprintf(stdout, "keys.rotate_left = %d\n", keys.rotate_left);
			break;
		}
	break;
	default:
	break;
}

return (1);

– chris (@Christopher_Thielen)

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?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!”)


Petri Latvala
adrinael at nuclearzone.org

Unsurprisingly, this is a very common question, and it has been added to
the documentation:

http://sdldoc.csn.ul.ie/guideinputkeyboard.php

In short, if you want key repeat, you use the key events to keep track
of key state, and every time through your main loop, do something like:

if (leftIsDown)
printf(“rotating left!\n”);On Fri, 2002-07-26 at 15:31, Petri Latvala wrote:

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?


| Screwtape | Reply-To: munged on Usenet |________ ______ ____ __ _ _
_
|
| Anyone ever told you you’re beautiful? No? Ever wondered why?
|

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’.

Later

Jason> 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?