Fast way of scanning keyboard input

If I want to to scan the state of the entire keyboard, is there a fast or
optimal way of writing this code with SDL? The only way I can figure this
out is to loop through each key of interest after getting the keyboard state
from SDL_GetKeyState(null). If I have to scan most or all keys on the
keyboard, it feels inefficient.

The reason why I have to do this is because I believe that getting the state
of the keyboard is more accurate that intercepting SDL events of key up or
key down messages. Certain keyboards fail to generate those key up or key
down messages. With the various keyboards I have tested with so far,
determining the rise and fall of keys seems to be more accurate by sampling
over time the state of the keyboard with SDL_GetKeyState(null). Are these
assumptions correct?

Wesley_________________________________________________________________
Add photos to your messages with MSN 8. Get 2 months FREE*.
http://join.msn.com/?page=features/featuredemail

If I want to to scan the state of the entire keyboard, is there a fast or
optimal way of writing this code with SDL? The only way I can figure this
out is to loop through each key of interest after getting the keyboard state
from SDL_GetKeyState(null). If I have to scan most or all keys on the
keyboard, it feels inefficient.

If you know what keys you are interested in, you can dereference them
directly.

If you have to iterate over the whole array, it’s a vector of Uint8’s,
so you could be tricky and treat it as an array of Uint32’s and handle 4
keys at once, or use Altivec/SSE/whatever and handle 16 at once.

In practice, even a for-loop probably won’t be a huge CPU killer, but it’s
frequently not the right way to do things.

But, see below:

The reason why I have to do this is because I believe that getting the state
of the keyboard is more accurate that intercepting SDL events of key up or
key down messages. Certain keyboards fail to generate those key up or key
down messages. With the various keyboards I have tested with so far,
determining the rise and fall of keys seems to be more accurate by sampling
over time the state of the keyboard with SDL_GetKeyState(null). Are these
assumptions correct?

Not really; the array that GetKeyState() returns is maintained by the code
that feeds your application key events, so it’s unlikely to be out of
sync, as long as you pump the event loop regularly.

–ryan.

If I have to scan most or all keys on the keyboard, it feels
inefficient.

Whoa. Don’t get yourself tangled up in premature optimization.
Scanning an array of 256 values is nothing compared to the amount of
code that was likely executed in order to keep that array updated.

The reason why I have to do this is because I believe that getting
the state of the keyboard is more accurate that intercepting SDL
events of key up or key down messages. […] Are these assumptions
correct?

No. Checking the keyboard array is going to be no more or less
accurate than intercepting SDL events. You should use whichever
approach works best for what you’re trying to do with the keyboard.

b