SDL2 PollingEvent and KeyDown Delay

So I have this bit of code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
int main(int argc, char* args[]) {
    int framecount = 0;
    int counter = 0;
    //Start up SDL and create window
    if (!init()){
        printf("Failed to initialize\n");
    }
    else{
        //Load media
        if (!loadMedia()){
            printf("Failed to load media\n");
            
        }
        else{
            //Main loop flag
            bool quit = false;
            
            //Event Handler
            SDL_Event e;
            
            while(!quit){
                
                //Handle events on queue
                while (SDL_PollEvent(&e) != 0){
                    //User requests quit
                    if (e.type == SDL_QUIT){
                        quit = true;
                    }
                    else if (e.type == SDL_KEYDOWN){
                        switch (e.key.keysym.sym){
                            case SDLK_UP:{
                                std::cout << "Counter: " << counter << std::endl;
                                counter++;
                                break;
                            }
                        }
                    }
                }
                
                //Clear screen
                SDL_SetRenderDrawColor(gRenderer, 255, 255, 255, 0xFF);
                SDL_RenderClear(gRenderer);
                
                //Update screen
                SDL_RenderPresent(gRenderer);
                std::cout << "Frame: " << framecount << std::endl;
                framecount++;
            }
        }
    }
    
    //Free resources and close SDL
    close();
    
    return 0;
}

When I run it and hold down the up key, I get this output:

Counter: 0
Frame: 33
Frame: 34
Frame: 35
Frame: 36
Frame: 37
Frame: 38
Frame: 39
Frame: 40
Frame: 41
Frame: 42
Frame: 43
Frame: 44
Frame: 45
Frame: 46
Frame: 47
Frame: 48
Frame: 49
Frame: 50
Frame: 51
Frame: 52
Frame: 53
Frame: 54
Frame: 55
Frame: 56
Frame: 57
Frame: 58
Frame: 59
Frame: 60
Frame: 61
Frame: 62
Frame: 63
Counter: 1
Frame: 64
Frame: 65
Frame: 66
Frame: 67
Counter: 2
Frame: 68
Frame: 69
Frame: 70
Frame: 71
Frame: 72
Counter: 3
Frame: 73
Frame: 74
Frame: 75
Frame: 76
Frame: 77
Counter: 4
Frame: 78
Frame: 79
Frame: 80
Frame: 81
Frame: 82
Frame: 83

I have two questions: Why doesn't the counter get displayed every iteration of the while(!quit) loop. Is there a delay in SDL adding a key event to the queue? The counter gets displayed consistently every 5 iterations. Also, why is there such a delay between the first output of counter and the second?

Thanks
Last edited on
If there were no delay the loop handling the events would not end for as long as you hold the key down because SDL_PollEvent would continuously give you another SDL_KEYDOWN event.

Repeated SDL_KEYDOWN events are mostly useful for text input and certain GUI actions. If you just want to check if a key is currently hold down you can use the SDL_GetKeyboardState function.

https://wiki.libsdl.org/SDL_GetKeyboardState
Last edited on
Topic archived. No new replies allowed.