Save keys as Uint8?

Hello

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.

I am a beginner, so please…

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

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

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 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