diff -r 869b6723e1fc -r 4a8bbaa52b9b src/events/SDL_keyboard.c --- a/src/events/SDL_keyboard.c Mon Oct 24 23:00:09 2011 -0400 +++ b/src/events/SDL_keyboard.c Mon Oct 24 21:34:54 2011 -0400 @@ -854,6 +854,23 @@ return keyboard->keymap[scancode]; } +SDL_Keycode SDL_GetKeycodeFromName(const char *name) +{ + int i; + + if (!name || !*name) { + return SDL_SCANCODE_UNKNOWN; + } + + for (i = 0; i < SDL_arraysize(SDL_scancode_names); ++i) { + if (SDL_strcasecmp(name, SDL_scancode_names[i]) == 0) { + return (SDL_Scancode)i; + } + } + return SDL_SCANCODE_UNKNOWN; +} + + SDL_Scancode SDL_GetScancodeFromKey(SDL_Keycode key) { @@ -880,6 +897,25 @@ return ""; } +SDL_Scancode SDL_GetScancodeFromName(const char *name) +{ + int i; + + if (!name || !*name) { + return SDL_SCANCODE_UNKNOWN; + } + + for (i = 0; i < SDL_arraysize(SDL_scancode_names); ++i) { + if (!SDL_scancode_names[i]) { + continue; + } + if (SDL_strcasecmp(name, SDL_scancode_names[i]) == 0) { + return (SDL_Scancode)i; + } + } + return SDL_SCANCODE_UNKNOWN; +} + const char * SDL_GetKeyName(SDL_Keycode key) { @@ -919,4 +955,51 @@ } } +SDL_Keycode +SDL_GetKeyFromName(const char *name) +{ + SDL_Keycode key; + + /* If it's a single UTF-8 character, then that's the keycode itself */ + key = *(const unsigned char *)name; + if (key >= 0xF0) { + if (SDL_strlen(name) == 4) { + int i = 0; + key = (Uint16)(name[i]&0x07) << 18; + key |= (Uint16)(name[++i]&0x3F) << 12; + key |= (Uint16)(name[++i]&0x3F) << 6; + key |= (Uint16)(name[++i]&0x3F); + return key; + } + return SDLK_UNKNOWN; + } else if (key >= 0xE0) { + if (SDL_strlen(name) == 3) { + int i = 0; + key = (Uint16)(name[i]&0x0F) << 12; + key |= (Uint16)(name[++i]&0x3F) << 6; + key |= (Uint16)(name[++i]&0x3F); + return key; + } + return SDLK_UNKNOWN; + } else if (key >= 0xC0) { + if (SDL_strlen(name) == 2) { + int i = 0; + key = (Uint16)(name[i]&0x1F) << 6; + key |= (Uint16)(name[++i]&0x3F); + return key; + } + return SDLK_UNKNOWN; + } else { + if (SDL_strlen(name) == 1) { + if (key >= 'A' && key <= 'Z') { + key += 32; + } + return key; + } + + /* Get the scancode for this name, and the associated keycode */ + return SDL_default_keymap[SDL_GetScancodeFromName(name)]; + } +} + /* vi: set ts=4 sw=4 expandtab: */