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)

http://sdldoc.csn.ul.ie/sdlgetkeystate.php

Regards,

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

From: sdl-admin@libsdl.org [mailto:sdl-admin at libsdl.org]On Behalf Of
Christopher Thielen
Sent: Friday, 28 March 2003 10:53 AM
To: sdl at 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)


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

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 Hire me!
http://newbreedsoftware.com/bill/ http://newbreedsoftware.com/bill/resume/

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)


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

±----------------------------------+

  • Bob Pendleton: independent writer +
  • and programmer. +
  • email: Bob at Pendleton.com +
    ±----------------------------------+

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


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

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

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


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

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). 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://www.libsdl.org/mailman/listinfo/sdl


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl