# SDL and updating keyboard

This topic is 4721 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello. I'm having trouble. When I use SDL the way I get the keystate is by updating the keys every frame and then testing the key value by using a simple if statement: if(keys[SDLK_RETURN]) { }. The problem is that if I hit the keys it makes it appear like I hit it more then once due to the speed of which it is looping. Ideas? Thanks.

##### Share on other sites
look into the function :

SDL_EnableKeyRepeat(X, Y)

clicky

-ij

##### Share on other sites
It didn't appear to work.

I set the value of delay to 0 to disable key repeat and the interval at 300 but it still was repeating.

##### Share on other sites
All you really need to do is clear the key after you press it if you want a 'one action per press'
if( keys[SDLK_RETURN] ){   ...   keys[SDLK_RETURN] = 0;}

##### Share on other sites
For when you need to do something on key-press, just handle a SDL_KEYDOWN event.

key-state arrays better for when you need to know something like "Is the G key being held down right now?"

##### Share on other sites
Hi! I don't know if people are still looking at this thread, but I have a similar issue, so ...

I am making atop-down car sim game with openGL. I want the car-move-forward action while the 'up' key is pressed, car-turn action when the left/right keys are pressed. Now if the left/right keys are pressed and released while the up key is down, the car-move-forward action is stopped! And I don't want that - anyone who has played a car sim must know what I want, can you help me? My code:

SDL_EnableKeyRepeat(10, 10);// .....Uint8 *keys = SDL_GetKeyState(NULL);SDL_PumpEvents();      if (keys[SDLK_LEFT]) turn();if (keys[SDLK_RIGHT]) turn();if (keys[SDLK_UP]) carmoveforward();

##### Share on other sites
Quote:
 Original post by deavikHi! I don't know if people are still looking at this thread, but I have a similar issue, so ...I am making atop-down car sim game with openGL. I want the car-move-forward action while the 'up' key is pressed, car-turn action when the left/right keys are pressed. Now if the left/right keys are pressed and released while the up key is down, the car-move-forward action is stopped! And I don't want that - anyone who has played a car sim must know what I want, can you help me? My code:*** Source Snippet Removed ***

I would guess (completely unverified) that the event queue is filling up since from what you've posted you don't seem to remove anything from it (I'm assuming it has a max size).

I would suggest
replacingSDL_PumpEvents();withSDL_Event event;while(SDL_PollEvent(&event)); //this will clear out the buffer

This isn't the best solution since there a several event's that you shouldn't be ignoring, (like SDL_QUIT). But you put the stuff for handling that into the while loop. Also I don't think you need SDL_EnableKeyRepeat.

##### Share on other sites
Hi Cocalus! Thanks for writing in! I tried what you said + disabled SDL_EnableKeyRepeat, but that just works to move stuff one time. But I need the effect while the key is pressed. Also I tried SDL_PRESSED instead of SDL_KEYDOWN, didn't work. Enabling SDL_EnableKeyRepeat and trying the same results in the same problem as I had at first. Have you any more ideas?

while (SDL_PollEvent(&event))     {       switch (event.type)       {            case SDL_KEYDOWN:               if(event.key.keysym.sym==SDLK_LEFT)                  keyPresses(R_z, 0.5f);               if(event.key.keysym.sym==SDLK_RIGHT)                  keyPresses(R_z, -0.5f);          break;                         case SDL_QUIT:            done=1;      }    }

##### Share on other sites
If you want something to happen continuously while the key is depressed, SDL_GetKeyState is what you want. If you only want something to happen at the precise moment that the key is depressed or released, then you use the event queue.

##### Share on other sites
Hi Kylotan, SDL_GetKeyState is what I did use initially (check a couple of posts back). The problem was the same - it seems to not be able to handle multiple keypresses ... any ideas?

• 10
• 17
• 9
• 13
• 41