Ok, I want to do something when a key is pressed. I’m looping an
SDL_PollEvent to do this, and among others asking for a SDL_KEYDOWN
event in a switch. Well, I noticed that the code inside that case
executes many times if I press a key once, so I want it to wait a bit
before two consecutive KEYDOWN events. I actually managed to do this,
but the code turned out to be quite ugly. Then, I found a function that
seems to do exactly what I want: SDL_EnableKeyRepeat. But I can’t get it
to work! This is what I’m doing:
…
SDL_EnableKeyRepeat(200, 200);
while (some_condition){
SDL_PollEvent(&event);
switch(event.type){
case SDL_KEYDOWN:
... //EXECUTE SOME CODE
break;
}
}
…
Is there something wrong with that? It behaves exactly the same when I
comment the EnableKeyRepeat line or use zero’s as its arguments.
Hey there,
Ah, I think I just figured it out. You probably should enclose your switch so it doesn’t get run unless SDL_PollEvent() returns true. You can see that in the code I’ve included.
If you’re trying to do something with repeatable input (like text), you might want to use this line:SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
When I’m making a game, I use this next line to turn off the repeat:
SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
Here’s some code that might help you figure out how to check keys with SDL. This is generally what I do for games…
SDL_Event event; Uint8* keystates; // This is for checking the state of keys.
// Polling events only checks CHANGES in state. keystates = SDL_GetKeyState(0); bool done = 0; while (!done) {
while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT)
exit(0); if (event.type == SDL_KEYDOWN) { if (event.key.keysym.sym == SDLK_ESCAPE) exit(0);
if (event.key.keysym.sym == SDLK_SPACE) { // jump or something (only once) } } // END if keydown
} // END while(pollevent)
if (keystates[SDLK_LEFT]) { // move left continuously }
else if (keystates[SDLK_RIGHT]) { // move right continuously } // draw, flip, etc.
} // END while(!done)
Jonny D> Date: Sat, 9 Jun 2007 13:38:38 -0300> From: manugarciac5 at yahoo.com.ar> To: sdl at lists.libsdl.org> Subject: [SDL] Arghhh, this is driving me crazy (SDL_EnableKeyRepeat)> > Ok, I want to do something when a key is pressed. I’m looping an> SDL_PollEvent to do this, and among others asking for a SDL_KEYDOWN> event in a switch. Well, I noticed that the code inside that case> executes many times if I press a key once, so I want it to wait a bit> before two consecutive KEYDOWN events. I actually managed to do this,> but the code turned out to be quite ugly. Then, I found a function that> seems to do exactly what I want: SDL_EnableKeyRepeat. But I can’t get it> to work! This is what I’m doing:> > > …> > SDL_EnableKeyRepeat(200, 200);> > while (some_condition){> > SDL_PollEvent(&event);> > switch(event.type){> case SDL_KEYDOWN:> … //EXECUTE SOME CODE> break;> }> }> > …> > > Is there something wrong with that? It behaves exactly the same when I> comment the EnableKeyRepeat line or use zero’s as its arguments.> _______________________________________________> SDL mailing list> SDL at lists.libsdl.org> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Play free games, earn tickets, get cool prizes! Join Live Search Club.?
http://club.live.com/home.aspx?icid=CLUB_wlmailtextlink
Yes! Thank you man, I had to do the while(PollEvent)
Jonathan Dearborn escribi?:> Hey there,
Ah, I think I just figured it out. You probably should enclose your
switch so it doesn’t get run unless SDL_PollEvent() returns true. You
can see that in the code I’ve included.
If you’re trying to do something with repeatable input (like text), you
might want to use this line:
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
When I’m making a game, I use this next line to turn off the repeat:
SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
Here’s some code that might help you figure out how to check keys with
SDL. This is generally what I do for games…
SDL_Event event;
Uint8* keystates; // This is for checking the state of keys.
// Polling events only checks CHANGES in
state.
keystates = SDL_GetKeyState(0);
bool done = 0;
while (!done)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
exit(0);
if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.sym == SDLK_ESCAPE)
exit(0);
if (event.key.keysym.sym == SDLK_SPACE)
{
// jump or something (only once)
}
} // END if keydown
} // END while(pollevent)
if (keystates[SDLK_LEFT])
{
// move left continuously
}
else if (keystates[SDLK_RIGHT])
{
// move right continuously
}
// draw, flip, etc.
} // END while(!done)
Jonny D
Date: Sat, 9 Jun 2007 13:38:38 -0300
From: @Manuel_Garcia_Cabrer
To: sdl at lists.libsdl.org
Subject: [SDL] Arghhh, this is driving me crazy (SDL_EnableKeyRepeat)
Ok, I want to do something when a key is pressed. I’m looping an
SDL_PollEvent to do this, and among others asking for a SDL_KEYDOWN
event in a switch. Well, I noticed that the code inside that case
executes many times if I press a key once, so I want it to wait a bit
before two consecutive KEYDOWN events. I actually managed to do this,
but the code turned out to be quite ugly. Then, I found a function that
seems to do exactly what I want: SDL_EnableKeyRepeat. But I can’t get it
to work! This is what I’m doing:
…
SDL_EnableKeyRepeat(200, 200);
while (some_condition){
SDL_PollEvent(&event);
switch(event.type){
case SDL_KEYDOWN:
… //EXECUTE SOME CODE
break;
}
}
…
Is there something wrong with that? It behaves exactly the same when I
comment the EnableKeyRepeat line or use zero’s as its arguments.
SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
Play free games, earn tickets, get cool prizes! Join Live Search Club.
Join Live Search Club!
http://club.live.com/home.aspx?icid=CLUB_wlmailtextlink
SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org