SDL Keystate help

Started by
6 comments, last by rip-off 12 years, 7 months ago
[font=arial, verdana, tahoma, sans-serif][size=2]
Uint8 *keystate = SDL_GetKeyState(NULL);

while(quit != true)[/font]
{
{
if(keystate[SDLK_DOWN])
{
paddle_y = paddle_y + 1;
if(paddle_y > 500){paddle_y = 500;}
apply_surface(paddle_y);
}}


for some reason my keystates isnt working, i could make this work using "sdl_pollevent" but i would like it so that as long as user is holding the down key my function runs. I am doing this to make a paddle in my pong game go down, or if there is a better way to handle continuous inut, plz tell me how.
Help would be gratly appreciated
thanks
Advertisement
Try doing:
SDL_GetKeyState

Inside of your while loop so it gets the keystate every frame.
I think you need to use SDL_PumpEvents each time the loop runs
SDL_GetKeyState returns the same array every time so it doesn't matter if you call it inside or outside the loop.

zacaj is right. Normally when you use SDL_PollEvent or SDL_WaitEvent you don't need to call SDL_PumpEvents because these functions calls SDL_PumpEvents on their own. Here it is needed or otherwise the key state will never update.

SDL_GetKeyState returns the same array every time so it doesn't matter if you call it inside or outside the loop.

zacaj is right. Normally when you use SDL_PollEvent or SDL_WaitEvent you don't need to call SDL_PumpEvents because these functions calls SDL_PumpEvents on their own. Here it is needed or otherwise the key state will never update.


Thanks for the reply guys, can you tell me how to use Pump event or give me a link explaining it, because i cant seem to find a decent tutorial on it
The SDL documentation wiki has some information: http://www.libsdl.org/cgi/docwiki.cgi/SDL_PumpEvents
Thank you guys so much, i finally got my paddle working, what i did was i put SDL_Pumpevents as soon as the loop starts and then inside i have my if statements. Thanks again :D
You'll need to consume events from the event queue or the OS may assume your application is unresponsive. At the very least, you'll want to be processing SDL_QUIT events too.

This topic is closed to new replies.

Advertisement