How to disable key repeat in SDL2?

别说谁变了你拦得住时间么 提交于 2019-12-06 18:20:07

问题


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?


回答1:


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




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/22156815/how-to-disable-key-repeat-in-sdl2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!