summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam Lantinga <slouken@libsdl.org>2011-10-24 21:34:54 -0400
committerSam Lantinga <slouken@libsdl.org>2011-10-24 21:34:54 -0400
commit517d92d975d460f17590afdf4455d87fbf3c268e (patch)
treee3bd2f05ce47a6349d45cd309960bc1929f8ce00
parentb2faaf2be49e00ef9750de52b453c2f9d562e4f1 (diff)
Added SDL_GetScancodeFromName() and SDL_GetKeyFromName()
-rw-r--r--include/SDL_keyboard.h26
-rw-r--r--src/events/SDL_keyboard.c83
-rw-r--r--src/stdlib/SDL_string.c2
3 files changed, 105 insertions, 6 deletions
diff --git a/include/SDL_keyboard.h b/include/SDL_keyboard.h
index c390cb69..1f9a66df 100644
--- a/include/SDL_keyboard.h
+++ b/include/SDL_keyboard.h
@@ -111,15 +111,22 @@ extern DECLSPEC SDL_Scancode SDLCALL SDL_GetScancodeFromKey(SDL_Keycode key);
/**
* \brief Get a human-readable name for a scancode.
*
- * \return A pointer to a UTF-8 string that stays valid at least until the next
- * call to this function. If you need it around any longer, you must
- * copy it. If the scancode doesn't have a name, this function returns
+ * \return A pointer to the name for the scancode.
+ * If the scancode doesn't have a name, this function returns
* an empty string ("").
*
* \sa SDL_Scancode
*/
-extern DECLSPEC const char *SDLCALL SDL_GetScancodeName(SDL_Scancode
- scancode);
+extern DECLSPEC const char *SDLCALL SDL_GetScancodeName(SDL_Scancode scancode);
+
+/**
+ * \brief Get a scancode from a human-readable name
+ *
+ * \return scancode, or SDL_SCANCODE_UNKNOWN if the name wasn't recognized
+ *
+ * \sa SDL_Scancode
+ */
+extern DECLSPEC SDL_Scancode SDLCALL SDL_GetScancodeFromName(const char *name);
/**
* \brief Get a human-readable name for a key.
@@ -134,6 +141,15 @@ extern DECLSPEC const char *SDLCALL SDL_GetScancodeName(SDL_Scancode
extern DECLSPEC const char *SDLCALL SDL_GetKeyName(SDL_Keycode key);
/**
+ * \brief Get a key code from a human-readable name
+ *
+ * \return key code, or SDLK_UNKNOWN if the name wasn't recognized
+ *
+ * \sa SDL_Keycode
+ */
+extern DECLSPEC SDL_Keycode SDLCALL SDL_GetKeyFromName(const char *name);
+
+/**
* \brief Start accepting Unicode text input events.
*
* \sa SDL_StopTextInput()
diff --git a/src/events/SDL_keyboard.c b/src/events/SDL_keyboard.c
index 94dd060a..7d990dd9 100644
--- a/src/events/SDL_keyboard.c
+++ b/src/events/SDL_keyboard.c
@@ -854,6 +854,23 @@ SDL_GetKeyFromScancode(SDL_Scancode scancode)
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 @@ SDL_GetScancodeName(SDL_Scancode scancode)
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_GetKeyName(SDL_Keycode key)
}
}
+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: */
diff --git a/src/stdlib/SDL_string.c b/src/stdlib/SDL_string.c
index dc7d5a81..e3942713 100644
--- a/src/stdlib/SDL_string.c
+++ b/src/stdlib/SDL_string.c
@@ -33,7 +33,7 @@
int UTF8_TrailingBytes(unsigned char c)
{
- if (c >= 0xC0 && c<= 0xDF)
+ if (c >= 0xC0 && c <= 0xDF)
return 1;
else if (c >= 0xE0 && c <= 0xEF)
return 2;