Added SDL_GetScancodeFromName() and SDL_GetKeyFromName()
authorSam Lantinga <slouken@libsdl.org>
Mon, 24 Oct 2011 21:34:54 -0400
changeset 60294a8bbaa52b9b
parent 6028 869b6723e1fc
child 6030 50560ca58f80
Added SDL_GetScancodeFromName() and SDL_GetKeyFromName()
include/SDL_keyboard.h
src/events/SDL_keyboard.c
src/stdlib/SDL_string.c
     1.1 --- a/include/SDL_keyboard.h	Mon Oct 24 23:00:09 2011 -0400
     1.2 +++ b/include/SDL_keyboard.h	Mon Oct 24 21:34:54 2011 -0400
     1.3 @@ -111,15 +111,22 @@
     1.4  /**
     1.5   *  \brief Get a human-readable name for a scancode.
     1.6   *  
     1.7 - *  \return A pointer to a UTF-8 string that stays valid at least until the next
     1.8 - *          call to this function. If you need it around any longer, you must 
     1.9 - *          copy it.  If the scancode doesn't have a name, this function returns
    1.10 + *  \return A pointer to the name for the scancode.
    1.11 + *          If the scancode doesn't have a name, this function returns
    1.12   *          an empty string ("").
    1.13   *
    1.14   *  \sa SDL_Scancode
    1.15   */
    1.16 -extern DECLSPEC const char *SDLCALL SDL_GetScancodeName(SDL_Scancode
    1.17 -                                                        scancode);
    1.18 +extern DECLSPEC const char *SDLCALL SDL_GetScancodeName(SDL_Scancode scancode);
    1.19 +
    1.20 +/**
    1.21 + *  \brief Get a scancode from a human-readable name
    1.22 + *  
    1.23 + *  \return scancode, or SDL_SCANCODE_UNKNOWN if the name wasn't recognized
    1.24 + *
    1.25 + *  \sa SDL_Scancode
    1.26 + */
    1.27 +extern DECLSPEC SDL_Scancode SDLCALL SDL_GetScancodeFromName(const char *name);
    1.28  
    1.29  /**
    1.30   *  \brief Get a human-readable name for a key.
    1.31 @@ -134,6 +141,15 @@
    1.32  extern DECLSPEC const char *SDLCALL SDL_GetKeyName(SDL_Keycode key);
    1.33  
    1.34  /**
    1.35 + *  \brief Get a key code from a human-readable name
    1.36 + *  
    1.37 + *  \return key code, or SDLK_UNKNOWN if the name wasn't recognized
    1.38 + *
    1.39 + *  \sa SDL_Keycode
    1.40 + */
    1.41 +extern DECLSPEC SDL_Keycode SDLCALL SDL_GetKeyFromName(const char *name);
    1.42 +
    1.43 +/**
    1.44   *  \brief Start accepting Unicode text input events.
    1.45   *  
    1.46   *  \sa SDL_StopTextInput()
     2.1 --- a/src/events/SDL_keyboard.c	Mon Oct 24 23:00:09 2011 -0400
     2.2 +++ b/src/events/SDL_keyboard.c	Mon Oct 24 21:34:54 2011 -0400
     2.3 @@ -854,6 +854,23 @@
     2.4      return keyboard->keymap[scancode];
     2.5  }
     2.6  
     2.7 +SDL_Keycode SDL_GetKeycodeFromName(const char *name)
     2.8 +{
     2.9 +	int i;
    2.10 +
    2.11 +	if (!name || !*name) {
    2.12 +		return SDL_SCANCODE_UNKNOWN;
    2.13 +	}
    2.14 +
    2.15 +	for (i = 0; i < SDL_arraysize(SDL_scancode_names); ++i) {
    2.16 +		if (SDL_strcasecmp(name, SDL_scancode_names[i]) == 0) {
    2.17 +			return (SDL_Scancode)i;
    2.18 +		}
    2.19 +	}
    2.20 +	return SDL_SCANCODE_UNKNOWN;
    2.21 +}
    2.22 +
    2.23 +
    2.24  SDL_Scancode
    2.25  SDL_GetScancodeFromKey(SDL_Keycode key)
    2.26  {
    2.27 @@ -880,6 +897,25 @@
    2.28          return "";
    2.29  }
    2.30  
    2.31 +SDL_Scancode SDL_GetScancodeFromName(const char *name)
    2.32 +{
    2.33 +	int i;
    2.34 +
    2.35 +	if (!name || !*name) {
    2.36 +		return SDL_SCANCODE_UNKNOWN;
    2.37 +	}
    2.38 +
    2.39 +	for (i = 0; i < SDL_arraysize(SDL_scancode_names); ++i) {
    2.40 +		if (!SDL_scancode_names[i]) {
    2.41 +			continue;
    2.42 +		}
    2.43 +		if (SDL_strcasecmp(name, SDL_scancode_names[i]) == 0) {
    2.44 +			return (SDL_Scancode)i;
    2.45 +		}
    2.46 +	}
    2.47 +	return SDL_SCANCODE_UNKNOWN;
    2.48 +}
    2.49 +
    2.50  const char *
    2.51  SDL_GetKeyName(SDL_Keycode key)
    2.52  {
    2.53 @@ -919,4 +955,51 @@
    2.54      }
    2.55  }
    2.56  
    2.57 +SDL_Keycode
    2.58 +SDL_GetKeyFromName(const char *name)
    2.59 +{
    2.60 +	SDL_Keycode key;
    2.61 +
    2.62 +	/* If it's a single UTF-8 character, then that's the keycode itself */
    2.63 +	key = *(const unsigned char *)name;
    2.64 +	if (key >= 0xF0) {
    2.65 +		if (SDL_strlen(name) == 4) {
    2.66 +			int i = 0;
    2.67 +			key  = (Uint16)(name[i]&0x07) << 18;
    2.68 +			key |= (Uint16)(name[++i]&0x3F) << 12;
    2.69 +			key |= (Uint16)(name[++i]&0x3F) << 6;
    2.70 +			key |= (Uint16)(name[++i]&0x3F);
    2.71 +			return key;
    2.72 +		}
    2.73 +		return SDLK_UNKNOWN;
    2.74 +	} else if (key >= 0xE0) {
    2.75 +		if (SDL_strlen(name) == 3) {
    2.76 +			int i = 0;
    2.77 +			key  = (Uint16)(name[i]&0x0F) << 12;
    2.78 +			key |= (Uint16)(name[++i]&0x3F) << 6;
    2.79 +			key |= (Uint16)(name[++i]&0x3F);
    2.80 +			return key;
    2.81 +		}
    2.82 +		return SDLK_UNKNOWN;
    2.83 +	} else if (key >= 0xC0) {
    2.84 +		if (SDL_strlen(name) == 2) {
    2.85 +			int i = 0;
    2.86 +			key  = (Uint16)(name[i]&0x1F) << 6;
    2.87 +			key |= (Uint16)(name[++i]&0x3F);
    2.88 +			return key;
    2.89 +		}
    2.90 +		return SDLK_UNKNOWN;
    2.91 +	} else {
    2.92 +		if (SDL_strlen(name) == 1) {
    2.93 +			if (key >= 'A' && key <= 'Z') {
    2.94 +				key += 32;
    2.95 +			}
    2.96 +			return key;
    2.97 +		}
    2.98 +
    2.99 +		/* Get the scancode for this name, and the associated keycode */
   2.100 +		return SDL_default_keymap[SDL_GetScancodeFromName(name)];
   2.101 +	}
   2.102 +}
   2.103 +
   2.104  /* vi: set ts=4 sw=4 expandtab: */
     3.1 --- a/src/stdlib/SDL_string.c	Mon Oct 24 23:00:09 2011 -0400
     3.2 +++ b/src/stdlib/SDL_string.c	Mon Oct 24 21:34:54 2011 -0400
     3.3 @@ -33,7 +33,7 @@
     3.4  
     3.5  int UTF8_TrailingBytes(unsigned char c)
     3.6  {
     3.7 -    if (c >= 0xC0 && c<= 0xDF)
     3.8 +    if (c >= 0xC0 && c <= 0xDF)
     3.9          return 1;
    3.10      else if (c >= 0xE0 && c <= 0xEF)
    3.11          return 2;