如何禁用 SDL2 中的重复键? [英] How to disable key repeat in SDL2?

查看:49
本文介绍了如何禁用 SDL2 中的重复键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 SDL 中曾经有一个名为 SDL_EnableKeyRepeat() 的函数,但在 SDL2 中不再有.

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

我在 SDL2-wiki 中四处搜索,但未能找到任何相关内容.

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

有什么想法吗?

推荐答案

在处理键盘事件时,只需过滤掉任何重复事件,即检查 repeat 字段SDL_Event 联合的 SDL_KeyboardEvent.

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.

例如:

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
  }
}

参见https://wiki.libsdl.org/SDL_KeyboardEvent

这篇关于如何禁用 SDL2 中的重复键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆