process_caps_word.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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)) {
  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. return true;
  89. #ifndef NO_ACTION_TAPPING
  90. case QK_MOD_TAP ... QK_MOD_TAP_MAX:
  91. if (record->tap.count == 0) {
  92. // Deactivate if a mod becomes active through holding
  93. // a mod-tap key.
  94. caps_word_off();
  95. return true;
  96. }
  97. keycode &= 0xff;
  98. break;
  99. # ifndef NO_ACTION_LAYER
  100. case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
  101. # endif // NO_ACTION_LAYER
  102. if (record->tap.count == 0) {
  103. return true;
  104. }
  105. keycode &= 0xff;
  106. break;
  107. #endif // NO_ACTION_TAPPING
  108. #ifdef SWAP_HANDS_ENABLE
  109. case QK_SWAP_HANDS ... QK_SWAP_HANDS_MAX:
  110. if (keycode > 0x56F0 || record->tap.count == 0) {
  111. return true;
  112. }
  113. keycode &= 0xff;
  114. break;
  115. #endif // SWAP_HANDS_ENABLE
  116. }
  117. clear_weak_mods();
  118. if (caps_word_press_user(keycode)) {
  119. send_keyboard_report();
  120. return true;
  121. }
  122. }
  123. caps_word_off();
  124. return true;
  125. }
  126. __attribute__((weak)) bool caps_word_press_user(uint16_t keycode) {
  127. switch (keycode) {
  128. // Keycodes that continue Caps Word, with shift applied.
  129. case KC_A ... KC_Z:
  130. case KC_MINS:
  131. add_weak_mods(MOD_BIT(KC_LSFT)); // Apply shift to next key.
  132. return true;
  133. // Keycodes that continue Caps Word, without shifting.
  134. case KC_1 ... KC_0:
  135. case KC_BSPC:
  136. case KC_DEL:
  137. case KC_UNDS:
  138. return true;
  139. default:
  140. return false; // Deactivate Caps Word.
  141. }
  142. }