Keyboard trouble

Hi,

I must be stupid, but I’m having trouble with the following, used on
linux (X and rendering in a window):

Uint8* keystate;

keystate = SDL_GetKeyState(NULL);

if (keystate[SDLK_q])
  run = 0;

It seems that keystate never changes. Is the above code wrong, or
does something have to be set up for this to work? (BTW, I tried it
on different SDL versions / linux systems, no go.) Note that
SDL_PollEvent() and SDL_WaitEvent() do return the correct keyboard
events.

I was trying to get the ‘real’ keyboard state (without autorepeat
troubling the water), and was hoping to find this through
SDL_GetKeyState().

Thanks,

  • Reinoud

I must be stupid, but I’m having trouble with the following, used on
linux (X and rendering in a window):

Uint8* keystate;

keystate = SDL_GetKeyState(NULL);
if (keystate[SDLK_q])
  run = 0;

Since asynchronous events were disabled, you need to periodically
call SDL_PollEvent() or SDL_WaitEvent() to trigger the event collection.
If you don’t want to actually receive keyboard events, you can “ignore”
them with SDL_EventState(), but you still need to poll for events to
trigger the keyboard state update.

There is an example of this (tested) in the examples for SDL events
in the online documentation:

http://www.devolution.com/~slouken/SDL/docs-0.7/events/examples.html

See ya!
-Sam Lantinga (slouken at devolution.com)–
Author of Simple DirectMedia Layer -
http://www.devolution.com/~slouken/SDL/

Sam Lantinga wrote:

Since asynchronous events were disabled, you need to periodically
call SDL_PollEvent() or SDL_WaitEvent() to trigger the event collection.

Thanks! It works (of course).

However, the key state as reported through SDL_GetKeyState() also suffers
from keyboard autorepeat… Is there a way to observe the ‘physical’ state
of keys? That would seem quite important for games :-).

  • Reinoud

However, the key state as reported through SDL_GetKeyState() also suffers
from keyboard autorepeat… Is there a way to observe the ‘physical’ state
of keys? That would seem quite important for games :-).

How does it suffer from autorepeat?

See ya!
-Sam Lantinga (slouken at devolution.com)–
Author of Simple DirectMedia Layer -
http://www.devolution.com/~slouken/SDL/

Sam Lantinga wrote:

However, the key state as reported through SDL_GetKeyState() also suffers
from keyboard autorepeat… Is there a way to observe the ‘physical’ state
of keys? That would seem quite important for games :-).

How does it suffer from autorepeat?

In that keys seem to toggle (at autorepeat rate), while physically they are
not. I have to turn off autorepeat each time now, for the entire console;
otherwise the controls for the SDL app are stuttering (while flying through
voxel mountains, can’t have that:).

Of course, if through X one cannot distinguish between the physical and
logical state of keys (I wouldn’t know), then there is no choice but to
turn off autorepeat.

Groetjes,

  • Reinoud