qmk-config
qmk configs for my open-source keyboards
git clone https://9o.is/git/qmk-config.git
achordion.h
(6727B)
1 // Copyright 2022-2024 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 * @file achordion.h
17 * @brief Achordion: Customizing the tap-hold decision.
18 *
19 * Overview
20 * --------
21 *
22 * This library customizes when tap-hold keys are considered held vs. tapped
23 * based on the next pressed key, like Manna Harbour's Bilateral Combinations or
24 * ZMK's positional hold. The library works on top of QMK's existing tap-hold
25 * implementation. You define mod-tap and layer-tap keys as usual and use
26 * Achordion to fine-tune the behavior.
27 *
28 * When QMK settles a tap-hold key as held, Achordion intercepts the event.
29 * Achordion then revises the event as a tap or passes it along as a hold:
30 *
31 * * Chord condition: On the next key press, a customizable `achordion_chord()`
32 * function is called, which takes the tap-hold key and the next key pressed
33 * as args. When the function returns true, the tap-hold key is settled as
34 * held, and otherwise as tapped.
35 *
36 * * Timeout: If no other key press occurs within a timeout, the tap-hold key
37 * is settled as held. This is customizable with `achordion_timeout()`.
38 *
39 * Achordion only changes the behavior when QMK considered the key held. It
40 * changes some would-be holds to taps, but no taps to holds.
41 *
42 * @note Some QMK features handle events before the point where Achordion can
43 * intercept them, particularly: Combos, Key Lock, and Dynamic Macros. It's
44 * still possible to use these features and Achordion in your keymap, but beware
45 * they might behave poorly when used simultaneously with tap-hold keys.
46 *
47 *
48 * For full documentation, see
49 * <https://getreuer.info/posts/keyboards/achordion>
50 */
51
52 #pragma once
53
54 #include "quantum.h"
55
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59
60 /**
61 * Handler function for Achordion.
62 *
63 * Call this function from `process_record_user()` as
64 *
65 * #include "features/achordion.h"
66 *
67 * bool process_record_user(uint16_t keycode, keyrecord_t* record) {
68 * if (!process_achordion(keycode, record)) { return false; }
69 * // Your macros...
70 * return true;
71 * }
72 */
73 bool process_achordion(uint16_t keycode, keyrecord_t* record);
74
75 /**
76 * Matrix task function for Achordion.
77 *
78 * Call this function from `matrix_scan_user()` as
79 *
80 * void matrix_scan_user(void) {
81 * achordion_task();
82 * }
83 */
84 void achordion_task(void);
85
86 /**
87 * Optional callback to customize which key chords are considered "held".
88 *
89 * In your keymap.c, define the callback
90 *
91 * bool achordion_chord(uint16_t tap_hold_keycode,
92 * keyrecord_t* tap_hold_record,
93 * uint16_t other_keycode,
94 * keyrecord_t* other_record) {
95 * // Conditions...
96 * }
97 *
98 * This callback is called if while `tap_hold_keycode` is pressed,
99 * `other_keycode` is pressed. Return true if the tap-hold key should be
100 * considered held, or false to consider it tapped.
101 *
102 * @param tap_hold_keycode Keycode of the tap-hold key.
103 * @param tap_hold_record keyrecord_t from the tap-hold press event.
104 * @param other_keycode Keycode of the other key.
105 * @param other_record keyrecord_t from the other key's press event.
106 * @return True if the tap-hold key should be considered held.
107 */
108 bool achordion_chord(uint16_t tap_hold_keycode, keyrecord_t* tap_hold_record,
109 uint16_t other_keycode, keyrecord_t* other_record);
110
111 /**
112 * Optional callback to define a timeout duration per keycode.
113 *
114 * In your keymap.c, define the callback
115 *
116 * uint16_t achordion_timeout(uint16_t tap_hold_keycode) {
117 * // ...
118 * }
119 *
120 * The callback determines Achordion's timeout duration for `tap_hold_keycode`
121 * in units of milliseconds. The timeout be in the range 0 to 32767 ms (upper
122 * bound is due to 16-bit timer limitations). Use a timeout of 0 to bypass
123 * Achordion.
124 *
125 * @param tap_hold_keycode Keycode of the tap-hold key.
126 * @return Timeout duration in milliseconds in the range 0 to 32767.
127 */
128 uint16_t achordion_timeout(uint16_t tap_hold_keycode);
129
130 /**
131 * Optional callback defining which mods are "eagerly" applied.
132 *
133 * This callback defines which mods are "eagerly" applied while a mod-tap
134 * key is still being settled. This is helpful to reduce delay particularly when
135 * using mod-tap keys with an external mouse.
136 *
137 * Define this callback in your keymap.c. The default callback is eager for
138 * Shift and Ctrl, and not for Alt and GUI:
139 *
140 * bool achordion_eager_mod(uint8_t mod) {
141 * return (mod & (MOD_LALT | MOD_LGUI)) == 0;
142 * }
143 *
144 * @note `mod` should be compared with `MOD_` prefixed codes, not `KC_` codes,
145 * described at <https://docs.qmk.fm/mod_tap>.
146 *
147 * @param mod Modifier `MOD_` code.
148 * @return True if the modifier should be eagerly applied.
149 */
150 bool achordion_eager_mod(uint8_t mod);
151
152 /**
153 * Returns true if the args come from keys on opposite hands.
154 *
155 * @param tap_hold_record keyrecord_t from the tap-hold key's event.
156 * @param other_record keyrecord_t from the other key's event.
157 * @return True if the keys are on opposite hands.
158 */
159 bool achordion_opposite_hands(const keyrecord_t* tap_hold_record,
160 const keyrecord_t* other_record);
161
162 /**
163 * Suppress tap-hold mods within a *typing streak* by defining
164 * ACHORDION_STREAK. This can help preventing accidental mod
165 * activation when performing a fast tapping sequence.
166 * This is inspired by
167 * https://sunaku.github.io/home-row-mods.html#typing-streaks
168 *
169 * Enable with:
170 *
171 * #define ACHORDION_STREAK
172 *
173 * Adjust the maximum time between key events before modifiers can be enabled
174 * by defining the following callback in your keymap.c:
175 *
176 * uint16_t achordion_streak_chord_timeout(
177 * uint16_t tap_hold_keycode, uint16_t next_keycode) {
178 * return 200; // Default of 200 ms.
179 * }
180 */
181 #ifdef ACHORDION_STREAK
182 uint16_t achordion_streak_chord_timeout(uint16_t tap_hold_keycode,
183 uint16_t next_keycode);
184
185 bool achordion_streak_continue(uint16_t keycode);
186
187 /** @deprecated Use `achordion_streak_chord_timeout()` instead. */
188 uint16_t achordion_streak_timeout(uint16_t tap_hold_keycode);
189 #endif
190
191 #ifdef __cplusplus
192 }
193 #endif
194