Shivam Gupta
Shivam Gupta

Reputation: 67

SDL 2.0 Keyboard input issues

When I try to Poll a keydown event in SDL 2.0 and hold down a key, I get multiple keydown events, one after the other. When I try running the same program using SDL 1.2.15 (with minor changes as SDL 1.2.15 does not support SDL_Window), I do not have this issue. The keydown event only occurs once like it is supposed to. I even tried executing the program on a different computer to make sure that it was not a computer specific issue.

The relevant code is as follows:

#include <iostream>
#include <SDL.h>
using namespace std;

SDL_Event event;
SDL_Window* screen = NULL;
int main(int argc, char* args[])
{
    if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
    {
        cout << "ERROR INIT";
        return 0;
    }
    screen = SDL_CreateWindow("My Game Window",
                          SDL_WINDOWPOS_UNDEFINED,
                          SDL_WINDOWPOS_UNDEFINED,
                          640, 480,
                          SDL_WINDOW_OPENGL);


    bool quit = false;
    while(!quit)
    {
        while(SDL_PollEvent(&event))
        {
            if(event.type == SDL_KEYDOWN)
            {
                cout << "KEY PRESSED!" << endl;
            }
        }
    }
    return 0;
}

Upvotes: 4

Views: 1861

Answers (1)

genpfault
genpfault

Reputation: 52084

Ignore SDL_KEYDOWN events where event.key.repeat != 0.

Upvotes: 3

Related Questions