SDL_GetKeyState Question

Started by
6 comments, last by Kylotan 15 years, 8 months ago
I was recently working on an engine, and used Uint8 *keys=SDL_GetKeyState(NULL) in my main loop. At some point I call a function(keys). For some reason when inside the function, keys only contains the correct data 1/8 of the time, but in the main loop keys is always working. Inside the function keys seems empty the other 7/8 of the time. Can anybody tell me why this is? Let me know if it is code specific, and I will post my code. I am presently running the code on Windows by the way. I haven't tried it on any other platforms. Thanks.
Advertisement
Post the code.
Wow. Quick response. My code itself is at my house, and I will post it specifically this evening, but the gist of the problem area is the following:

class character{    int x, y;    int move;}int character::move(Uint8 *keys){    if(keys[SDLK_UP]) y--;    if(keys[SDLK_DOWN]) y++;    if(keys[SDLK_LEFT]) x--;    if(keys[SDLK_RIGHT]) x++;    if(keys[SDLK_UP]) y--;return 0;}character hero;bool quit=false;SDL_Event event;Uint8 *keys;while(!quit){    if(SDL_PollEvent(&event)    {        keys=SDL_GetKeyState(NULL);    }    if(keys[SDL_ESCAPE]) quit=true;    hero.move(keys);}


It is mainly a problem with holding down a key. When holding down up the character only moves about 1/8 of the time, but if I cut and paste that section of the move function into main then he moves 100% of the time.


By the way, what are the tags for placing code in a post?
I don't have a lot of experience with SDL, but from what I've seen, SDL_GetKeyState() is not meant to be used like that. You just call it every frame to get the current state of the keyboard:

while(!quit){    keys=SDL_GetKeyState(NULL);    if(keys[SDL_ESCAPE]) quit=true;    hero.move(keys);}
According to what I've read though the pointer that SDL_GetKeyState returns is constant throughout the program and is updated by the event pipe, so calling the SDL_GetKeyState in main before the loop even starts should be and is actually fine if I look at keys in the main loop, but again doesn't seem to work if I look at keys in the function.
Quote:Original post by Zael
According to what I've read though the pointer that SDL_GetKeyState returns is constant throughout the program and is updated by the event pipe, so calling the SDL_GetKeyState in main before the loop even starts should be and is actually fine if I look at keys in the main loop, but again doesn't seem to work if I look at keys in the function.


I'm not sure what you mean. Why do you need to call it before the main loop?

Also, did you try my code?
I wasn't saying you have to, just that you should be able to. Unfortunately I don't have a compiler until I get home this evening, but I am pretty sure I have already tried code similar to that. Either way though the keys shouldn't currently be working in the main loop either if your code is the fix. I want to know why keys work in the main loop, but not in the function.
Because you only update the array right after an event comes in. At all other times, it doesn't reflect the current state of the keyboard. If you want to use polling-based input, you should call SDL_GetKeyState every single iteration before querying the returned array.

This topic is closed to new replies.

Advertisement