# SDL\_GetKeyState() and Uint?

**URL:** <https://discourse.libsdl.org/t/sdl-getkeystate-and-uint/3949>\
**Category:** SDL Development\
**Created:** [February 19, 2001, 1:55pm UTC](https://discourse.libsdl.org/t/sdl-getkeystate-and-uint/3949 "2001-02-19T13:55:48Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sylvain.Hellegouarch](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/f17d59/32.png) [@Sylvain.Hellegouarch](https://discourse.libsdl.org/u/Sylvain.Hellegouarch)\
**Post date:** [February 19, 2001, 1:55pm UTC](https://discourse.libsdl.org/t/sdl-getkeystate-and-uint/3949/1 "2001-02-19T13:55:48Z")

</div>

Hi,

I’m using the SDL\_GetKeyState() in a bomberman like (another one… but  
it’s just for me and my friends which like drink beers each time they  
loose…) anyway, I’m using it and I’m really disapointed as it returns  
a Uint8\*, and so the state of the keypad and the arrows are not provided  
with this returns… so instead of Uin8\* I wonder if this function  
couldn’t return a Uint16\* which would be much convenient…

I looked to the SDL sources and I think it woundln’t change so many  
things…

Bye  
Lawouach

---

<div class="post-metadata">

**Author:** ![Mattias\_Engdegard](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/49beb7/32.png) [@Mattias\_Engdegard](https://discourse.libsdl.org/u/Mattias_Engdegard)\
**Post date:** [February 19, 2001, 4:03pm UTC](https://discourse.libsdl.org/t/sdl-getkeystate-and-uint/3949/2 "2001-02-19T16:03:38Z")

</div>

> I’m using the SDL\_GetKeyState() in a bomberman like (another one… but  
> it’s just for me and my friends which like drink beers each time they  
> loose…) anyway, I’m using it and I’m really disapointed as it returns  
> a Uint8\*, and so the state of the keypad and the arrows are not provided  
> with this returns… so instead of Uin8\* I wonder if this function  
> couldn’t return a Uint16\* which would be much convenient…

the key state vector is indexed by key sym number (SDLK\_foo), not  
by ascii or unicode

to anticipate your unasked question, don’t forget to pump events

---

<div class="post-metadata">

**Author:** ![Sylvain.Hellegouarch](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/f17d59/32.png) [@Sylvain.Hellegouarch](https://discourse.libsdl.org/u/Sylvain.Hellegouarch)\
**Post date:** [February 20, 2001, 9:05am UTC](https://discourse.libsdl.org/t/sdl-getkeystate-and-uint/3949/3 "2001-02-20T09:05:03Z")

</div>

Mattias Engdeg?rd wrote:

> > I’m using the SDL\_GetKeyState() in a bomberman like (another one… but  
> > it’s just for me and my friends which like drink beers each time they  
> > loose…) anyway, I’m using it and I’m really disapointed as it returns  
> > a Uint8\*, and so the state of the keypad and the arrows are not provided  
> > with this returns… so instead of Uin8\* I wonder if this function  
> > couldn’t return a Uint16\* which would be much convenient…
> 
> the key state vector is indexed by key sym number (SDLK\_foo), not  
> by ascii or unicode
> 
> to anticipate your unasked question, don’t forget to pump events

I’m neither a fool nor a stupid guy ! 😕

I know that this function returns keysym number but just look at the  
SDL\_keysym.h and you’ll see that lots of keysym are bigger that 255 and  
can’t be stored in a Uint8 array… so lots of keys aren’t return within  
this function… moreover I put that fukin’ (sqz) function into the main  
event loop so the the call to SDL\_PumpEvent() is done…

Bye  
Lawouach

---

<div class="post-metadata">

**Author:** ![Dominique\_Biesmans](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/d/8797f3/32.png) [@Dominique\_Biesmans](https://discourse.libsdl.org/u/Dominique_Biesmans)\
**Post date:** [February 20, 2001, 10:22am UTC](https://discourse.libsdl.org/t/sdl-getkeystate-and-uint/3949/4 "2001-02-20T10:22:12Z")

</div>

> I’m neither a fool nor a stupid guy ! 😕

But you didn’t read the docs, did you.

\</quote doc\>  
Uint8 \*SDL\_GetKeyState(int \*numkeys);

Gets a snapshot of the current keyboard state. The current state is return  
as a pointer to an array, the size of this array is stored in numkeys. The  
array is indexed by the SDLK\_\* symbols. A value of 1 means the key is  
pressed and a value of 0 means its not.  
\</quote doc\>

So the array can have values that vary between 0 and 1. I don’t think you  
need a Uint16 to store a 0 or a 1, do you?

\</hint mode\>  
Uint8 \*keystate = SDL\_GetKeyState(NULL);  
// check for the keypad ‘up’ key  
if (keystate[SDLK\_KP8])  
{  
// key is pressed  
}  
\</hint mode\>

> [snip]  
> this function… moreover I put that fukin’ (sqz) function  
> [snip]

Another hint: try to be a little more friendly when you’re trying to ask  
something. Refering to free & cool software like SDL, as “fuckin whatever”,  
won’t increase the chances someone replies to your question…\> -----Original Message-----

> From: Sylvain.Hellegouarch

---

<div class="post-metadata">

**Author:** ![Mattias\_Engdegard](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/49beb7/32.png) [@Mattias\_Engdegard](https://discourse.libsdl.org/u/Mattias_Engdegard)\
**Post date:** [February 20, 2001, 10:38am UTC](https://discourse.libsdl.org/t/sdl-getkeystate-and-uint/3949/5 "2001-02-20T10:38:12Z")

</div>

> I know that this function returns keysym number but just look at the  
> SDL\_keysym.h and you’ll see that lots of keysym are bigger that 255 and  
> can’t be stored in a Uint8 array… so lots of keys aren’t return within  
> this function… moreover I put that fukin’ (sqz) function into the main  
> event loop so the the call to SDL\_PumpEvent() is done…

rtfm. SDL\_GetKeyState() returns a pointer to a vector _indexed_ by  
keysym where each element is 1 if the key is pressed, 0 if not  
i.e.

Uint8 \*keystate = SDL\_GetKeyState();  
if(keystate[SDLK\_RIGHT])  
move\_to\_the\_right();

note that you don’t have to use SDL\_GetKeyState() — you can look at the  
keypress/release events and keep track of which keys are pressed yourself

> I’m neither a fool nor a stupid guy ! 😕

we don’t think you are, but even clever people can misunderstand things

---

<div class="post-metadata">

**Author:** ![Sylvain.Hellegouarch](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/f17d59/32.png) [@Sylvain.Hellegouarch](https://discourse.libsdl.org/u/Sylvain.Hellegouarch)\
**Post date:** [February 20, 2001, 12:45pm UTC](https://discourse.libsdl.org/t/sdl-getkeystate-and-uint/3949/6 "2001-02-20T12:45:59Z")

</div>

Dominique Biesmans wrote:

> > I’m neither a fool nor a stupid guy ! 😕
> 
> But you didn’t read the docs, did you.
> 
> \</quote doc\>  
> Uint8 \*SDL\_GetKeyState(int \*numkeys);
> 
> Gets a snapshot of the current keyboard state. The current state is return  
> as a pointer to an array, the size of this array is stored in numkeys. The  
> array is indexed by the SDLK\_\* symbols. A value of 1 means the key is  
> pressed and a value of 0 means its not.  
> \</quote doc\>
> 
> So the array can have values that vary between 0 and 1. I don’t think you  
> need a Uint16 to store a 0 or a 1, do you?
> 
> \</hint mode\>  
> Uint8 \*keystate = SDL\_GetKeyState(NULL);  
> // check for the keypad ‘up’ key  
> if (keystate[SDLK\_KP8])  
> {  
> // key is pressed  
> }  
> \</hint mode\>
> 
> > [snip]  
> > this function… moreover I put that fukin’ (sqz) function  
> > [snip]
> 
> Another hint: try to be a little more friendly when you’re trying to ask  
> something. Refering to free & cool software like SDL, as “fuckin whatever”,  
> won’t increase the chances someone replies to your question…

Thanks for being cool with me and really sorry to be little nervous, I  
didn’t want to be so agressive

Bye guys  
Lawouach\> \> -----Original Message-----

> > From: Sylvain.Hellegouarch

---

<div class="post-metadata">

**Author:** ![David\_Olofson](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/david_olofson/32/115_2.png) [@David\_Olofson](https://discourse.libsdl.org/u/David_Olofson)\
**Post date:** [February 20, 2001, 6:56pm UTC](https://discourse.libsdl.org/t/sdl-getkeystate-and-uint/3949/7 "2001-02-20T18:56:15Z")

</div>

Is that a very good idea, actually?

I’m thinking about things like the application losing focus and thus missing  
some up/down events - if SDL was to do anything clever about that, it would  
also have to make sure to mirror the clever adjustments in the keystate array  
by “emulating” the corresponding key events, or these two methods would  
produce different results…

//David

.- M A I A -------------------------------------------------.  
| Multimedia Application Integration Architecture |  
| A Free/Open Source Plugin API for Professional Multimedia |  
`----------------------> http://www.linuxaudiodev.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter | `--------------------------------------\> david at [linuxdj.com](http://linuxdj.com) -'On Tuesday 20 February 2001 11:38, Mattias Engdeg?rd wrote:

> note that you don’t have to use SDL\_GetKeyState() — you can look at the  
> keypress/release events and keep track of which keys are pressed yourself

---

<div class="post-metadata">

**Author:** ![Martin\_Donlon](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@Martin\_Donlon](https://discourse.libsdl.org/u/Martin_Donlon)\
**Post date:** [February 21, 2001, 12:50am UTC](https://discourse.libsdl.org/t/sdl-getkeystate-and-uint/3949/8 "2001-02-21T00:50:13Z")

</div>

The array SDL\_GetKeyState returns is kept updated internally withing the  
SDL\_PrivateKeyboard function. SDL\_PrivateKeyboard is also responsible  
for posting SDL keyboard events at the same time, so the information in  
the SDL\_GetKeyState array the any information obtained from SDL keyboard  
events is always identical.On Tue, Feb 20, 2001 at 07:56:15PM +0100, David Olofson wrote:

> On Tuesday 20 February 2001 11:38, Mattias Engdeg?rd wrote:
> 
> > note that you don’t have to use SDL\_GetKeyState() — you can look at the  
> > keypress/release events and keep track of which keys are pressed yourself
> 
> Is that a very good idea, actually?
> 
> I’m thinking about things like the application losing focus and thus missing  
> some up/down events - if SDL was to do anything clever about that, it would  
> also have to make sure to mirror the clever adjustments in the keystate array  
> by “emulating” the corresponding key events, or these two methods would  
> produce different results…

## – Martin

Bother! said Pooh, as Will Riker attempted to mate with him.

---

<div class="post-metadata">

**Author:** ![David\_Olofson](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/david_olofson/32/115_2.png) [@David\_Olofson](https://discourse.libsdl.org/u/David_Olofson)\
**Post date:** [February 21, 2001, 1:53am UTC](https://discourse.libsdl.org/t/sdl-getkeystate-and-uint/3949/9 "2001-02-21T01:53:34Z")

</div>

Is that behavior guaranteed in the API spec? 🙂

(I wasn’t expecting that it would work any other way; just pointing out that  
it’s theoretically possible that it could change some time for some reason.)

//David

.- M A I A -------------------------------------------------.  
| Multimedia Application Integration Architecture |  
| A Free/Open Source Plugin API for Professional Multimedia |  
`----------------------> http://www.linuxaudiodev.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter | `--------------------------------------\> david at [linuxdj.com](http://linuxdj.com) -'On Wednesday 21 February 2001 01:50, Martin J Donlon wrote:

> On Tue, Feb 20, 2001 at 07:56:15PM +0100, David Olofson wrote:
> 
> > On Tuesday 20 February 2001 11:38, Mattias Engdeg?rd wrote:
> > 
> > > note that you don’t have to use SDL\_GetKeyState() — you can look at  
> > > the keypress/release events and keep track of which keys are pressed  
> > > yourself
> > 
> > Is that a very good idea, actually?
> > 
> > I’m thinking about things like the application losing focus and thus  
> > missing some up/down events - if SDL was to do anything clever about  
> > that, it would also have to make sure to mirror the clever adjustments in  
> > the keystate array by “emulating” the corresponding key events, or these  
> > two methods would produce different results…
> 
> The array SDL\_GetKeyState returns is kept updated internally withing the  
> SDL\_PrivateKeyboard function. SDL\_PrivateKeyboard is also responsible  
> for posting SDL keyboard events at the same time, so the information in  
> the SDL\_GetKeyState array the any information obtained from SDL keyboard  
> events is always identical.

---

<div class="post-metadata">

**Author:** ![Martin\_Donlon](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@Martin\_Donlon](https://discourse.libsdl.org/u/Martin_Donlon)\
**Post date:** [February 21, 2001, 2:38pm UTC](https://discourse.libsdl.org/t/sdl-getkeystate-and-uint/3949/10 "2001-02-21T14:38:35Z")

</div>

> > The array SDL\_GetKeyState returns is kept updated internally withing the  
> > SDL\_PrivateKeyboard function. SDL\_PrivateKeyboard is also responsible  
> > for posting SDL keyboard events at the same time, so the information in  
> > the SDL\_GetKeyState array the any information obtained from SDL keyboard  
> > events is always identical.
> 
> Is that behavior guaranteed in the API spec? 🙂  
> Spec?

> (I wasn’t expecting that it would work any other way; just pointing out that  
> it’s theoretically possible that it could change some time for some reason.)  
> Well any other way would be broken.  
> However, I am curious how SDL handles key releases when the app doesn’t  
> have focus. Sam?On Wed, Feb 21, 2001 at 02:53:34AM +0100, David Olofson wrote:

## – Martin

Bother! said Pooh, as he turned into a bat.

---

<div class="post-metadata">

**Author:** ![slouken](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/ec9cab/32.png) [@slouken](https://discourse.libsdl.org/u/slouken)\
**Post date:** [February 21, 2001, 6:05pm UTC](https://discourse.libsdl.org/t/sdl-getkeystate-and-uint/3949/11 "2001-02-21T18:05:10Z")

</div>

> However, I am curious how SDL handles key releases when the app doesn’t  
> have focus. Sam?

When the app loses focus, all keys are released.  
When the app gains focus, the current key state is queried if possible,  
and the keys that are still down generate key press events. If it’s not  
possible to detect the current key state, the keys are left released, and  
the next time they are pressed the keyboard state is in sync again.

It’s not necesarily the best way to do things, but it’s the cleanest  
portable way I could think of.

See ya!  
-Sam Lantinga, Lead Programmer, Loki Entertainment Software

---

<div class="post-metadata">

**Author:** ![David\_Olofson](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/david_olofson/32/115_2.png) [@David\_Olofson](https://discourse.libsdl.org/u/David_Olofson)\
**Post date:** [February 21, 2001, 9:32pm UTC](https://discourse.libsdl.org/t/sdl-getkeystate-and-uint/3949/12 "2001-02-21T21:32:35Z")

</div>

Well, if there _is_ anything more than the man page and the code comments…

//David

.- M A I A -------------------------------------------------.  
| Multimedia Application Integration Architecture |  
| A Free/Open Source Plugin API for Professional Multimedia |  
`----------------------> http://www.linuxaudiodev.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter | `--------------------------------------\> david at [linuxdj.com](http://linuxdj.com) -'On Wednesday 21 February 2001 15:38, Martin J Donlon wrote:

> On Wed, Feb 21, 2001 at 02:53:34AM +0100, David Olofson wrote:
> 
> > > The array SDL\_GetKeyState returns is kept updated internally withing  
> > > the SDL\_PrivateKeyboard function. SDL\_PrivateKeyboard is also  
> > > responsible for posting SDL keyboard events at the same time, so the  
> > > information in the SDL\_GetKeyState array the any information obtained  
> > > from SDL keyboard events is always identical.
> > 
> > Is that behavior guaranteed in the API spec? 🙂
> 
> Spec?

---

<div class="post-metadata">

**Author:** ![slouken](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/s/ec9cab/32.png) [@slouken](https://discourse.libsdl.org/u/slouken)\
**Post date:** [February 22, 2001, 6:18am UTC](https://discourse.libsdl.org/t/sdl-getkeystate-and-uint/3949/13 "2001-02-22T06:18:01Z")

</div>

> Well, if there _is_ anything more than the man page and the code comments…

There isn’t a formal spec. SDL sort of evolved. If someone has enough time,  
I’ll be happy to help them whip up one based on the SDL documentation project.

See ya,  
-Sam Lantinga, Lead Programmer, Loki Entertainment Software

---

<div class="post-metadata">

**Author:** ![Martin\_Donlon](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@Martin\_Donlon](https://discourse.libsdl.org/u/Martin_Donlon)\
**Post date:** [February 22, 2001, 12:10pm UTC](https://discourse.libsdl.org/t/sdl-getkeystate-and-uint/3949/14 "2001-02-22T12:10:27Z")

</div>

Hmmm…I was gonna say that would be a bad idea since I’m sure there are  
still many areas in the bowels of SDLdoc that are just plain wrong.  
However, if someone was to write a spec around SDLdoc then I’m sure  
they would have to give SDLdoc a good thorough proof-reading:)  
So, I’ll be happy to help anyone out who would like to take up the  
project.On Wed, Feb 21, 2001 at 10:18:01PM -0800, Sam Lantinga wrote:

> > Well, if there _is_ anything more than the man page and the code comments…
> 
> There isn’t a formal spec. SDL sort of evolved. If someone has enough time,  
> I’ll be happy to help them whip up one based on the SDL documentation project.
> 
> See ya,  
> -Sam Lantinga, Lead Programmer, Loki Entertainment Software

## – Martin

Bother, said Pooh, and turned the launch key.
