You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

81 lines
2.3 KiB

  1. // Copyright 2021 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. //
  15. //
  16. // For full documentation, see
  17. // https://getreuer.info/posts/keyboards/caps-word
  18. #include "caps_word.h"
  19. bool process_caps_word(uint16_t keycode, keyrecord_t* record) {
  20. static bool caps_word_enabled = false;
  21. static bool shifted = false;
  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 (!caps_word_enabled) {
  28. // Pressing both shift keys at the same time enables caps word.
  29. if ((mods & MOD_MASK_SHIFT) == MOD_MASK_SHIFT) {
  30. clear_mods();
  31. #ifndef NO_ACTION_ONESHOT
  32. clear_oneshot_mods();
  33. #endif // NO_ACTION_ONESHOT
  34. shifted = false;
  35. caps_word_enabled = true;
  36. return false;
  37. }
  38. return true;
  39. }
  40. if (!record->event.pressed) { return true; }
  41. if (!(mods & ~MOD_MASK_SHIFT)) {
  42. switch (keycode) {
  43. case QK_MOD_TAP ... QK_MOD_TAP_MAX:
  44. case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
  45. // Earlier return if this has not been considered tapped yet.
  46. if (record->tap.count == 0) { return true; }
  47. // Get the base tapping keycode of a mod- or layer-tap key.
  48. keycode &= 0xff;
  49. }
  50. switch (keycode) {
  51. // Letter keys should be shifted.
  52. case KC_A ... KC_Z:
  53. if (!shifted) { register_code(KC_LSFT); }
  54. shifted = true;
  55. return true;
  56. // Keycodes that continue caps word but shouldn't get shifted.
  57. case KC_1 ... KC_0:
  58. case KC_BSPC:
  59. case KC_MINS:
  60. case KC_UNDS:
  61. if (shifted) { unregister_code(KC_LSFT); }
  62. shifted = false;
  63. return true;
  64. // Any other keycode disables caps word.
  65. }
  66. }
  67. // Disable caps word.
  68. caps_word_enabled = false;
  69. if (shifted) { unregister_code(KC_LSFT); }
  70. shifted = false;
  71. return true;
  72. }