SDL_event KEYDOWN behavior issue

Here is the problem, I wrote an event loop to detect keydown and keyup events. The problem I am facing is that the keydown event generates a keydown and a keyup event when a key is pressed and held down. I use the arrow keys to move the object, and then to stop moving when the key is released (keyup). Any help would help. Thank you =)

Justin

PS I would post the code, but I can't make it look right.

print(" SDL_Event event; SDL_EnableKeyRepeat(0,0); while(SDL_PollEvent(&event)){ switch(event.type){ case SDL_QUIT: done = true; break; case SDL_KEYDOWN: switch(event.key.keysym.sym){ case SDLK_ESCAPE: done = true; break; case SDLK_LEFT: animate_x = -5; cout << "left press\n"; break; case SDLK_RIGHT: animate_x = 5; break; case SDLK_UP: animate_y = -5; break; case SDLK_DOWN: animate_y = 5; break; default: break; } break; -left out in original case SDL_KEYUP: switch(event.key.keysym.sym){ case SDLK_LEFT: cout << "left up\n"; animate_x = 0; break; case SDLK_RIGHT: animate_x = 0; break; case SDLK_UP: animate_y = 0; break; case SDLK_DOWN: animate_y = 0; break; default: break; } break; -left out in original } }"); 

When trying to figure out how to send the code, I noticed that in two cases I refused to default. The code now works. He continued to look through cases and execute code that corresponded to what was in the queue. I was a fool. Thanks for the help. =)

+3
source share
2 answers

It looks like you have enabled key repetition. To disable it, use

 SDL_EnableKeyRepeat(0, 0); 
+3
source

You might want to use SDL_GetKeyState instead of tracking keydown / keyup; I use it to detect the momentary state of keys, which you can use to determine if keys are held on subsequent frames.

+3
source

All Articles