21

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?

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
Diaz
  • 957
  • 2
  • 13
  • 22
  • 1
    If these answers really are the best solution, that just seems wrong. "key down" should mean when the key is physically pressed down. Any type of automated repeating should be an optional behavior. – xaxxon Nov 29 '15 at 06:37

3 Answers3

38

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

joshbodily
  • 1,038
  • 8
  • 17
  • 1
    what about the all the events generated that are not processed because of repeat? wouldn't be just better having a way to disable "key repeat" events? And just receiving key up/down? – Raffaello Apr 06 '20 at 14:12
5

You may be better served by the SDL_GetKeyboardState function which tells you all the keys that are pressed. You can also just check the repeat flag in the event and ignore when repeat is true.

joeforker
  • 40,459
  • 37
  • 151
  • 246
1

You can do it yourself, by adding your "down" key into a list and removing them when you catch a KEY_UP and at each frames you can iterate your list to know which key is still down.

jordsti
  • 746
  • 8
  • 12
  • 2
    That sounds totally doable, but isn't this supposed to be a standard function? I was just looking for the one that impersonates SDL_EnableKeyRepeat:) Thanks for answering. – Diaz Mar 04 '14 at 01:04
  • 1
    what about the all the events generated that are not processed because of repeat? wouldn't be just better having a way to disable "key repeat" events? And just receiving key up/down? – Raffaello Apr 06 '20 at 14:12