process_caps_word.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. bool process_caps_word(uint16_t keycode, keyrecord_t* record) {
  16. if (keycode == CAPSWRD) { // Pressing CAPSWRD toggles Caps Word.
  17. if (record->event.pressed) {
  18. caps_word_toggle();
  19. }
  20. return false;
  21. }
  22. #ifndef NO_ACTION_ONESHOT
  23. const uint8_t mods = get_mods() | get_oneshot_mods();
  24. #else
  25. const uint8_t mods = get_mods();
  26. #endif // NO_ACTION_ONESHOT
  27. if (!is_caps_word_on()) {
  28. // The following optionally turns on Caps Word by holding left and
  29. // right shifts or by double tapping left shift. This way Caps Word
  30. // may be used without needing a dedicated key and also without
  31. // needing combos or tap dance.
  32. #ifdef BOTH_SHIFTS_TURNS_ON_CAPS_WORD
  33. // Many keyboards enable the Command feature by default, which also
  34. // uses left+right shift. It can be configured to use a different
  35. // key combination by defining IS_COMMAND(). We make a non-fatal
  36. // warning if Command is enabled but IS_COMMAND() is *not* defined.
  37. # if defined(COMMAND_ENABLE) && !defined(IS_COMMAND)
  38. # 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)."
  39. # else
  40. if (mods == MOD_MASK_SHIFT
  41. # ifdef COMMAND_ENABLE
  42. // Don't activate Caps Word at the same time as Command.
  43. && !(IS_COMMAND())
  44. # endif // COMMAND_ENABLE
  45. ) {
  46. caps_word_on();
  47. }
  48. # endif // defined(COMMAND_ENABLE) && !defined(IS_COMMAND)
  49. #endif // BOTH_SHIFTS_TURNS_ON_CAPS_WORD
  50. #ifdef DOUBLE_TAP_SHIFT_TURNS_ON_CAPS_WORD
  51. // Double tapping left shift turns on Caps Word.
  52. //
  53. // NOTE: This works with KC_LSFT and one-shot left shift. It
  54. // wouldn't make sense with mod-tap or Space Cadet shift since
  55. // double tapping would of course trigger the tapping action.
  56. if (record->event.pressed) {
  57. static bool tapped = false;
  58. static uint16_t timer = 0;
  59. if (keycode == KC_LSFT || keycode == OSM(MOD_LSFT)) {
  60. if (tapped && !timer_expired(record->event.time, timer)) {
  61. // Left shift was double tapped, activate Caps Word.
  62. caps_word_on();
  63. }
  64. tapped = true;
  65. timer = record->event.time + GET_TAPPING_TERM(keycode, record);
  66. } else {
  67. tapped = false; // Reset when any other key is pressed.
  68. }
  69. }
  70. #endif // DOUBLE_TAP_SHIFT_TURNS_ON_CAPS_WORD
  71. return true;
  72. }
  73. #if CAPS_WORD_IDLE_TIMEOUT > 0
  74. caps_word_reset_idle_timer();
  75. #endif // CAPS_WORD_IDLE_TIMEOUT > 0
  76. // From here on, we only take action on press events.
  77. if (!record->event.pressed) {
  78. return true;
  79. }
  80. if (!(mods & ~(MOD_MASK_SHIFT | MOD_BIT(KC_RALT)))) {
  81. switch (keycode) {
  82. // Ignore MO, TO, TG, TT, and OSL layer switch keys.
  83. case QK_MOMENTARY ... QK_MOMENTARY_MAX:
  84. case QK_TO ... QK_TO_MAX:
  85. case QK_TOGGLE_LAYER ... QK_TOGGLE_LAYER_MAX:
  86. case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX:
  87. case QK_ONE_SHOT_LAYER ... QK_ONE_SHOT_LAYER_MAX:
  88. // Ignore AltGr.
  89. case KC_RALT:
  90. case OSM(MOD_RALT):
  91. return true;
  92. #ifndef NO_ACTION_TAPPING
  93. // Corresponding to mod keys above, a held mod-tap is handled as:
  94. // * For shift mods, pass KC_LSFT or KC_RSFT to
  95. // caps_word_press_user() to determine whether to continue.
  96. // * For Shift + AltGr (MOD_RSFT | MOD_RALT), pass RSFT(KC_RALT).
  97. // * AltGr (MOD_RALT) is ignored.
  98. // * Otherwise stop Caps Word.
  99. case QK_MOD_TAP ... QK_MOD_TAP_MAX:
  100. if (record->tap.count == 0) { // Mod-tap key is held.
  101. const uint8_t mods = (keycode >> 8) & 0x1f;
  102. switch (mods) {
  103. case MOD_LSFT:
  104. keycode = KC_LSFT;
  105. break;
  106. case MOD_RSFT:
  107. keycode = KC_RSFT;
  108. break;
  109. case MOD_RSFT | MOD_RALT:
  110. keycode = RSFT(KC_RALT);
  111. break;
  112. case MOD_RALT:
  113. return true;
  114. default:
  115. caps_word_off();
  116. return true;
  117. }
  118. } else {
  119. keycode &= 0xff;
  120. }
  121. break;
  122. # ifndef NO_ACTION_LAYER
  123. case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
  124. # endif // NO_ACTION_LAYER
  125. if (record->tap.count == 0) {
  126. return true;
  127. }
  128. keycode &= 0xff;
  129. break;
  130. #endif // NO_ACTION_TAPPING
  131. #ifdef SWAP_HANDS_ENABLE
  132. case QK_SWAP_HANDS ... QK_SWAP_HANDS_MAX:
  133. if (keycode > 0x56F0 || record->tap.count == 0) {
  134. return true;
  135. }
  136. keycode &= 0xff;
  137. break;
  138. #endif // SWAP_HANDS_ENABLE
  139. }
  140. #ifdef AUTO_SHIFT_ENABLE
  141. del_weak_mods(get_autoshift_state() ? ~MOD_BIT(KC_LSFT) : 0xff);
  142. #else
  143. clear_weak_mods();
  144. #endif // AUTO_SHIFT_ENABLE
  145. if (caps_word_press_user(keycode)) {
  146. send_keyboard_report();
  147. return true;
  148. }
  149. }
  150. caps_word_off();
  151. return true;
  152. }
  153. __attribute__((weak)) bool caps_word_press_user(uint16_t keycode) {
  154. switch (keycode) {
  155. // Keycodes that continue Caps Word, with shift applied.
  156. case KC_A ... KC_Z:
  157. case KC_MINS:
  158. add_weak_mods(MOD_BIT(KC_LSFT)); // Apply shift to next key.
  159. return true;
  160. // Keycodes that continue Caps Word, without shifting.
  161. case KC_1 ... KC_0:
  162. case KC_BSPC:
  163. case KC_DEL:
  164. case KC_UNDS:
  165. return true;
  166. default:
  167. return false; // Deactivate Caps Word.
  168. }
  169. }