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