summaryrefslogtreecommitdiff
path: root/quantum/process_keycode/process_repeat_key.c
diff options
context:
space:
mode:
authorPascal Getreuer <50221757+getreuer@users.noreply.github.com>2023-05-20 05:35:06 -0700
committerGitHub <noreply@github.com>2023-05-20 22:35:06 +1000
commit3993b15f054265071730cdb450f43457dcf4c64a (patch)
tree61c5b980ed14428bae3c0278c27937dbb5d33627 /quantum/process_keycode/process_repeat_key.c
parente1766df185869d8a591228d37f3f7b6d5b4049b4 (diff)
[Core] Add Repeat Key ("repeat last key") as a core feature. (#19700)
Co-authored-by: casuanoob <96005765+casuanoob@users.noreply.github.com> Co-authored-by: Sergey Vlasov <sigprof@gmail.com>
Diffstat (limited to 'quantum/process_keycode/process_repeat_key.c')
-rw-r--r--quantum/process_keycode/process_repeat_key.c109
1 files changed, 109 insertions, 0 deletions
diff --git a/quantum/process_keycode/process_repeat_key.c b/quantum/process_keycode/process_repeat_key.c
new file mode 100644
index 0000000000..f819aa226e
--- /dev/null
+++ b/quantum/process_keycode/process_repeat_key.c
@@ -0,0 +1,109 @@
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
15#include "process_repeat_key.h"
16
17// Default implementation of remember_last_key_user().
18__attribute__((weak)) bool remember_last_key_user(uint16_t keycode, keyrecord_t* record, uint8_t* remembered_mods) {
19 return true;
20}
21
22static bool remember_last_key(uint16_t keycode, keyrecord_t* record, uint8_t* remembered_mods) {
23 switch (keycode) {
24 // Ignore MO, TO, TG, TT, and TL layer switch keys.
25 case QK_MOMENTARY ... QK_MOMENTARY_MAX:
26 case QK_TO ... QK_TO_MAX:
27 case QK_TOGGLE_LAYER ... QK_TOGGLE_LAYER_MAX:
28 case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX:
29 // Ignore mod keys.
30 case KC_LCTL ... KC_RGUI:
31 case KC_HYPR:
32 case KC_MEH:
33#ifndef NO_ACTION_ONESHOT // Ignore one-shot keys.
34 case QK_ONE_SHOT_LAYER ... QK_ONE_SHOT_LAYER_MAX:
35 case QK_ONE_SHOT_MOD ... QK_ONE_SHOT_MOD_MAX:
36#endif // NO_ACTION_ONESHOT
37#ifdef TRI_LAYER_ENABLE // Ignore Tri Layer keys.
38 case QK_TRI_LAYER_LOWER:
39 case QK_TRI_LAYER_UPPER:
40#endif // TRI_LAYER_ENABLE
41 return false;
42
43 // Ignore hold events on tap-hold keys.
44#ifndef NO_ACTION_TAPPING
45 case QK_MOD_TAP ... QK_MOD_TAP_MAX:
46# ifndef NO_ACTION_LAYER
47 case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
48# endif // NO_ACTION_LAYER
49 if (record->tap.count == 0) {
50 return false;
51 }
52 break;
53#endif // NO_ACTION_TAPPING
54
55#ifdef SWAP_HANDS_ENABLE
56 case QK_SWAP_HANDS ... QK_SWAP_HANDS_MAX:
57 if (IS_SWAP_HANDS_KEYCODE(keycode) || record->tap.count == 0) {
58 return false;
59 }
60 break;
61#endif // SWAP_HANDS_ENABLE
62
63 case QK_REPEAT_KEY:
64#ifndef NO_ALT_REPEAT_KEY
65 case QK_ALT_REPEAT_KEY:
66#endif // NO_ALT_REPEAT_KEY
67 return false;
68 }
69
70 return remember_last_key_user(keycode, record, remembered_mods);
71}
72
73bool process_last_key(uint16_t keycode, keyrecord_t* record) {
74 if (get_repeat_key_count()) {
75 return true;
76 }
77
78 if (record->event.pressed) {
79 uint8_t remembered_mods = get_mods() | get_weak_mods();
80#ifndef NO_ACTION_ONESHOT
81 remembered_mods |= get_oneshot_mods();
82#endif // NO_ACTION_ONESHOT
83
84 if (remember_last_key(keycode, record, &remembered_mods)) {
85 set_last_record(keycode, record);
86 set_last_mods(remembered_mods);
87 }
88 }
89
90 return true;
91}
92
93bool process_repeat_key(uint16_t keycode, keyrecord_t* record) {
94 if (get_repeat_key_count()) {
95 return true;
96 }
97
98 if (keycode == QK_REPEAT_KEY) {
99 repeat_key_invoke(&record->event);
100 return false;
101#ifndef NO_ALT_REPEAT_KEY
102 } else if (keycode == QK_ALT_REPEAT_KEY) {
103 alt_repeat_key_invoke(&record->event);
104 return false;
105#endif // NO_ALT_REPEAT_KEY
106 }
107
108 return true;
109}