Enter / Escape key not detected by SDL_PollEvent() if SDL_WaitEvent() is called previously

Hi guys,

It seems like calling SDL_WaitEvent() before SDL_PollEvent() cause Enter and Escape key to be undetected by SDL_PollEvent(). If I remove SDL_WaitEvent(), then it works again. I wonder if this behavior is expected? I’m using SDL2 2.28.1 on macOS (Intel).

#include <SDL2/SDL.h>

int main()
{
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_Window *window = SDL_CreateWindow("Test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, SDL_WINDOW_SHOWN);
    SDL_bool running = SDL_TRUE;

    while (running) {
        SDL_Event event;

        SDL_WaitEvent(&event); // Adding SDL_WaitEvent() seems to make Escape and Enter key undetected by SDL_PollEvent()

        while (SDL_PollEvent(&event)) {
            switch (event.type) {
            case SDL_QUIT:
                running = SDL_FALSE;
                break;
            case SDL_KEYUP:
                printf("%d\n", event.key.keysym.sym);
                break;
            }
        }

        SDL_Delay(16);
    }

    SDL_DestroyWindow(window);
    SDL_Quit();
}

Yes, it is expected unless you pass NULL as argument to SDL_WaitEvent.

https://wiki.libsdl.org/SDL2/SDL_WaitEvent
If event is not NULL, the next event is removed from the queue and stored in the SDL_Event structure pointed to by event.

2 Likes

Thanks @Peter87! I understand what’s going on now.