process_repeat_key.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright 2022-2023 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_repeat_key.h"
  15. // Default implementation of remember_last_key_user().
  16. __attribute__((weak)) bool remember_last_key_user(uint16_t keycode, keyrecord_t* record, uint8_t* remembered_mods) {
  17. return true;
  18. }
  19. static bool remember_last_key(uint16_t keycode, keyrecord_t* record, uint8_t* remembered_mods) {
  20. switch (keycode) {
  21. // Ignore MO, TO, TG, TT, and TL layer switch keys.
  22. case QK_MOMENTARY ... QK_MOMENTARY_MAX:
  23. case QK_TO ... QK_TO_MAX:
  24. case QK_TOGGLE_LAYER ... QK_TOGGLE_LAYER_MAX:
  25. case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX:
  26. // Ignore mod keys.
  27. case KC_LCTL ... KC_RGUI:
  28. case KC_HYPR:
  29. case KC_MEH:
  30. #ifndef NO_ACTION_ONESHOT // Ignore one-shot keys.
  31. case QK_ONE_SHOT_LAYER ... QK_ONE_SHOT_LAYER_MAX:
  32. case QK_ONE_SHOT_MOD ... QK_ONE_SHOT_MOD_MAX:
  33. #endif // NO_ACTION_ONESHOT
  34. #ifdef TRI_LAYER_ENABLE // Ignore Tri Layer keys.
  35. case QK_TRI_LAYER_LOWER:
  36. case QK_TRI_LAYER_UPPER:
  37. #endif // TRI_LAYER_ENABLE
  38. return false;
  39. // Ignore hold events on tap-hold keys.
  40. #ifndef NO_ACTION_TAPPING
  41. case QK_MOD_TAP ... QK_MOD_TAP_MAX:
  42. # ifndef NO_ACTION_LAYER
  43. case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
  44. # endif // NO_ACTION_LAYER
  45. if (record->tap.count == 0) {
  46. return false;
  47. }
  48. break;
  49. #endif // NO_ACTION_TAPPING
  50. #ifdef SWAP_HANDS_ENABLE
  51. case QK_SWAP_HANDS ... QK_SWAP_HANDS_MAX:
  52. if (IS_SWAP_HANDS_KEYCODE(keycode) || record->tap.count == 0) {
  53. return false;
  54. }
  55. break;
  56. #endif // SWAP_HANDS_ENABLE
  57. case QK_REPEAT_KEY:
  58. #ifndef NO_ALT_REPEAT_KEY
  59. case QK_ALT_REPEAT_KEY:
  60. #endif // NO_ALT_REPEAT_KEY
  61. return false;
  62. }
  63. return remember_last_key_user(keycode, record, remembered_mods);
  64. }
  65. bool process_last_key(uint16_t keycode, keyrecord_t* record) {
  66. if (get_repeat_key_count()) {
  67. return true;
  68. }
  69. if (record->event.pressed) {
  70. uint8_t remembered_mods = get_mods() | get_weak_mods();
  71. #ifndef NO_ACTION_ONESHOT
  72. remembered_mods |= get_oneshot_mods();
  73. #endif // NO_ACTION_ONESHOT
  74. if (remember_last_key(keycode, record, &remembered_mods)) {
  75. set_last_record(keycode, record);
  76. set_last_mods(remembered_mods);
  77. }
  78. }
  79. return true;
  80. }
  81. bool process_repeat_key(uint16_t keycode, keyrecord_t* record) {
  82. if (get_repeat_key_count()) {
  83. return true;
  84. }
  85. if (keycode == QK_REPEAT_KEY) {
  86. repeat_key_invoke(&record->event);
  87. return false;
  88. #ifndef NO_ALT_REPEAT_KEY
  89. } else if (keycode == QK_ALT_REPEAT_KEY) {
  90. alt_repeat_key_invoke(&record->event);
  91. return false;
  92. #endif // NO_ALT_REPEAT_KEY
  93. }
  94. return true;
  95. }