process_caps_word.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. // Copyright 2021-2022 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. #include "process_caps_word.h"
  15. #ifdef CAPS_WORD_INVERT_ON_SHIFT
  16. static uint8_t held_mods = 0;
  17. static bool handle_shift(uint16_t keycode, keyrecord_t* record) {
  18. switch (keycode) {
  19. case OSM(MOD_LSFT):
  20. keycode = KC_LSFT;
  21. break;
  22. case OSM(MOD_RSFT):
  23. keycode = KC_RSFT;
  24. break;
  25. # ifndef NO_ACTION_TAPPING
  26. case QK_MOD_TAP ... QK_MOD_TAP_MAX:
  27. if (record->tap.count == 0) { // Mod-tap key is held.
  28. switch (QK_MOD_TAP_GET_MODS(keycode)) {
  29. case MOD_LSFT:
  30. keycode = KC_LSFT;
  31. break;
  32. case MOD_RSFT:
  33. keycode = KC_RSFT;
  34. break;
  35. }
  36. }
  37. # endif // NO_ACTION_TAPPING
  38. }
  39. if (keycode == KC_LSFT || keycode == KC_RSFT) {
  40. const uint8_t mod = MOD_BIT(keycode);
  41. if (is_caps_word_on()) {
  42. if (record->event.pressed) {
  43. held_mods |= mod;
  44. } else {
  45. held_mods &= ~mod;
  46. }
  47. return false;
  48. } else if ((held_mods & mod) != 0) {
  49. held_mods &= ~mod;
  50. del_mods(mod);
  51. return record->event.pressed;
  52. }
  53. }
  54. return true;
  55. }
  56. #endif // CAPS_WORD_INVERT_ON_SHIFT
  57. bool process_caps_word(uint16_t keycode, keyrecord_t* record) {
  58. if (keycode == QK_CAPS_WORD_TOGGLE) {
  59. if (record->event.pressed) {
  60. caps_word_toggle();
  61. }
  62. return false;
  63. }
  64. #ifdef CAPS_WORD_INVERT_ON_SHIFT
  65. if (!handle_shift(keycode, record)) {
  66. return false;
  67. }
  68. #endif // CAPS_WORD_INVERT_ON_SHIFT
  69. #ifndef NO_ACTION_ONESHOT
  70. const uint8_t mods = get_mods() | get_oneshot_mods();
  71. #else
  72. const uint8_t mods = get_mods();
  73. #endif // NO_ACTION_ONESHOT
  74. if (!is_caps_word_on()) {
  75. // The following optionally turns on Caps Word by holding left and
  76. // right shifts or by double tapping left shift. This way Caps Word
  77. // may be used without needing a dedicated key and also without
  78. // needing combos or tap dance.
  79. #ifdef BOTH_SHIFTS_TURNS_ON_CAPS_WORD
  80. // Many keyboards enable the Command feature by default, which also
  81. // uses left+right shift. It can be configured to use a different
  82. // key combination by defining IS_COMMAND(). We make a non-fatal
  83. // warning if Command is enabled but IS_COMMAND() is *not* defined.
  84. # if defined(COMMAND_ENABLE) && !defined(IS_COMMAND)
  85. # pragma message "BOTH_SHIFTS_TURNS_ON_CAPS_WORD and Command should not be enabled at the same time, since both use the Left Shift + Right Shift key combination. Please disable Command, or ensure that `IS_COMMAND` is not set to (get_mods() == MOD_MASK_SHIFT)."
  86. # else
  87. if (mods == MOD_MASK_SHIFT
  88. # ifdef COMMAND_ENABLE
  89. // Don't activate Caps Word at the same time as Command.
  90. && !(IS_COMMAND())
  91. # endif // COMMAND_ENABLE
  92. ) {
  93. caps_word_on();
  94. }
  95. # endif // defined(COMMAND_ENABLE) && !defined(IS_COMMAND)
  96. #endif // BOTH_SHIFTS_TURNS_ON_CAPS_WORD
  97. #ifdef DOUBLE_TAP_SHIFT_TURNS_ON_CAPS_WORD
  98. // Double tapping left shift turns on Caps Word.
  99. //
  100. // NOTE: This works with KC_LSFT and one-shot left shift. It
  101. // wouldn't make sense with mod-tap or Space Cadet shift since
  102. // double tapping would of course trigger the tapping action.
  103. if (record->event.pressed) {
  104. static bool tapped = false;
  105. static uint16_t timer = 0;
  106. if (keycode == KC_LSFT || keycode == OSM(MOD_LSFT)) {
  107. if (tapped && !timer_expired(record->event.time, timer)) {
  108. // Left shift was double tapped, activate Caps Word.
  109. caps_word_on();
  110. }
  111. tapped = true;
  112. timer = record->event.time + GET_TAPPING_TERM(keycode, record);
  113. } else {
  114. tapped = false; // Reset when any other key is pressed.
  115. }
  116. }
  117. #endif // DOUBLE_TAP_SHIFT_TURNS_ON_CAPS_WORD
  118. return true;
  119. }
  120. #if CAPS_WORD_IDLE_TIMEOUT > 0
  121. caps_word_reset_idle_timer();
  122. #endif // CAPS_WORD_IDLE_TIMEOUT > 0
  123. // From here on, we only take action on press events.
  124. if (!record->event.pressed) {
  125. return true;
  126. }
  127. if (!(mods & ~(MOD_MASK_SHIFT | MOD_BIT(KC_RALT)))) {
  128. switch (keycode) {
  129. // Ignore MO, TO, TG, TT, and OSL layer switch keys.
  130. case QK_MOMENTARY ... QK_MOMENTARY_MAX:
  131. case QK_TO ... QK_TO_MAX:
  132. case QK_TOGGLE_LAYER ... QK_TOGGLE_LAYER_MAX:
  133. case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX:
  134. case QK_ONE_SHOT_LAYER ... QK_ONE_SHOT_LAYER_MAX:
  135. case QK_TRI_LAYER_LOWER ... QK_TRI_LAYER_UPPER:
  136. // Ignore AltGr.
  137. case KC_RALT:
  138. case OSM(MOD_RALT):
  139. return true;
  140. #ifndef NO_ACTION_TAPPING
  141. // Corresponding to mod keys above, a held mod-tap is handled as:
  142. // * For shift mods, pass KC_LSFT or KC_RSFT to
  143. // caps_word_press_user() to determine whether to continue.
  144. // * For Shift + AltGr (MOD_RSFT | MOD_RALT), pass RSFT(KC_RALT).
  145. // * AltGr (MOD_RALT) is ignored.
  146. // * Otherwise stop Caps Word.
  147. case QK_MOD_TAP ... QK_MOD_TAP_MAX:
  148. if (record->tap.count == 0) { // Mod-tap key is held.
  149. const uint8_t mods = QK_MOD_TAP_GET_MODS(keycode);
  150. switch (mods) {
  151. # ifndef CAPS_WORD_INVERT_ON_SHIFT
  152. case MOD_LSFT:
  153. keycode = KC_LSFT;
  154. break;
  155. case MOD_RSFT:
  156. keycode = KC_RSFT;
  157. break;
  158. # endif // CAPS_WORD_INVERT_ON_SHIFT
  159. case MOD_RSFT | MOD_RALT:
  160. keycode = RSFT(KC_RALT);
  161. break;
  162. case MOD_RALT:
  163. return true;
  164. default:
  165. caps_word_off();
  166. # ifdef CAPS_WORD_INVERT_ON_SHIFT
  167. add_mods(held_mods);
  168. # endif // CAPS_WORD_INVERT_ON_SHIFT
  169. return true;
  170. }
  171. } else {
  172. keycode = QK_MOD_TAP_GET_TAP_KEYCODE(keycode);
  173. }
  174. break;
  175. # ifndef NO_ACTION_LAYER
  176. case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
  177. # endif // NO_ACTION_LAYER
  178. if (record->tap.count == 0) {
  179. return true;
  180. }
  181. keycode = QK_LAYER_TAP_GET_TAP_KEYCODE(keycode);
  182. break;
  183. #endif // NO_ACTION_TAPPING
  184. #ifdef SWAP_HANDS_ENABLE
  185. case QK_SWAP_HANDS ... QK_SWAP_HANDS_MAX:
  186. // Note: IS_SWAP_HANDS_KEYCODE() actually tests for the special action keycodes like SH_TOGG, SH_TT, ...,
  187. // which currently overlap the SH_T(kc) range.
  188. if (IS_SWAP_HANDS_KEYCODE(keycode)
  189. # ifndef NO_ACTION_TAPPING
  190. || record->tap.count == 0
  191. # endif // NO_ACTION_TAPPING
  192. ) {
  193. return true;
  194. }
  195. keycode = QK_SWAP_HANDS_GET_TAP_KEYCODE(keycode);
  196. break;
  197. #endif // SWAP_HANDS_ENABLE
  198. }
  199. #ifdef AUTO_SHIFT_ENABLE
  200. del_weak_mods(get_autoshift_state() ? ~MOD_BIT(KC_LSFT) : 0xff);
  201. #else
  202. clear_weak_mods();
  203. #endif // AUTO_SHIFT_ENABLE
  204. if (caps_word_press_user(keycode)) {
  205. #ifdef CAPS_WORD_INVERT_ON_SHIFT
  206. if (held_mods) {
  207. set_weak_mods(get_weak_mods() ^ MOD_BIT(KC_LSFT));
  208. }
  209. #endif // CAPS_WORD_INVERT_ON_SHIFT
  210. send_keyboard_report();
  211. return true;
  212. }
  213. }
  214. caps_word_off();
  215. #ifdef CAPS_WORD_INVERT_ON_SHIFT
  216. add_mods(held_mods);
  217. #endif // CAPS_WORD_INVERT_ON_SHIFT
  218. return true;
  219. }
  220. __attribute__((weak)) bool caps_word_press_user(uint16_t keycode) {
  221. switch (keycode) {
  222. // Keycodes that continue Caps Word, with shift applied.
  223. case KC_A ... KC_Z:
  224. case KC_MINS:
  225. add_weak_mods(MOD_BIT(KC_LSFT)); // Apply shift to next key.
  226. return true;
  227. // Keycodes that continue Caps Word, without shifting.
  228. case KC_1 ... KC_0:
  229. case KC_BSPC:
  230. case KC_DEL:
  231. case KC_UNDS:
  232. return true;
  233. default:
  234. return false; // Deactivate Caps Word.
  235. }
  236. }