# Taking input

**URL:** <https://discourse.libsdl.org/t/taking-input/8477>\
**Category:** SDL Development\
**Created:** [March 27, 2003, 3:55pm UTC](https://discourse.libsdl.org/t/taking-input/8477 "2003-03-27T15:55:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christopher\_Thielen](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/c/ce73a5/32.png) [@Christopher\_Thielen](https://discourse.libsdl.org/u/Christopher_Thielen)\
**Post date:** [March 27, 2003, 3:55pm UTC](https://discourse.libsdl.org/t/taking-input/8477/1 "2003-03-27T15:55:01Z")

</div>

I’m working on a simple text entry box and I’m using the SDL\_Event  
structure to obtain input. From experience, what is the best way to  
check for a key? Currently I’m using bitfields to remember which keys  
were pressed, and on KEYUP events, I check to see if the key that was  
released was previously down, if so, that’s a keypress, and I can take  
it as input. Is there any better way than this? The way I just described  
is two or three hundred lines and kinda ugly. Any thoughts? I also run  
into the problem, using the largest bitfield, Uint32, that I only have  
32 bits to set, but all the letters, plus the 10 numbers, backspace,  
spacebar, left shift, right shift, caps, etc. adds up to a lot more than  
32 bits needed to remember that all. What should I do?

– chris (@Christopher_Thielen)

---

<div class="post-metadata">

**Author:** ![Owen\_Butler](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/o/a183cd/32.png) [@Owen\_Butler](https://discourse.libsdl.org/u/Owen_Butler)\
**Post date:** [March 27, 2003, 4:29pm UTC](https://discourse.libsdl.org/t/taking-input/8477/2 "2003-03-27T16:29:02Z")

</div>

[http://sdldoc.csn.ul.ie/sdlgetkeystate.php](http://sdldoc.csn.ul.ie/sdlgetkeystate.php)

Regards,

Owen Butler\> ----- Original Message -----

> From: [sdl-admin@libsdl.org](mailto:sdl-admin@libsdl.org) [mailto:sdl-admin at [libsdl.org](http://libsdl.org)]On Behalf Of  
> Christopher Thielen  
> Sent: Friday, 28 March 2003 10:53 AM  
> To: sdl at [libsdl.org](http://libsdl.org)  
> Subject: [SDL] taking input
> 
> I’m working on a simple text entry box and I’m using the SDL\_Event  
> structure to obtain input. From experience, what is the best way to  
> check for a key? Currently I’m using bitfields to remember which keys  
> were pressed, and on KEYUP events, I check to see if the key that was  
> released was previously down, if so, that’s a keypress, and I can take  
> it as input. Is there any better way than this? The way I just described  
> is two or three hundred lines and kinda ugly. Any thoughts? I also run  
> into the problem, using the largest bitfield, Uint32, that I only have  
> 32 bits to set, but all the letters, plus the 10 numbers, backspace,  
> spacebar, left shift, right shift, caps, etc. adds up to a lot more than  
> 32 bits needed to remember that all. What should I do?
> 
> – chris (chris at [luethy.net](http://luethy.net))
> 
> * * *
> 
> SDL mailing list  
> SDL at [libsdl.org](http://libsdl.org)  
> [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)

---

<div class="post-metadata">

**Author:** ![Bill\_Kendrick](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@Bill\_Kendrick](https://discourse.libsdl.org/u/Bill_Kendrick)\
**Post date:** [March 27, 2003, 4:30pm UTC](https://discourse.libsdl.org/t/taking-input/8477/3 "2003-03-27T16:30:00Z")

</div>

Why are you concerned with keyup events in text entry?

Look at any other application you’ve ever used… it doesn’t work that way :^)  
It gets the key the instant you depress it! ;^)

If you’re interested in key repeat, SDL can take care of this for you, too…

-bill!On Thu, Mar 27, 2003 at 03:52:51PM -0800, Christopher Thielen wrote:

> I’m working on a simple text entry box and I’m using the SDL\_Event  
> structure to obtain input. From experience, what is the best way to  
> check for a key? Currently I’m using bitfields to remember which keys  
> were pressed, and on KEYUP events,  
> –  
> bill at [newbreedsoftware.com](http://newbreedsoftware.com) Hire me!  
> [Bill Kendrick](http://newbreedsoftware.com/bill/)[Bill Kendrick | Résumé](http://newbreedsoftware.com/bill/resume/)

---

<div class="post-metadata">

**Author:** ![Bob\_Pendleton](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/b/9de053/32.png) [@Bob\_Pendleton](https://discourse.libsdl.org/u/Bob_Pendleton)\
**Post date:** [March 27, 2003, 5:32pm UTC](https://discourse.libsdl.org/t/taking-input/8477/4 "2003-03-27T17:32:05Z")

</div>

> I’m working on a simple text entry box and I’m using the SDL\_Event  
> structure to obtain input. From experience, what is the best way to  
> check for a key? Currently I’m using bitfields to remember which keys  
> were pressed, and on KEYUP events, I check to see if the key that was  
> released was previously down, if so, that’s a keypress, and I can take  
> it as input. Is there any better way than this? The way I just described  
> is two or three hundred lines and kinda ugly. Any thoughts? I also run  
> into the problem, using the largest bitfield, Uint32, that I only have  
> 32 bits to set, but all the letters, plus the 10 numbers, backspace,  
> spacebar, left shift, right shift, caps, etc. adds up to a lot more than  
> 32 bits needed to remember that all. What should I do?

The other guys have already posted solutions to you main problem. So  
I’ll just handle the other one. Use a byte array, or if you are using  
c++ a bool array to keep track of the state. It is not worth you time to  
save a few hundred bytes of memory by using bits instead of bytes. There  
is a good chance that the code needed to map the key events to specific  
bits takes up more memory that you are “saving” by using bit fields. Not  
to mention the code needed to extract, set, and clear bit fields.

Always do it the simplest way until you have proven that the simple way  
will not work.

```
		Bob PendletonOn Thu, 2003-03-27 at 17:52, Christopher Thielen wrote:

```

> – chris (chris at [luethy.net](http://luethy.net))
> 
> * * *
> 
> SDL mailing list  
> SDL at [libsdl.org](http://libsdl.org)  
> [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)  
> –  
> ±----------------------------------+

- Bob Pendleton: independent writer +
- and programmer. +
- email: Bob at [Pendleton.com](http://Pendleton.com) +  
±----------------------------------+

---

<div class="post-metadata">

**Author:** ![Jim](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/j/ac91a4/32.png) [@Jim](https://discourse.libsdl.org/u/Jim)\
**Post date:** [March 27, 2003, 6:40pm UTC](https://discourse.libsdl.org/t/taking-input/8477/5 "2003-03-27T18:40:01Z")

</div>

I’ll give you several simple lines of code that can greatly simplify that  
which you seem to have overcomplicated… hmm… but then again - looking at  
the code and the several layers of translation that happen to a key - I  
guess mine is as large, but 95% of it is in tables of structures … (  
keychar[key][modifier].data )

But really these are only of interest for a thing that tracks specific key  
states… which actually normal text has about 5 keys which affect the  
state… and of course any implementation will be only for your specific  
nationality… But - most of the code is on\_press based, not on\_release…

ALPHA\_LOCK // set default state of alpha keys  
NUM\_LOCK  
SHIFT // not (!) the state of ALPHA\_LOCK/NUM\_LOCK

- that is if alpha is on - shift effect is off
- if alpha is off - shift effect is on

CONTROL - well this applies naturally to all alpha-characters and maps the  
key to keyval - ‘a’ + 1, ctrl-@ is NUL.  
ALT - open for application overriding…

typedef unsigned long \_32;

// declare a set of flags…  
#define FLAGSET(v,n) \_32 (v)[((n)+31)/32]  
// set a single flag index  
#define SETFLAG(v,n) ( (v)[(n)\>\>5] |= 1 \<\< ( (n) & 31 ))  
// clear a single flag index  
#define RESETFLAG(v,n) ( (v)[(n)\>\>5] &= ~( 1 \<\< ( (n) & 31 ) ) )  
// test if a flags is set  
#define TESTFLAG(v,n) ( (v)[(n)\>\>5] & ( 1 \<\< ( (n) & 31 ) ) )  
// reverse a flag from 1 to 0 and vice versa  
#define TOGGLEFLAG(v,n) ( (v)[(n)\>\>5] ^= 1 \<\< ( (n) & 31 ))

FLAGSET( keyboard, 256 ); // declare a set of bits…

SETFLAG( keyboard, nKey ); // set a bit

if( TESTFLAG( keyboard, nKey ) ) // test a bit  
{  
…  
}

Hmm… clear as mud, right?\> ----- Original Message -----

> From: [chris@luethy.net](mailto:chris@luethy.net) (Christopher Thielen)  
> To:   
> Sent: Thursday, March 27, 2003 3:52 PM  
> Subject: [SDL] taking input
> 
> > I’m working on a simple text entry box and I’m using the SDL\_Event  
> > structure to obtain input. From experience, what is the best way to  
> > check for a key? Currently I’m using bitfields to remember which keys  
> > were pressed, and on KEYUP events, I check to see if the key that was  
> > released was previously down, if so, that’s a keypress, and I can take  
> > it as input. Is there any better way than this? The way I just described  
> > is two or three hundred lines and kinda ugly. Any thoughts? I also run  
> > into the problem, using the largest bitfield, Uint32, that I only have  
> > 32 bits to set, but all the letters, plus the 10 numbers, backspace,  
> > spacebar, left shift, right shift, caps, etc. adds up to a lot more than  
> > 32 bits needed to remember that all. What should I do?
> > 
> > – chris (chris at [luethy.net](http://luethy.net))
> > 
> > * * *
> > 
> > SDL mailing list  
> > SDL at [libsdl.org](http://libsdl.org)  
> > [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)

---

<div class="post-metadata">

**Author:** ![atrix2](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/a/a9a28c/32.png) [@atrix2](https://discourse.libsdl.org/u/atrix2)\
**Post date:** [March 28, 2003, 8:40am UTC](https://discourse.libsdl.org/t/taking-input/8477/6 "2003-03-28T08:40:03Z")

</div>

Hi Chris,

in my game im working on right now, i have it have duel input states. It  
can either work where i check what keys are down and do things accordingly  
(ie game control where multiple keys are held down at the same time) or  
where you can type, and it will take your keystrokes in the order you typed  
them (for input boxes like what your saying).

like owen pointed out, check out this link to be able to get a pointer to  
check what keys are down:

[http://sdldoc.csn.ul.ie/sdlgetkeystate.php](http://sdldoc.csn.ul.ie/sdlgetkeystate.php)

and for buffered keyboard input like for a text box, what i do is i have a  
32 character array with an Index. Whenever i get a keydown message, if the  
buffer isnt full (Index\<32), i add the key to the buffer at the current  
index and then increment the Index. So, when i am inputing into a text box  
i have a loop thats something like this psuedo code:

while(Index\>0)  
{  
if key is \b (backspace)  
remove a character from my text box  
if key is \r or \n (newline, enter)  
do something…like move to another text box if theres more than 1  
else if key \> 0x20 and key \< 0x7f (valid range for printable characters)  
put it into my text box

Index–;  
}

whenever i go into text box input mode (when the user clicks the text box  
lets say), i set Index to 0 so all previous keystrokes are ignored. The  
only problem i have with this design is that if you hold down the backspace  
key, it doesnt work like it normaly does when your typing. It will just  
delete one letter and stop. Im not sure how to get over this problem, but  
yesterday i saw some posts on SDL\_SetKeyRepeat or something like that which  
might be able to solve this issue.

Hope this helps,  
Atrix  
}\> ----- Original Message -----

> From: [chris@luethy.net](mailto:chris@luethy.net) (Christopher Thielen)  
> To:   
> Sent: Thursday, March 27, 2003 3:52 PM  
> Subject: [SDL] taking input
> 
> > I’m working on a simple text entry box and I’m using the SDL\_Event  
> > structure to obtain input. From experience, what is the best way to  
> > check for a key? Currently I’m using bitfields to remember which keys  
> > were pressed, and on KEYUP events, I check to see if the key that was  
> > released was previously down, if so, that’s a keypress, and I can take  
> > it as input. Is there any better way than this? The way I just described  
> > is two or three hundred lines and kinda ugly. Any thoughts? I also run  
> > into the problem, using the largest bitfield, Uint32, that I only have  
> > 32 bits to set, but all the letters, plus the 10 numbers, backspace,  
> > spacebar, left shift, right shift, caps, etc. adds up to a lot more than  
> > 32 bits needed to remember that all. What should I do?
> > 
> > – chris (chris at [luethy.net](http://luethy.net))
> > 
> > * * *
> > 
> > SDL mailing list  
> > SDL at [libsdl.org](http://libsdl.org)  
> > [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)

---

<div class="post-metadata">

**Author:** ![Christopher\_Thielen](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/c/ce73a5/32.png) [@Christopher\_Thielen](https://discourse.libsdl.org/u/Christopher_Thielen)\
**Post date:** [March 30, 2003, 3:11pm UTC](https://discourse.libsdl.org/t/taking-input/8477/7 "2003-03-30T15:11:01Z")

</div>

Heh, thanks everybody! I was able to simply my text entry code and get a  
nice looking UI working for a game  
([http://chris.luethy.net/screenshot.png](http://chris.luethy.net/screenshot.png)). Thanks again!On Thu, 2003-03-27 at 18:39, Jim wrote:

> I’ll give you several simple lines of code that can greatly simplify that  
> which you seem to have overcomplicated… hmm… but then again - looking at  
> the code and the several layers of translation that happen to a key - I  
> guess mine is as large, but 95% of it is in tables of structures … (  
> keychar[key][modifier].data )
> 
> But really these are only of interest for a thing that tracks specific key  
> states… which actually normal text has about 5 keys which affect the  
> state… and of course any implementation will be only for your specific  
> nationality… But - most of the code is on\_press based, not on\_release…
> 
> ALPHA\_LOCK // set default state of alpha keys  
> NUM\_LOCK  
> SHIFT // not (!) the state of ALPHA\_LOCK/NUM\_LOCK
> 
> - that is if alpha is on - shift effect is off
> - if alpha is off - shift effect is on
> 
> CONTROL - well this applies naturally to all alpha-characters and maps the  
> key to keyval - ‘a’ + 1, ctrl-@ is NUL.  
> ALT - open for application overriding…
> 
> typedef unsigned long \_32;
> 
> // declare a set of flags…  
> #define FLAGSET(v,n) \_32 (v)[((n)+31)/32]  
> // set a single flag index  
> #define SETFLAG(v,n) ( (v)[(n)\>\>5] |= 1 \<\< ( (n) & 31 ))  
> // clear a single flag index  
> #define RESETFLAG(v,n) ( (v)[(n)\>\>5] &= ~( 1 \<\< ( (n) & 31 ) ) )  
> // test if a flags is set  
> #define TESTFLAG(v,n) ( (v)[(n)\>\>5] & ( 1 \<\< ( (n) & 31 ) ) )  
> // reverse a flag from 1 to 0 and vice versa  
> #define TOGGLEFLAG(v,n) ( (v)[(n)\>\>5] ^= 1 \<\< ( (n) & 31 ))
> 
> FLAGSET( keyboard, 256 ); // declare a set of bits…
> 
> SETFLAG( keyboard, nKey ); // set a bit
> 
> if( TESTFLAG( keyboard, nKey ) ) // test a bit  
> {  
> …  
> }
> 
> Hmm… clear as mud, right?
> 
> ----- Original Message -----  
> From: “Christopher Thielen” \<@Christopher\_Thielen\>  
> To:   
> Sent: Thursday, March 27, 2003 3:52 PM  
> Subject: [SDL] taking input
> 
> > I’m working on a simple text entry box and I’m using the SDL\_Event  
> > structure to obtain input. From experience, what is the best way to  
> > check for a key? Currently I’m using bitfields to remember which keys  
> > were pressed, and on KEYUP events, I check to see if the key that was  
> > released was previously down, if so, that’s a keypress, and I can take  
> > it as input. Is there any better way than this? The way I just described  
> > is two or three hundred lines and kinda ugly. Any thoughts? I also run  
> > into the problem, using the largest bitfield, Uint32, that I only have  
> > 32 bits to set, but all the letters, plus the 10 numbers, backspace,  
> > spacebar, left shift, right shift, caps, etc. adds up to a lot more than  
> > 32 bits needed to remember that all. What should I do?
> > 
> > – chris (@Christopher_Thielen)
> > 
> > * * *
> > 
> > SDL mailing list  
> > SDL at [libsdl.org](http://libsdl.org)  
> > [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)
> 
> * * *
> 
> SDL mailing list  
> SDL at [libsdl.org](http://libsdl.org)  
> [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)
