[SDL] Save keys as Uint8?

Johan Ronström smekis at yahoo.com
Sat Nov 22 07:41:01 PST 2003


I solved my problem by creating a global 'extern SDLKey myKey' and set it to
the key pressed (in my file containing my menu() ), and then in my other file
(wich contains gameplay() ) using something like:

.  // doc-example...
.
while(SDL_PollEvent(&event)){
  switch(event.type){
    case SDL_KEYDOWN:
      if(event.key.keysym.sym==myKey) // ...using myKey
        move_left();
      break;
    .
    .
    .
  }
}
.
.


Is there any downsides with this (exept having to use globals...)?

My code works perfectly, so far.

Thanks! / Johan

 --- Mikkel_Gjøl <s971661 at student.dtu.dk> wrote: > 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.
> 
> http://sdldoc.csn.ul.ie/sdlgetkeystate.php
> 
> 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;
>   }break;
> 
>   case SDL_KEYUP: {
>    keys[event.key.keysym.sym] = 0;	
>   }break;
>   }
> }
> 
> 
> hope it helps,
> \\Mikkel Gjoel
> 
> 
> _______________________________________________
> SDL mailing list
> SDL at libsdl.org
> http://www.libsdl.org/mailman/listinfo/sdl 

________________________________________________________________________
Want to chat instantly with your online friends?  Get the FREE Yahoo!
Messenger http://mail.messenger.yahoo.co.uk




More information about the SDL mailing list