Reputation: 448
Does anyone know how to get SDL_GetKeyState to work properly, without having to setup a loop which polls the SDL Events?
int *keystates;
keystates = SDL_GetKeyState(NULL);
SDL_PumpEvents();
std::cout << "Test 1\n";
if (keystates[SDLK_F1]) {
std::cout << "Test 1 Okay\n";
key_ac = true;
emu->setPower(true);
}
This code is run over 100 times a second, however even when I hold down the F1 key, I still do not get any output to say it was successful.
I have no SDL event loop, so could this be that events are being discarded because I'm not using them? In which case, how could I get SDL to filter out all events?
Upvotes: 3
Views: 8357
Reputation:
SDL_GetKeyState()
only has to be called once at the start of your program.
unsigned char * keys = SDL_GetKeyState(NULL);
Then the value it returns, a unsigned char pointer to an array is stored internally. To to update the state of the array call the function SDL_PumpEvents()
.
To update array keys
in your main loop:
SDL_PumpEvents();
if( keys[ SDLK_m ] )
{
//do stuff
}
EDIT:
You can call SDL_GetKeyState()
as much as you want after the first call and you won't create a memory leak. The function is always returning the same pointer.
Upvotes: 7