Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable key repeat in SDL2?

Tags:

sdl

sdl-2

There used to be a function named SDL_EnableKeyRepeat() in SDL, but not anymore in SDL2.

I searched around in SDL2-wiki but failed to locate anything relevant.

Any ideas?

like image 550
Diaz Avatar asked Mar 03 '14 20:03

Diaz


1 Answers

When handling a keyboard event, just filter out any events that are repeat events, i.e. check the repeat field of the SDL_KeyboardEvent of the SDL_Event union.

For example:

SDL_Event event;
while (SDL_PollEvent(&event)) {
  if (event.type == SDL_QUIT) {
    quit = true;
  }
  if (event.type == SDL_KEYDOWN && event.key.repeat == 0) {
    if (event.key.keysym.sym == SDLK_d)
      debug = debug ? false : true;
    // ... handle other keys
  }
}

See https://wiki.libsdl.org/SDL_KeyboardEvent

like image 69
joshbodily Avatar answered Sep 22 '22 16:09

joshbodily