src/events/SDL_keyboard.c
changeset 6029 4a8bbaa52b9b
parent 5535 96594ac5fd1a
child 6044 35448a5ea044
     1.1 --- a/src/events/SDL_keyboard.c	Mon Oct 24 23:00:09 2011 -0400
     1.2 +++ b/src/events/SDL_keyboard.c	Mon Oct 24 21:34:54 2011 -0400
     1.3 @@ -854,6 +854,23 @@
     1.4      return keyboard->keymap[scancode];
     1.5  }
     1.6  
     1.7 +SDL_Keycode SDL_GetKeycodeFromName(const char *name)
     1.8 +{
     1.9 +	int i;
    1.10 +
    1.11 +	if (!name || !*name) {
    1.12 +		return SDL_SCANCODE_UNKNOWN;
    1.13 +	}
    1.14 +
    1.15 +	for (i = 0; i < SDL_arraysize(SDL_scancode_names); ++i) {
    1.16 +		if (SDL_strcasecmp(name, SDL_scancode_names[i]) == 0) {
    1.17 +			return (SDL_Scancode)i;
    1.18 +		}
    1.19 +	}
    1.20 +	return SDL_SCANCODE_UNKNOWN;
    1.21 +}
    1.22 +
    1.23 +
    1.24  SDL_Scancode
    1.25  SDL_GetScancodeFromKey(SDL_Keycode key)
    1.26  {
    1.27 @@ -880,6 +897,25 @@
    1.28          return "";
    1.29  }
    1.30  
    1.31 +SDL_Scancode SDL_GetScancodeFromName(const char *name)
    1.32 +{
    1.33 +	int i;
    1.34 +
    1.35 +	if (!name || !*name) {
    1.36 +		return SDL_SCANCODE_UNKNOWN;
    1.37 +	}
    1.38 +
    1.39 +	for (i = 0; i < SDL_arraysize(SDL_scancode_names); ++i) {
    1.40 +		if (!SDL_scancode_names[i]) {
    1.41 +			continue;
    1.42 +		}
    1.43 +		if (SDL_strcasecmp(name, SDL_scancode_names[i]) == 0) {
    1.44 +			return (SDL_Scancode)i;
    1.45 +		}
    1.46 +	}
    1.47 +	return SDL_SCANCODE_UNKNOWN;
    1.48 +}
    1.49 +
    1.50  const char *
    1.51  SDL_GetKeyName(SDL_Keycode key)
    1.52  {
    1.53 @@ -919,4 +955,51 @@
    1.54      }
    1.55  }
    1.56  
    1.57 +SDL_Keycode
    1.58 +SDL_GetKeyFromName(const char *name)
    1.59 +{
    1.60 +	SDL_Keycode key;
    1.61 +
    1.62 +	/* If it's a single UTF-8 character, then that's the keycode itself */
    1.63 +	key = *(const unsigned char *)name;
    1.64 +	if (key >= 0xF0) {
    1.65 +		if (SDL_strlen(name) == 4) {
    1.66 +			int i = 0;
    1.67 +			key  = (Uint16)(name[i]&0x07) << 18;
    1.68 +			key |= (Uint16)(name[++i]&0x3F) << 12;
    1.69 +			key |= (Uint16)(name[++i]&0x3F) << 6;
    1.70 +			key |= (Uint16)(name[++i]&0x3F);
    1.71 +			return key;
    1.72 +		}
    1.73 +		return SDLK_UNKNOWN;
    1.74 +	} else if (key >= 0xE0) {
    1.75 +		if (SDL_strlen(name) == 3) {
    1.76 +			int i = 0;
    1.77 +			key  = (Uint16)(name[i]&0x0F) << 12;
    1.78 +			key |= (Uint16)(name[++i]&0x3F) << 6;
    1.79 +			key |= (Uint16)(name[++i]&0x3F);
    1.80 +			return key;
    1.81 +		}
    1.82 +		return SDLK_UNKNOWN;
    1.83 +	} else if (key >= 0xC0) {
    1.84 +		if (SDL_strlen(name) == 2) {
    1.85 +			int i = 0;
    1.86 +			key  = (Uint16)(name[i]&0x1F) << 6;
    1.87 +			key |= (Uint16)(name[++i]&0x3F);
    1.88 +			return key;
    1.89 +		}
    1.90 +		return SDLK_UNKNOWN;
    1.91 +	} else {
    1.92 +		if (SDL_strlen(name) == 1) {
    1.93 +			if (key >= 'A' && key <= 'Z') {
    1.94 +				key += 32;
    1.95 +			}
    1.96 +			return key;
    1.97 +		}
    1.98 +
    1.99 +		/* Get the scancode for this name, and the associated keycode */
   1.100 +		return SDL_default_keymap[SDL_GetScancodeFromName(name)];
   1.101 +	}
   1.102 +}
   1.103 +
   1.104  /* vi: set ts=4 sw=4 expandtab: */