logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

keycode_string.h (5132B)


  1. // Copyright 2024-2025 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include <stdint.h>
  16. #if KEYCODE_STRING_ENABLE
  17. /**
  18. * @brief Formats a QMK keycode as a human-readable string.
  19. *
  20. * Given a keycode, like `KC_A`, this function returns a formatted string, like
  21. * "KC_A". This is useful for debugging and diagnostics so that keys are more
  22. * easily identified than they would be by raw numerical codes.
  23. *
  24. * @note The returned char* string should be used right away. The string memory
  25. * is reused and will be overwritten by the next call to `keycode_string()`.
  26. *
  27. * Many common QMK keycodes are understood by this function, but not all.
  28. * Recognized keycodes include:
  29. *
  30. * - Most basic keycodes, including letters `KC_A` - `KC_Z`, digits `KC_0` -
  31. * `KC_9`, function keys `KC_F1` - `KC_F24`, and modifiers like `KC_LSFT`.
  32. *
  33. * - Modified basic keycodes, like `S(KC_1)` (Shift + 1 = !).
  34. *
  35. * - `MO`, `TO`, `TG`, `OSL`, `LM(layer,mod)`, `LT(layer,kc)` layer switches.
  36. *
  37. * - One-shot mod `OSM(mod)` keycodes.
  38. *
  39. * - Mod-tap `MT(mod, kc)` keycodes.
  40. *
  41. * - Tap dance keycodes `TD(i)`.
  42. *
  43. * - Swap hands keycodes `SH_T(kc)`, `SH_TOGG`, etc.
  44. *
  45. * - Joystick keycodes `JS_n`.
  46. *
  47. * - Programmable button keycodes `PB_n`.
  48. *
  49. * - Unicode `UC(codepoint)` and Unicode Map `UM(i)` and `UP(i,j)` keycodes.
  50. *
  51. * - Keyboard range keycodes `QK_KB_*`.
  52. *
  53. * - User range (SAFE_RANGE) keycodes `QK_USER_*`.
  54. *
  55. * Keycodes involving mods like `OSM`, `LM`, `MT` are fully supported only where
  56. * a single mod is applied.
  57. *
  58. * Unrecognized keycodes are printed numerically as hex values like `0x1ABC`.
  59. *
  60. * Optionally, use `keycode_string_names_user` or `keycode_string_names_kb` to
  61. * define names for additional keycodes or override how any of the above are
  62. * formatted.
  63. *
  64. * @param keycode QMK keycode.
  65. * @return Stringified keycode.
  66. */
  67. const char* get_keycode_string(uint16_t keycode);
  68. /** Defines a human-readable name for a keycode. */
  69. typedef struct {
  70. uint16_t keycode;
  71. const char* name;
  72. } keycode_string_name_t;
  73. // clang-format off
  74. /**
  75. * @brief Defines names for additional keycodes for `get_keycode_string()`.
  76. *
  77. * Define `KEYCODE_STRING_NAMES_USER` in your keymap.c to add names for
  78. * additional keycodes to `keycode_string()`. This table may also be used to
  79. * override how `keycode_string()` formats a keycode. For example, supposing
  80. * keymap.c defines `MYMACRO1` and `MYMACRO2` as custom keycodes:
  81. *
  82. * KEYCODE_STRING_NAMES_USER(
  83. * KEYCODE_STRING_NAME(MYMACRO1),
  84. * KEYCODE_STRING_NAME(MYMACRO2),
  85. * KEYCODE_STRING_NAME(KC_EXLM),
  86. * );
  87. *
  88. * The above defines names for `MYMACRO1` and `MYMACRO2`, and overrides
  89. * `KC_EXLM` to format as "KC_EXLM" instead of the default "S(KC_1)".
  90. */
  91. # define KEYCODE_STRING_NAMES_USER(...) \
  92. static const keycode_string_name_t keycode_string_names_user[] = {__VA_ARGS__}; \
  93. uint16_t keycode_string_names_size_user = \
  94. sizeof(keycode_string_names_user) / sizeof(keycode_string_name_t); \
  95. const keycode_string_name_t* keycode_string_names_data_user = \
  96. keycode_string_names_user
  97. /** Same as above, but defines keycode string names at the keyboard level. */
  98. # define KEYCODE_STRING_NAMES_KB(...) \
  99. static const keycode_string_name_t keycode_string_names_kb[] = {__VA_ARGS__}; \
  100. uint16_t keycode_string_names_size_kb = \
  101. sizeof(keycode_string_names_kb) / sizeof(keycode_string_name_t); \
  102. const keycode_string_name_t* keycode_string_names_data_kb = \
  103. keycode_string_names_kb
  104. /** Helper to define a keycode_string_name_t. */
  105. # define KEYCODE_STRING_NAME(kc) \
  106. { (kc), #kc }
  107. // clang-format on
  108. extern const keycode_string_name_t* keycode_string_names_data_user;
  109. extern uint16_t keycode_string_names_size_user;
  110. extern const keycode_string_name_t* keycode_string_names_data_kb;
  111. extern uint16_t keycode_string_names_size_kb;
  112. #else
  113. // When keycode_string is disabled, fall back to printing keycodes numerically
  114. // as decimal values, using get_u16_str() from quantum.c.
  115. # define get_keycode_string(kc) get_u16_str(kc, ' ')
  116. const char* get_u16_str(uint16_t curr_num, char curr_pad);
  117. # define KEYCODE_STRING_NAMES_USER(...)
  118. # define KEYCODE_STRING_NAMES_KB(...)
  119. # define KEYCODE_STRING_NAME(kc)
  120. #endif // KEYCODE_STRING_ENABLE