Translating keycodes

Hi!

I’m working on a simple GUI for a game, and upon implementing an edit box I
bumped into a bit of a problem. How can I translate keypresses to characters
properly? Is there a simpler method of doing this, than just checking whether a
key is an alphabet and then checking whether Shift is pressed and the subtract
32 from the key to get capital letters properly? And the same goes for digits
and various other keys (?’\/) and so on.

I’m not sure if using SDL’s unicode functionality could handle this, but I guess
it’s not possible to use it since the GUI has to co-exist with the game (in-game
menus and so on).

Thanks in advance,
Anders.

Well, since you get an event for every key you press, you should check for a

  • shift to get A.
    Many keys are defined in SDL wich makes them a little easier to use.

Best regards
Daniel Liljeberg> ----- Original Message -----

From: testari@jippii.fi (Anders Froberg)
To:
Sent: Thursday, January 22, 2004 1:44 PM
Subject: [SDL] Translating keycodes

Hi!

I’m working on a simple GUI for a game, and upon implementing an edit box
I
bumped into a bit of a problem. How can I translate keypresses to
characters
properly? Is there a simpler method of doing this, than just checking
whether a
key is an alphabet and then checking whether Shift is pressed and the
subtract
32 from the key to get capital letters properly? And the same goes for
digits
and various other keys (?’\/) and so on.

I’m not sure if using SDL’s unicode functionality could handle this, but I
guess
it’s not possible to use it since the GUI has to co-exist with the game
(in-game
menus and so on).

Thanks in advance,
Anders.


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

Ahhh… Sorry… Didn’t read you question clear enogh :wink:
I basicly answered what you asked… hehe
Well, that is one way to go. Most likley the most basic and simple.

Sorry :wink:

Best regards
Daniel Liljeberg> ----- Original Message -----

From: @Daniel_Liljeberg (Daniel Liljeberg)
To:
Sent: Monday, January 26, 2004 8:18 AM
Subject: Re: [SDL] Translating keycodes

Well, since you get an event for every key you press, you should check for
a

  • shift to get A.
    Many keys are defined in SDL wich makes them a little easier to use.

Best regards
Daniel Liljeberg

----- Original Message -----
From: “Anders Fr??berg”
To:
Sent: Thursday, January 22, 2004 1:44 PM
Subject: [SDL] Translating keycodes

Hi!

I’m working on a simple GUI for a game, and upon implementing an edit
box
I

bumped into a bit of a problem. How can I translate keypresses to
characters
properly? Is there a simpler method of doing this, than just checking
whether a
key is an alphabet and then checking whether Shift is pressed and the
subtract
32 from the key to get capital letters properly? And the same goes for
digits
and various other keys (?’\/) and so on.

I’m not sure if using SDL’s unicode functionality could handle this, but
I
guess

it’s not possible to use it since the GUI has to co-exist with the game
(in-game
menus and so on).

Thanks in advance,
Anders.


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

I’m not sure if using SDL’s unicode functionality could handle this, but I guess
it’s not possible to use it since the GUI has to co-exist with the game (in-game
menus and so on).

I of course know nothing of your circumstances, but I’ve implemented a prototype GUI edit box which collects normal key events like this:

SDL_EnableUNICODE(true);

[…]

case SDL_KEYDOWN:
switch( event.key.keysym.sym )
{
case SDLK_ESCAPE:
done = true;
break;

   case SDLK_F1:
     if( current_font > 0 )
     {
       sdlview1->set_font(fontfiles[--current_font]);
       inp->refresh_views();
       updateStatus(screen,small,"font: "+std::string(fontfiles[current_font]));
     }
     break;

   [etc .. for any special keys that the input widget isn't supposed to see ...]

   default:
     s[0] = u8(event.key.keysym.unicode);
     inp->exec( s );
 }
 break;

[…]

Where ‘s’ is a std::string. Seems to work adequately. I haven’t looked into the whole Unicode thing deeper yet because I don’t have any font rendering code that could handle it anyhow (and I think it’s overkill for games).On Thu, 22 Jan 2004 12:44:11 +0000 (UTC), Anders Fr?berg wrote:

I’d be interested in the answers of those wiser than me about a simpler
method. However, This is how I implement the method you described:

SDL_Event *input = ;
Uint16 key = input->key.keysym.sym;

if (key >= SDLK_SPACE && key <= SDLK_KP_EQUALS){
if(input->key.keysym.mod & KMOD_LSHIFT || input->key.keysym.mod &
KMOD_RSHIFT){
if(key>=SDLK_a && key<=SDLK_z) key -= 32;
else switch(key){
case SDLK_1: key = SDLK_EXCLAIM; break;
case SDLK_2: key = SDLK_AT; break;
case SDLK_3: key = SDLK_HASH; break;
case SDLK_4: key = SDLK_DOLLAR; break;
case SDLK_5: key = 37; break;
case SDLK_6: key = SDLK_CARET; break;
case SDLK_7: key = SDLK_AMPERSAND; break;
case SDLK_8: key = SDLK_ASTERISK; break;
case SDLK_9: key = SDLK_LEFTPAREN; break;
case SDLK_0: key = SDLK_RIGHTPAREN; break;
case SDLK_SLASH: key = SDLK_QUESTION; break;
case SDLK_MINUS: key = SDLK_UNDERSCORE; break;
case SDLK_EQUALS: key = SDLK_PLUS; break;
case SDLK_LEFTBRACKET: key = 123; break;
case SDLK_RIGHTBRACKET: key = 125; break;
case SDLK_BACKSLASH: key = 124; break;
case SDLK_QUOTE: key = SDLK_QUOTEDBL; break;
case SDLK_SEMICOLON: key = SDLK_COLON; break;
case SDLK_COMMA: key = SDLK_LESS; break;
case SDLK_PERIOD: key = SDLK_GREATER; break;
case SDLK_BACKQUOTE: key = 126; break;
}
}
}

It processes the printing characters and adjusts them for shift.On Jan 22, 2004, at 7:44 AM, Anders Fr?berg wrote:

I’m working on a simple GUI for a game, and upon implementing an edit
box I
bumped into a bit of a problem. How can I translate keypresses to
characters
properly? Is there a simpler method of doing this, than just checking
whether a
key is an alphabet and then checking whether Shift is pressed and the
subtract
32 from the key to get capital letters properly? And the same goes for
digits
and various other keys (?’\/) and so on.

I’m not sure if using SDL’s unicode functionality could handle this,
but I guess
it’s not possible to use it since the GUI has to co-exist with the
game (in-game
menus and so on).

another cool way is to enable unicode and use the unicode portion of the key
event.

i think the function is SDLEnableUnicode(bool enable);

this is what i use atleast, works nicely since the unicode is ‘A’ if you
press a and hold shift etc, it handles all that junk for you.> ----- Original Message -----

From: damien_@hotmail.com (Daniel Liljeberg)
To:
Sent: Sunday, January 25, 2004 11:18 PM
Subject: Re: [SDL] Translating keycodes

Well, since you get an event for every key you press, you should check for
a

  • shift to get A.
    Many keys are defined in SDL wich makes them a little easier to use.

Best regards
Daniel Liljeberg

----- Original Message -----
From: “Anders Fr??berg”
To:
Sent: Thursday, January 22, 2004 1:44 PM
Subject: [SDL] Translating keycodes

Hi!

I’m working on a simple GUI for a game, and upon implementing an edit
box
I

bumped into a bit of a problem. How can I translate keypresses to
characters
properly? Is there a simpler method of doing this, than just checking
whether a
key is an alphabet and then checking whether Shift is pressed and the
subtract
32 from the key to get capital letters properly? And the same goes for
digits
and various other keys (?’\/) and so on.

I’m not sure if using SDL’s unicode functionality could handle this, but
I
guess

it’s not possible to use it since the GUI has to co-exist with the game
(in-game
menus and so on).

Thanks in advance,
Anders.


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

Hello!

Maybe SDL_GetKeyName (http://sdldoc.csn.ul.ie/sdlgetkeyname.php) will be
helpful. With that you can translate the keys to characters directly.
Perhaps have a look at this: http://sdldoc.csn.ul.ie/sdlkey.php.
As already mentioned, you could check if SHIFT was pressed for getting a
capital letter.

Regards,
Bernhard

Anders Fr?berg wrote:> Hi!

I’m working on a simple GUI for a game, and upon implementing an edit box I
bumped into a bit of a problem. How can I translate keypresses to characters
properly? Is there a simpler method of doing this, than just checking whether a
key is an alphabet and then checking whether Shift is pressed and the subtract
32 from the key to get capital letters properly? And the same goes for digits
and various other keys (?’\/) and so on.

I’m not sure if using SDL’s unicode functionality could handle this, but I guess
it’s not possible to use it since the GUI has to co-exist with the game (in-game
menus and so on).

Thanks in advance,
Anders.


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

another cool way is to enable unicode and use the unicode portion of the key
event.

i think the function is SDLEnableUnicode(bool enable);

this is what i use atleast, works nicely since the unicode is ‘A’ if you
press a and hold shift etc, it handles all that junk for you.

This is the right way to do it. Everybody who is using the sym directly,
you need to rewrite your code to look at the unicode member of the key event,
otherwise your program won’t work with non-english keyboards.

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

…or Dvorak. (Although my version is non-english as well. :wink:

Seriously, it’s really rather annoying when applications assume you
have a Qwerty layout.

//David Olofson - Programmer, Composer, Open Source Advocate

.- Audiality -----------------------------------------------.
| Free/Open Source audio engine for games and multimedia. |
| MIDI, modular synthesis, real time effects, scripting,… |
`-----------------------------------> http://audiality.org -’
http://olofson.nethttp://www.reologica.se —On Tuesday 27 January 2004 06.00, Sam Lantinga wrote:

another cool way is to enable unicode and use the unicode portion
of the key event.

i think the function is SDLEnableUnicode(bool enable);

this is what i use atleast, works nicely since the unicode is 'A’
if you press a and hold shift etc, it handles all that junk for
you.

This is the right way to do it. Everybody who is using the sym
directly, you need to rewrite your code to look at the unicode
member of the key event, otherwise your program won’t work with
non-english keyboards.