Talk:SDL:Tutorials:Practical Keyboard Input
From GDWiki
There is another way to do this stuff.
Uint8 *keys; keys = SDL_GetKeyState(NULL);
For instance if this is called when the key ESCAPE is pressed then keys[SDLK_ESCAPE] will be true.
[edit] Different way to do this
The other way seems easier and simpler. It should be put in the article itself too.
// Declare it Uint8* keystates; // Initialize it keystates = SDL_GetKeyState(0); // or SDL_GetKeyState(NULL) // Then in the loop, use it if(keystates[SDLK_UP]) { //move up at a constant rate y--; }
[edit] SDL_GetKeyState may be insufficient
The example using SDL_GetKeyState is certainly simpler, and in many cases might be just what the doctor ordered, but I'd like to point out that if the user presses and releases a key between the calls to SDL_GetKeyState, all information about that particular keypress is lost. Thus, if the framerate gets bad enough or the user has amazing lightning-fast keyboarding ability (more likely the former, I'd reckon), SDL_GetKeyState might not produce the desired results. On the other hand, the event-based mechanism would still register the keypress as a pair of SDL_KEYDOWN-SDL_KEYUP events.
Of course, in this particular example it makes no difference, since the exemplary event-based function would simply ignore such quick keypresses as well, and the simpler solution is indeed more elegant. However, if the simpler implementation gets inserted into the article, it should be pointed out that events should still be used for situations in which the fact that a key was pressed is important, e.g. when the player fires his gun in a first-person shooter. Note that holding down the key could still have additional effects, say, if you're shooting with an assault rifle, holding down the key could fire in burst-mode, but even in this case quickly tapping the key for a single shot should definitely not be ignored.

