I’m writing a user interface in rubysdl, and things have been going along
pretty smoothly. I was wondering if there is a way to get the ASCII code of an
aggregate key event, for example shift+‘a’ to get ‘A’. I know sdl is more for
raw input and game development, but is there a way to get this? I don’t want
to keep track of the shift state if I can help it, because that opens up a new
can of worms with alphabetic and non-alphabetic shifted values.
Thanks for sdl and rubysdl!
Mike
In C, it’s
SDL_EnableUNICODE (1); in your init stuff
Then use the `unicode’ field in the keysym.
I don’t know how that translates to ruby, but it should be helpful
– JoshOn 3/12/06, Mike Austin <mike_ekim at yahoo.com> wrote:
I’m writing a user interface in rubysdl, and things have been going along
pretty smoothly. I was wondering if there is a way to get the ASCII code of an
aggregate key event, for example shift+‘a’ to get ‘A’. I know sdl is more for
raw input and game development, but is there a way to get this? I don’t want
to keep track of the shift state if I can help it, because that opens up a new
can of worms with alphabetic and non-alphabetic shifted values.
Joshua Oreman wrote:> On 3/12/06, Mike Austin <@Mike_Austin> wrote:
I’m writing a user interface in rubysdl, and things have been going along
pretty smoothly. I was wondering if there is a way to get the ASCII code of an
aggregate key event, for example shift+‘a’ to get ‘A’. I know sdl is more for
raw input and game development, but is there a way to get this? I don’t want
to keep track of the shift state if I can help it, because that opens up a new
can of worms with alphabetic and non-alphabetic shifted values.
In C, it’s
SDL_EnableUNICODE (1); in your init stuff
Then use the `unicode’ field in the keysym.
I don’t know how that translates to ruby, but it should be helpful
– Josh
That works, great! But I guess the number pad is a different issue, as it
doesn’t produce unicode. Hmmm.
Mike
True enough. Another option is to do the conversions yourself:On 3/13/06, Mike Austin <mike_ekim at yahoo.com> wrote:
That works, great! But I guess the number pad is a different issue, as it
doesn’t produce unicode. Hmmm.
–
int get_char(SDL_keysym sym)
{
int c;
// Do not want the modifier keys by themselves
if (sym.sym >= SDLK_NUMLOCK && sym.sym <= SDLK_COMPOSE) {
return (0);
}
c = sym.sym;
if ((sym.mod & KMOD_LSHIFT) || (sym.mod & KMOD_RSHIFT)
|| (sym.mod & KMOD_CAPS)) {
// Get upper case
if (c >= SDLK_a && c <= SDLK_z) {
c = c - 32;
} else if (c == SDLK_BACKQUOTE) {
c = '~';
} else if (c == SDLK_1) {
c = SDLK_EXCLAIM;
} else if (c == SDLK_2) {
c = SDLK_AT;
} else if (c == SDLK_3) {
c = SDLK_HASH;
} else if (c == SDLK_4) {
c = SDLK_DOLLAR;
} else if (c == SDLK_5) {
c = '%';
} else if (c == SDLK_6) {
c = SDLK_CARET;
} else if (c == SDLK_7) {
c = SDLK_AMPERSAND;
} else if (c == SDLK_8) {
c = SDLK_ASTERISK;
} else if (c == SDLK_9) {
c = SDLK_LEFTPAREN;
} else if (c == SDLK_0) {
c = SDLK_RIGHTPAREN;
} else if (c == SDLK_MINUS) {
c = SDLK_UNDERSCORE;
} else if (c == SDLK_EQUALS) {
c = SDLK_PLUS;
} else if (c == SDLK_LEFTBRACKET) {
c = '{';
} else if (c == SDLK_RIGHTBRACKET) {
c = '}';
} else if (c == SDLK_SEMICOLON) {
c = SDLK_COLON;
} else if (c == SDLK_QUOTE) {
c = SDLK_QUOTEDBL;
} else if (c == SDLK_COMMA) {
c = SDLK_LESS;
} else if (c == SDLK_PERIOD) {
c = SDLK_GREATER;
} else if (c == SDLK_SLASH) {
c = SDLK_QUESTION;
} else if (c == SDLK_BACKSLASH) {
c = '|';
}
}
// control keys
if ((sym.mod & KMOD_LCTRL) || (sym.mod & KMOD_RCTRL)) {
if (c >= SDLK_a && c <= SDLK_z) {
c = c - 96;
}
}
// numeric key pad
if (c == SDLK_KP_DIVIDE) {
c = SDLK_SLASH;
} else if (c == SDLK_KP_MULTIPLY) {
c = SDLK_ASTERISK;
} else if (c == SDLK_KP_MINUS) {
c = SDLK_MINUS;
} else if (c == SDLK_KP_PLUS) {
c = SDLK_PLUS;
} else if (c == SDLK_KP_ENTER) {
c = SDLK_RETURN;
}
if (sym.mod & KMOD_NUM) {
if (c == SDLK_KP0) {
c = SDLK_0;
} else if (c == SDLK_KP1) {
c = SDLK_1;
} else if (c == SDLK_KP2) {
c = SDLK_2;
} else if (c == SDLK_KP3) {
c = SDLK_3;
} else if (c == SDLK_KP4) {
c = SDLK_4;
} else if (c == SDLK_KP5) {
c = SDLK_5;
} else if (c == SDLK_KP6) {
c = SDLK_6;
} else if (c == SDLK_KP7) {
c = SDLK_7;
} else if (c == SDLK_KP8) {
c = SDLK_8;
} else if (c == SDLK_KP9) {
c = SDLK_9;
} else if (c == SDLK_KP_PERIOD) {
c = SDLK_PERIOD;
}
} else {
if (c == SDLK_KP0) {
c = SDLK_INSERT;
} else if (c == SDLK_KP1) {
c = SDLK_END;
} else if (c == SDLK_KP2) {
c = SDLK_DOWN;
} else if (c == SDLK_KP3) {
c = SDLK_PAGEDOWN;
} else if (c == SDLK_KP4) {
c = SDLK_LEFT;
} else if (c == SDLK_KP5) {
c = 0;
} else if (c == SDLK_KP6) {
c = SDLK_RIGHT;
} else if (c == SDLK_KP7) {
c = SDLK_HOME;
} else if (c == SDLK_KP8) {
c = SDLK_UP;
} else if (c == SDLK_KP9) {
c = SDLK_PAGEUP;
} else if (c == SDLK_KP_PERIOD) {
c = SDLK_DELETE;
}
}
return (c);
}
–
E-Mail: Chris Nystrom
http://www.newio.org/
AIM: nystromchris