SDL 和 GLFW 每次轮询仅报告一个键(重复),即使同时按下多个键也是如此

SDL and GLFW only report one key (repeat) per poll even if multiple keys are held down at the same time

SDL_Event event;
while(SDL_PollEvent(&event)){
    if(event.type == SDL_KEYDOWN || event.type == SDL_KEYUP){
         //...
    }
}

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{

}

例如我同时按下WS,两个库都报告WS已被按下。

但如果我继续按住 WS,将只报告一个键,就好像另一个键根本没有按住一样。

此外,如果我按住 WS 并且只报告 S 并且我按下另一个键,例如 Q 两个库都不会报告任何当前完全按下键。

这两个库的键状态 repeated 似乎没什么用。

这是标准行为还是它只是发生在我的系统上?

您可以使用 SDL_GetKeyboardState when handling the SDL_KEYUP and SDL_KEYDOWN events to check for the state of more than a single key at a time. The keyboard state holds the current state of all the keys. The SDL_GetModState 调用应该用于 Ctrl 或 Shift 等修改键。

快速示例:

static void keyboard_handler()
{
    int n, count = 0;
    char buf[80];
    const uint8_t *state = SDL_GetKeyboardState(&count);
    buf[0] = 0;
    if (state[SDL_SCANCODE_RIGHT]) strcat(buf, "right ");
    if (state[SDL_SCANCODE_LEFT]) strcat(buf, "left ");
    if (state[SDL_SCANCODE_UP]) strcat(buf, "up ");
    if (state[SDL_SCANCODE_DOWN]) strcat(buf, "down ");
    if (buf[0] != 0)
        printf("%s\n", buf);
}

运行 这是对 SDL_KEYUPSDL_KEYDOWN 事件的响应 当一次按下超过 1 个箭头键时,我会打印多个单词。例如:

left
right left
left up down