SDL KeyRepeater trouble

I’m trying to write a turn-based game but in this game you should be able to run (taking turns very fast - It’s a ‘roguelike’ game if you know what that is). Everything works fine except key handling:

In my game if you hold down an arrow key to run in one direction and then quickly switch
to another arrow key to run in another direction (holding the new key
down) the character actually moves only one step in this new direction - and
then stops. Is SDLs key repeater buggy or what I’m doing wrong?

My key input routine looks like this:

/* Enable Key Repeat */
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,SDL_DEFAULT_REPEAT_INTERVAL);

/* wait for keypress or program termination. Ignore other events /
/
Changing this to a plain SDL_WaitEvent() doesn’t solve the problem
*/
do {
SDL_WaitEvent(&event);
} while (event.type != SDL_KEYDOWN && event.type != SDL_QUIT);

/* handling the events /
if ( event.type == SDL_QUIT ) { /
program termination / }
if ( event.type == SDL_KEYDOWN ) {
if ( event.key.keysym.sym == SDLK_UP ) { /
move up / }
if ( event.key.keysym.sym == SDLK_DOWN ) { /
move down / }
if ( event.key.keysym.sym == SDLK_LEFT ) { /
move left / }
if ( event.key.keysym.sym == SDLK_RIGHT ) { /
move right */ }
}

IMO you dont want to be using the key repeater, you want to use:

Keys=SDL_GetKeyState(0);

that returns an array that gives the state of each key. Your code could be:

if(Keys[SDLK_LEFT])
… //move left
else if(Keys[SDLK_RIGHT])
… //move right

this works great and you dont have the anoying keyboard repeat thing where
when you hold it down it types 1 letter, pauses, then starts spitting out a
bunch. It just works :stuck_out_tongue:

this way too, you can also handle it when users hold down more than 1 key at
a time.> ----- Original Message -----

From: whiteindian@arcor.de ()
To:
Sent: Friday, April 25, 2003 10:55 AM
Subject: [SDL] SDL KeyRepeater trouble

I’m trying to write a turn-based game but in this game you should be able
to run (taking turns very fast - It’s a ‘roguelike’ game if you know what
that is). Everything works fine except key handling:

In my game if you hold down an arrow key to run in one direction and then
quickly switch
to another arrow key to run in another direction (holding the new key
down) the character actually moves only one step in this new direction -
and
then stops. Is SDLs key repeater buggy or what I’m doing wrong?

My key input routine looks like this:

/* Enable Key Repeat */
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,SDL_DEFAULT_REPEAT_INTERVAL);

/* wait for keypress or program termination. Ignore other events /
/
Changing this to a plain SDL_WaitEvent() doesn’t solve the problem
*/
do {
SDL_WaitEvent(&event);
} while (event.type != SDL_KEYDOWN && event.type != SDL_QUIT);

/* handling the events /
if ( event.type == SDL_QUIT ) { /
program termination / }
if ( event.type == SDL_KEYDOWN ) {
if ( event.key.keysym.sym == SDLK_UP ) { /
move up / }
if ( event.key.keysym.sym == SDLK_DOWN ) { /
move down / }
if ( event.key.keysym.sym == SDLK_LEFT ) { /
move left / }
if ( event.key.keysym.sym == SDLK_RIGHT ) { /
move right */ }
}


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl