src/events/SDL_keyboard.c
changeset 6029 4a8bbaa52b9b
parent 5535 96594ac5fd1a
child 6044 35448a5ea044
equal deleted inserted replaced
6028:869b6723e1fc 6029:4a8bbaa52b9b
   852     SDL_Keyboard *keyboard = &SDL_keyboard;
   852     SDL_Keyboard *keyboard = &SDL_keyboard;
   853 
   853 
   854     return keyboard->keymap[scancode];
   854     return keyboard->keymap[scancode];
   855 }
   855 }
   856 
   856 
       
   857 SDL_Keycode SDL_GetKeycodeFromName(const char *name)
       
   858 {
       
   859 	int i;
       
   860 
       
   861 	if (!name || !*name) {
       
   862 		return SDL_SCANCODE_UNKNOWN;
       
   863 	}
       
   864 
       
   865 	for (i = 0; i < SDL_arraysize(SDL_scancode_names); ++i) {
       
   866 		if (SDL_strcasecmp(name, SDL_scancode_names[i]) == 0) {
       
   867 			return (SDL_Scancode)i;
       
   868 		}
       
   869 	}
       
   870 	return SDL_SCANCODE_UNKNOWN;
       
   871 }
       
   872 
       
   873 
   857 SDL_Scancode
   874 SDL_Scancode
   858 SDL_GetScancodeFromKey(SDL_Keycode key)
   875 SDL_GetScancodeFromKey(SDL_Keycode key)
   859 {
   876 {
   860     SDL_Keyboard *keyboard = &SDL_keyboard;
   877     SDL_Keyboard *keyboard = &SDL_keyboard;
   861     SDL_Scancode scancode;
   878     SDL_Scancode scancode;
   876 
   893 
   877     if (name)
   894     if (name)
   878         return name;
   895         return name;
   879     else
   896     else
   880         return "";
   897         return "";
       
   898 }
       
   899 
       
   900 SDL_Scancode SDL_GetScancodeFromName(const char *name)
       
   901 {
       
   902 	int i;
       
   903 
       
   904 	if (!name || !*name) {
       
   905 		return SDL_SCANCODE_UNKNOWN;
       
   906 	}
       
   907 
       
   908 	for (i = 0; i < SDL_arraysize(SDL_scancode_names); ++i) {
       
   909 		if (!SDL_scancode_names[i]) {
       
   910 			continue;
       
   911 		}
       
   912 		if (SDL_strcasecmp(name, SDL_scancode_names[i]) == 0) {
       
   913 			return (SDL_Scancode)i;
       
   914 		}
       
   915 	}
       
   916 	return SDL_SCANCODE_UNKNOWN;
   881 }
   917 }
   882 
   918 
   883 const char *
   919 const char *
   884 SDL_GetKeyName(SDL_Keycode key)
   920 SDL_GetKeyName(SDL_Keycode key)
   885 {
   921 {
   917         *end = '\0';
   953         *end = '\0';
   918         return name;
   954         return name;
   919     }
   955     }
   920 }
   956 }
   921 
   957 
       
   958 SDL_Keycode
       
   959 SDL_GetKeyFromName(const char *name)
       
   960 {
       
   961 	SDL_Keycode key;
       
   962 
       
   963 	/* If it's a single UTF-8 character, then that's the keycode itself */
       
   964 	key = *(const unsigned char *)name;
       
   965 	if (key >= 0xF0) {
       
   966 		if (SDL_strlen(name) == 4) {
       
   967 			int i = 0;
       
   968 			key  = (Uint16)(name[i]&0x07) << 18;
       
   969 			key |= (Uint16)(name[++i]&0x3F) << 12;
       
   970 			key |= (Uint16)(name[++i]&0x3F) << 6;
       
   971 			key |= (Uint16)(name[++i]&0x3F);
       
   972 			return key;
       
   973 		}
       
   974 		return SDLK_UNKNOWN;
       
   975 	} else if (key >= 0xE0) {
       
   976 		if (SDL_strlen(name) == 3) {
       
   977 			int i = 0;
       
   978 			key  = (Uint16)(name[i]&0x0F) << 12;
       
   979 			key |= (Uint16)(name[++i]&0x3F) << 6;
       
   980 			key |= (Uint16)(name[++i]&0x3F);
       
   981 			return key;
       
   982 		}
       
   983 		return SDLK_UNKNOWN;
       
   984 	} else if (key >= 0xC0) {
       
   985 		if (SDL_strlen(name) == 2) {
       
   986 			int i = 0;
       
   987 			key  = (Uint16)(name[i]&0x1F) << 6;
       
   988 			key |= (Uint16)(name[++i]&0x3F);
       
   989 			return key;
       
   990 		}
       
   991 		return SDLK_UNKNOWN;
       
   992 	} else {
       
   993 		if (SDL_strlen(name) == 1) {
       
   994 			if (key >= 'A' && key <= 'Z') {
       
   995 				key += 32;
       
   996 			}
       
   997 			return key;
       
   998 		}
       
   999 
       
  1000 		/* Get the scancode for this name, and the associated keycode */
       
  1001 		return SDL_default_keymap[SDL_GetScancodeFromName(name)];
       
  1002 	}
       
  1003 }
       
  1004 
   922 /* vi: set ts=4 sw=4 expandtab: */
  1005 /* vi: set ts=4 sw=4 expandtab: */