# Save keys as Uint8?

**URL:** <https://discourse.libsdl.org/t/save-keys-as-uint8/9724>\
**Category:** SDL Development\
**Created:** [November 20, 2003, 5:59am UTC](https://discourse.libsdl.org/t/save-keys-as-uint8/9724 "2003-11-20T05:59:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Johan\_Ronstrom](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@Johan\_Ronstrom](https://discourse.libsdl.org/u/Johan_Ronstrom)\
**Post date:** [November 20, 2003, 5:59am UTC](https://discourse.libsdl.org/t/save-keys-as-uint8/9724/1 "2003-11-20T05:59:00Z")

</div>

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](http://mail.messenger.yahoo.co.uk)

---

<div class="post-metadata">

**Author:** ![Mikkel\_Gjol](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/f14d63/32.png) [@Mikkel\_Gjol](https://discourse.libsdl.org/u/Mikkel_Gjol)\
**Post date:** [November 21, 2003, 1:47am UTC](https://discourse.libsdl.org/t/save-keys-as-uint8/9724/2 "2003-11-21T01:47:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Johan\_Ronstrom](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/j/d07c76/32.png) [@Johan\_Ronstrom](https://discourse.libsdl.org/u/Johan_Ronstrom)\
**Post date:** [November 22, 2003, 7:41am UTC](https://discourse.libsdl.org/t/save-keys-as-uint8/9724/3 "2003-11-22T07:41:01Z")

</div>

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](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://libsdl.org)  
> [http://www.libsdl.org/mailman/listinfo/sdl](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](http://mail.messenger.yahoo.co.uk)
