[SDL] Save keys as Uint8?

Mikkel Gjøl s971661 at student.dtu.dk
Fri Nov 21 01:47:00 PST 2003

Johan Ronström wrote:
 > I want to make my program able to save pressed keys, but not as
 > "SDLK_*" but as
 > Uint8 (i.e. save key bindings in options menu, and use in my game), as
 > I don't like strings in c++.
 > Can anyone show me how I to store a pressed key in a Uint8?
 > I know that SDL_GetKeyState is of type Uint8, but how do you get past
 > the index and that.


Another way that is probably preferable, is to process the events that
SDL pass on to you, using either SDL_PollEvent() or another way to
retrieve events (read the docs). This will also give you the modifier
presses. The events return a 'SDLKey', which is an enum with values from
0-322, so you need at least an uint16 to hold the value. You might do
something like this, after which the currently pressed keys will reside
in 'keys'.
(...or use another structure for keys that would enable you to traverse
it faster afterwards)

SDL_Event event;
uint16 keys[512];

while( SDL_PollEvent( &event )) {
  switch( event.type ) {
  case SDL_KEYDOWN: {
   keys[event.key.keysym.sym] = 1;

  case SDL_KEYUP: {
   keys[event.key.keysym.sym] = 0;	

hope it helps,
\\Mikkel Gjoel

More information about the SDL mailing list