SDL_keysym_to_keycode.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "SDL_internal.h"
  19. #if defined(SDL_VIDEO_DRIVER_WAYLAND) || defined(SDL_VIDEO_DRIVER_X11)
  20. #include "SDL_keyboard_c.h"
  21. #include "SDL_keysym_to_scancode_c.h"
  22. #include "imKStoUCS.h"
  23. // Extended key code mappings
  24. static const struct
  25. {
  26. Uint32 keysym;
  27. SDL_Keycode keycode;
  28. } keysym_to_keycode_table[] = {
  29. { 0xfe03, SDLK_MODE }, // XK_ISO_Level3_Shift
  30. { 0xfe11, SDLK_LEVEL5_SHIFT }, // XK_ISO_Level5_Shift
  31. { 0xfe20, SDLK_LEFT_TAB }, // XK_ISO_Left_Tab
  32. { 0xff20, SDLK_MULTI_KEY_COMPOSE }, // XK_Multi_key
  33. { 0xffe7, SDLK_LMETA }, // XK_Meta_L
  34. { 0xffe8, SDLK_RMETA }, // XK_Meta_R
  35. { 0xffed, SDLK_LHYPER }, // XK_Hyper_L
  36. { 0xffee, SDLK_RHYPER }, // XK_Hyper_R
  37. };
  38. SDL_Keycode SDL_GetKeyCodeFromKeySym(Uint32 keysym, Uint32 keycode, SDL_Keymod modifiers)
  39. {
  40. SDL_Keycode sdl_keycode = SDL_KeySymToUcs4(keysym);
  41. if (!sdl_keycode) {
  42. for (int i = 0; i < SDL_arraysize(keysym_to_keycode_table); ++i) {
  43. if (keysym == keysym_to_keycode_table[i].keysym) {
  44. return keysym_to_keycode_table[i].keycode;
  45. }
  46. }
  47. }
  48. if (!sdl_keycode) {
  49. const SDL_Scancode scancode = SDL_GetScancodeFromKeySym(keysym, keycode);
  50. if (scancode != SDL_SCANCODE_UNKNOWN) {
  51. sdl_keycode = SDL_GetKeymapKeycode(NULL, scancode, modifiers);
  52. }
  53. }
  54. return sdl_keycode;
  55. }
  56. #endif // SDL_VIDEO_DRIVER_WAYLAND || SDL_VIDEO_DRIVER_X11