I'm new to game dev,and I just trying to create my first game,a simple pong game.But i can't handle the keyboard input.

This is what i got :

const Uint8 * keystates = SDL_GetKeyboardState(NULL);

if (_keystates[SDLK_s])
    std::cout << "Down is pressed";

But if i run it and press 's',nothing ever happens.

if (_keystates[SDLK_RETURN])
    std::cout << "Return is pressed";

For this one is working just when i press 'j'... Yeah,right?

And more,if I add this line :

if (_keystates[SDLK_DOWN])
    std::cout << "Bla bla..";

My game crashes,what do you think is the problem?Thank you.


1 Answer 1


As it says on the SDL documentation, SDL_GetKeyboardState returns an array of the keyboard state, with SDL_SCANCODE values as indices. You can't use the SDLK_ values, they won't reference the right values. So change your code to use the SDL_SCANCODE values. There is a reference page here.

Your if-statement should then look like if (_keystates[SDL_SCANCODE_RETURN]) {...}.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .