aboutsummaryrefslogtreecommitdiff
path: root/docs/feature_tap_dance.md
diff options
context:
space:
mode:
authorRyan <fauxpark@gmail.com>2020-06-18 10:10:35 +1000
committerGitHub <noreply@github.com>2020-06-17 17:10:35 -0700
commit1a159a38ed127fa80762a7c1b8150ba19ac34ddf (patch)
tree218dfe2cdbe97001236d3ad6c5d3bbe1b587cfea /docs/feature_tap_dance.md
parenta0bf235644064e6a63f4df51ae501269bbd68652 (diff)
downloadqmk_firmware-1a159a38ed127fa80762a7c1b8150ba19ac34ddf.tar.gz
qmk_firmware-1a159a38ed127fa80762a7c1b8150ba19ac34ddf.zip
Clean up Tap Dance docs (#9372)
* Clean up Tap Dance docs * Add heading IDs for translation
Diffstat (limited to 'docs/feature_tap_dance.md')
-rw-r--r--docs/feature_tap_dance.md532
1 files changed, 260 insertions, 272 deletions
diff --git a/docs/feature_tap_dance.md b/docs/feature_tap_dance.md
index 2e8ab5350..877c37336 100644
--- a/docs/feature_tap_dance.md
+++ b/docs/feature_tap_dance.md
@@ -1,31 +1,24 @@
1# Tap Dance: A Single Key Can Do 3, 5, or 100 Different Things 1# Tap Dance: A Single Key Can Do 3, 5, or 100 Different Things
2 2
3## Introduction 3## Introduction :id=introduction
4
4Hit the semicolon key once, send a semicolon. Hit it twice, rapidly -- send a colon. Hit it three times, and your keyboard's LEDs do a wild dance. That's just one example of what Tap Dance can do. It's one of the nicest community-contributed features in the firmware, conceived and created by [algernon](https://github.com/algernon) in [#451](https://github.com/qmk/qmk_firmware/pull/451). Here's how algernon describes the feature: 5Hit the semicolon key once, send a semicolon. Hit it twice, rapidly -- send a colon. Hit it three times, and your keyboard's LEDs do a wild dance. That's just one example of what Tap Dance can do. It's one of the nicest community-contributed features in the firmware, conceived and created by [algernon](https://github.com/algernon) in [#451](https://github.com/qmk/qmk_firmware/pull/451). Here's how algernon describes the feature:
5 6
6With this feature one can specify keys that behave differently, based on the amount of times they have been tapped, and when interrupted, they get handled before the interrupter. 7With this feature one can specify keys that behave differently, based on the amount of times they have been tapped, and when interrupted, they get handled before the interrupter.
7 8
8## Explanatory Comparison with `ACTION_FUNCTION_TAP` 9## How to Use Tap Dance :id=how-to-use
9`ACTION_FUNCTION_TAP` can offer similar functionality to Tap Dance, but it's worth noting some important differences. To do this, let's explore a certain setup! We want one key to send `Space` on single-tap, but `Enter` on double-tap.
10
11With `ACTION_FUNCTION_TAP`, it is quite a rain-dance to set this up, and has the problem that when the sequence is interrupted, the interrupting key will be sent first. Thus, `SPC a` will result in `a SPC` being sent, if `SPC` and `a` are both typed within `TAPPING_TERM`. With the Tap Dance feature, that'll come out correctly as `SPC a` (even if both `SPC` and `a` are typed within the `TAPPING_TERM`.
12
13To achieve this correct handling of interrupts, the implementation of Tap Dance hooks into two parts of the system: `process_record_quantum()`, and the matrix scan. These two parts are explained below, but for now the point to note is that we need the latter to be able to time out a tap sequence even when a key is not being pressed. That way, `SPC` alone will time out and register after `TAPPING_TERM` time.
14 10
15## How to Use Tap Dance 11First, you will need `TAP_DANCE_ENABLE = yes` in your `rules.mk`, because the feature is disabled by default. This adds a little less than 1k to the firmware size.
16But enough of the generalities; lets look at how to actually use Tap Dance!
17
18First, you will need `TAP_DANCE_ENABLE=yes` in your `rules.mk`, because the feature is disabled by default. This adds a little less than 1k to the firmware size.
19 12
20Optionally, you might want to set a custom `TAPPING_TERM` time by adding something like this in you `config.h`: 13Optionally, you might want to set a custom `TAPPING_TERM` time by adding something like this in you `config.h`:
21 14
22``` 15```c
23#define TAPPING_TERM 175 16#define TAPPING_TERM 175
24``` 17```
25 18
26The `TAPPING_TERM` time is the maximum time allowed between taps of your Tap Dance key, and is measured in milliseconds. For example, if you used the above `#define` statement and set up a Tap Dance key that sends `Space` on single-tap and `Enter` on double-tap, then this key will send `ENT` only if you tap this key twice in less than 175ms. If you tap the key, wait more than 175ms, and tap the key again you'll end up sending `SPC SPC` instead. 19The `TAPPING_TERM` time is the maximum time allowed between taps of your Tap Dance key, and is measured in milliseconds. For example, if you used the above `#define` statement and set up a Tap Dance key that sends `Space` on single-tap and `Enter` on double-tap, then this key will send `ENT` only if you tap this key twice in less than 175ms. If you tap the key, wait more than 175ms, and tap the key again you'll end up sending `SPC SPC` instead.
27 20
28Next, you will want to define some tap-dance keys, which is easiest to do with the `TD()` macro, that - similar to `F()` - takes a number, which will later be used as an index into the `tap_dance_actions` array. 21Next, you will want to define some tap-dance keys, which is easiest to do with the `TD()` macro, that takes a number which will later be used as an index into the `tap_dance_actions` array.
29 22
30After this, you'll want to use the `tap_dance_actions` array to specify what actions shall be taken when a tap-dance key is in action. Currently, there are five possible options: 23After this, you'll want to use the `tap_dance_actions` array to specify what actions shall be taken when a tap-dance key is in action. Currently, there are five possible options:
31 24
@@ -43,11 +36,12 @@ The first option is enough for a lot of cases, that just want dual roles. For ex
43 36
44Similar to the first option, the second option is good for simple layer-switching cases. 37Similar to the first option, the second option is good for simple layer-switching cases.
45 38
46For more complicated cases, use the third or fourth options (examples of each are listed below). 39For more complicated cases, use the third or fourth options (examples of each are listed below).
47 40
48Finally, the fifth option is particularly useful if your non-Tap-Dance keys start behaving weirdly after adding the code for your Tap Dance keys. The likely problem is that you changed the `TAPPING_TERM` time to make your Tap Dance keys easier for you to use, and that this has changed the way your other keys handle interrupts. 41Finally, the fifth option is particularly useful if your non-Tap-Dance keys start behaving weirdly after adding the code for your Tap Dance keys. The likely problem is that you changed the `TAPPING_TERM` time to make your Tap Dance keys easier for you to use, and that this has changed the way your other keys handle interrupts.
49 42
50## Implementation Details 43## Implementation Details :id=implementation
44
51Well, that's the bulk of it! You should now be able to work through the examples below, and to develop your own Tap Dance functionality. But if you want a deeper understanding of what's going on behind the scenes, then read on for the explanation of how it all works! 45Well, that's the bulk of it! You should now be able to work through the examples below, and to develop your own Tap Dance functionality. But if you want a deeper understanding of what's going on behind the scenes, then read on for the explanation of how it all works!
52 46
53The main entry point is `process_tap_dance()`, called from `process_record_quantum()`, which is run for every keypress, and our handler gets to run early. This function checks whether the key pressed is a tap-dance key. If it is not, and a tap-dance was in action, we handle that first, and enqueue the newly pressed key. If it is a tap-dance key, then we check if it is the same as the already active one (if there's one active, that is). If it is not, we fire off the old one first, then register the new one. If it was the same, we increment the counter and reset the timer. 47The main entry point is `process_tap_dance()`, called from `process_record_quantum()`, which is run for every keypress, and our handler gets to run early. This function checks whether the key pressed is a tap-dance key. If it is not, and a tap-dance was in action, we handle that first, and enqueue the newly pressed key. If it is a tap-dance key, then we check if it is the same as the already active one (if there's one active, that is). If it is not, we fire off the old one first, then register the new one. If it was the same, we increment the counter and reset the timer.
@@ -58,9 +52,9 @@ Our next stop is `matrix_scan_tap_dance()`. This handles the timeout of tap-danc
58 52
59For the sake of flexibility, tap-dance actions can be either a pair of keycodes, or a user function. The latter allows one to handle higher tap counts, or do extra things, like blink the LEDs, fiddle with the backlighting, and so on. This is accomplished by using an union, and some clever macros. 53For the sake of flexibility, tap-dance actions can be either a pair of keycodes, or a user function. The latter allows one to handle higher tap counts, or do extra things, like blink the LEDs, fiddle with the backlighting, and so on. This is accomplished by using an union, and some clever macros.
60 54
61# Examples 55## Examples :id=examples
62 56
63## Simple Example 57### Simple Example :id=simple-example
64 58
65Here's a simple example for a single definition: 59Here's a simple example for a single definition:
66 60
@@ -69,23 +63,26 @@ Here's a simple example for a single definition:
693. In your `keymap.c` file, define the variables and definitions, then add to your keymap: 633. In your `keymap.c` file, define the variables and definitions, then add to your keymap:
70 64
71```c 65```c
72//Tap Dance Declarations 66// Tap Dance declarations
73enum { 67enum {
74 TD_ESC_CAPS = 0 68 TD_ESC_CAPS,
75}; 69};
76 70
77//Tap Dance Definitions 71// Tap Dance definitions
78qk_tap_dance_action_t tap_dance_actions[] = { 72qk_tap_dance_action_t tap_dance_actions[] = {
79 //Tap once for Esc, twice for Caps Lock 73 // Tap once for Escape, twice for Caps Lock
80 [TD_ESC_CAPS] = ACTION_TAP_DANCE_DOUBLE(KC_ESC, KC_CAPS) 74 [TD_ESC_CAPS] = ACTION_TAP_DANCE_DOUBLE(KC_ESC, KC_CAPS),
81// Other declarations would go here, separated by commas, if you have them
82}; 75};
83 76
84//In Layer declaration, add tap dance item in place of a key code 77// Add tap dance item in place of a key code
85TD(TD_ESC_CAPS) 78const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
79 // ...
80 TD(TD_ESC_CAPS)
81 // ...
82};
86``` 83```
87 84
88## Complex Examples 85### Complex Examples :id=complex-examples
89 86
90This section details several complex tap dance examples. 87This section details several complex tap dance examples.
91All the enums used in the examples are declared like this: 88All the enums used in the examples are declared like this:
@@ -93,104 +90,105 @@ All the enums used in the examples are declared like this:
93```c 90```c
94// Enums defined for all examples: 91// Enums defined for all examples:
95enum { 92enum {
96 CT_SE = 0, 93 CT_SE,
97 CT_CLN, 94 CT_CLN,
98 CT_EGG, 95 CT_EGG,
99 CT_FLSH, 96 CT_FLSH,
100 X_TAP_DANCE 97 X_TAP_DANCE
101}; 98};
102``` 99```
103### Example 1: Send `:` on Single Tap, `;` on Double Tap 100
101#### Example 1: Send `:` on Single Tap, `;` on Double Tap :id=example-1
102
104```c 103```c
105void dance_cln_finished (qk_tap_dance_state_t *state, void *user_data) { 104void dance_cln_finished(qk_tap_dance_state_t *state, void *user_data) {
106 if (state->count == 1) { 105 if (state->count == 1) {
107 register_code (KC_RSFT); 106 register_code16(KC_COLN);
108 register_code (KC_SCLN); 107 } else {
109 } else { 108 register_code(KC_SCLN);
110 register_code (KC_SCLN); 109 }
111 }
112} 110}
113 111
114void dance_cln_reset (qk_tap_dance_state_t *state, void *user_data) { 112void dance_cln_reset(qk_tap_dance_state_t *state, void *user_data) {
115 if (state->count == 1) { 113 if (state->count == 1) {
116 unregister_code (KC_RSFT); 114 unregister_code16(KC_COLN);
117 unregister_code (KC_SCLN); 115 } else {
118 } else { 116 unregister_code(KC_SCLN);
119 unregister_code (KC_SCLN); 117 }
120 }
121} 118}
122 119
123//All tap dance functions would go here. Only showing this one. 120// All tap dance functions would go here. Only showing this one.
124qk_tap_dance_action_t tap_dance_actions[] = { 121qk_tap_dance_action_t tap_dance_actions[] = {
125 [CT_CLN] = ACTION_TAP_DANCE_FN_ADVANCED (NULL, dance_cln_finished, dance_cln_reset) 122 [CT_CLN] = ACTION_TAP_DANCE_FN_ADVANCED(NULL, dance_cln_finished, dance_cln_reset),
126}; 123};
127``` 124```
128### Example 2: Send "Safety Dance!" After 100 Taps 125
126#### Example 2: Send "Safety Dance!" After 100 Taps :id=example-2
127
129```c 128```c
130void dance_egg (qk_tap_dance_state_t *state, void *user_data) { 129void dance_egg(qk_tap_dance_state_t *state, void *user_data) {
131 if (state->count >= 100) { 130 if (state->count >= 100) {
132 SEND_STRING ("Safety dance!"); 131 SEND_STRING("Safety dance!");
133 reset_tap_dance (state); 132 reset_tap_dance(state);
134 } 133 }
135} 134}
136 135
137qk_tap_dance_action_t tap_dance_actions[] = { 136qk_tap_dance_action_t tap_dance_actions[] = {
138 [CT_EGG] = ACTION_TAP_DANCE_FN (dance_egg) 137 [CT_EGG] = ACTION_TAP_DANCE_FN(dance_egg),
139}; 138};
140``` 139```
141 140
142### Example 3: Turn LED Lights On Then Off, One at a Time 141#### Example 3: Turn LED Lights On Then Off, One at a Time :id=example-3
143 142
144```c 143```c
145// on each tap, light up one led, from right to left 144// On each tap, light up one LED, from right to left
146// on the forth tap, turn them off from right to left 145// On the fourth tap, turn them off from right to left
147void dance_flsh_each(qk_tap_dance_state_t *state, void *user_data) { 146void dance_flsh_each(qk_tap_dance_state_t *state, void *user_data) {
148 switch (state->count) { 147 switch (state->count) {
149 case 1: 148 case 1:
150 ergodox_right_led_3_on(); 149 ergodox_right_led_3_on();
151 break; 150 break;
152 case 2: 151 case 2:
153 ergodox_right_led_2_on(); 152 ergodox_right_led_2_on();
154 break; 153 break;
155 case 3: 154 case 3:
156 ergodox_right_led_1_on(); 155 ergodox_right_led_1_on();
157 break; 156 break;
158 case 4: 157 case 4:
159 ergodox_right_led_3_off(); 158 ergodox_right_led_3_off();
160 _delay_ms(50); 159 wait_ms(50);
161 ergodox_right_led_2_off(); 160 ergodox_right_led_2_off();
162 _delay_ms(50); 161 wait_ms(50);
163 ergodox_right_led_1_off(); 162 ergodox_right_led_1_off();
164 } 163 }
165} 164}
166 165
167// on the fourth tap, set the keyboard on flash state 166// On the fourth tap, set the keyboard on flash state
168void dance_flsh_finished(qk_tap_dance_state_t *state, void *user_data) { 167void dance_flsh_finished(qk_tap_dance_state_t *state, void *user_data) {
169 if (state->count >= 4) { 168 if (state->count >= 4) {
170 reset_keyboard(); 169 reset_keyboard();
171 reset_tap_dance(state); 170 }
172 }
173} 171}
174 172
175// if the flash state didn't happen, then turn off LEDs, left to right 173// If the flash state didn't happen, then turn off LEDs, left to right
176void dance_flsh_reset(qk_tap_dance_state_t *state, void *user_data) { 174void dance_flsh_reset(qk_tap_dance_state_t *state, void *user_data) {
177 ergodox_right_led_1_off(); 175 ergodox_right_led_1_off();
178 _delay_ms(50); 176 wait_ms(50);
179 ergodox_right_led_2_off(); 177 ergodox_right_led_2_off();
180 _delay_ms(50); 178 wait_ms(50);
181 ergodox_right_led_3_off(); 179 ergodox_right_led_3_off();
182} 180}
183 181
184//All tap dances now put together. Example 3 is "CT_FLASH" 182// All tap dances now put together. Example 3 is "CT_FLASH"
185qk_tap_dance_action_t tap_dance_actions[] = { 183qk_tap_dance_action_t tap_dance_actions[] = {
186 [CT_SE] = ACTION_TAP_DANCE_DOUBLE (KC_SPC, KC_ENT) 184 [CT_SE] = ACTION_TAP_DANCE_DOUBLE(KC_SPC, KC_ENT),
187 ,[CT_CLN] = ACTION_TAP_DANCE_FN_ADVANCED (NULL, dance_cln_finished, dance_cln_reset) 185 [CT_CLN] = ACTION_TAP_DANCE_FN_ADVANCED(NULL, dance_cln_finished, dance_cln_reset),
188 ,[CT_EGG] = ACTION_TAP_DANCE_FN (dance_egg) 186 [CT_EGG] = ACTION_TAP_DANCE_FN(dance_egg),
189 ,[CT_FLSH] = ACTION_TAP_DANCE_FN_ADVANCED (dance_flsh_each, dance_flsh_finished, dance_flsh_reset) 187 [CT_FLSH] = ACTION_TAP_DANCE_FN_ADVANCED(dance_flsh_each, dance_flsh_finished, dance_flsh_reset)
190}; 188};
191``` 189```
192 190
193### Example 4: 'Quad Function Tap-Dance' 191#### Example 4: 'Quad Function Tap-Dance' :id=example-4
194 192
195By [DanielGGordon](https://github.com/danielggordon) 193By [DanielGGordon](https://github.com/danielggordon)
196 194
@@ -201,40 +199,37 @@ Below is a specific example:
201* Double Tap = Send `Escape` 199* Double Tap = Send `Escape`
202* Double Tap and Hold = Send `Alt` 200* Double Tap and Hold = Send `Alt`
203 201
204## Setup
205
206You will need a few things that can be used for 'Quad Function Tap-Dance'. 202You will need a few things that can be used for 'Quad Function Tap-Dance'.
207 203
208You'll need to add these to the top of your `keymap.c` file, before your keymap. 204You'll need to add these to the top of your `keymap.c` file, before your keymap.
209 205
210```c 206```c
211typedef struct { 207typedef struct {
212 bool is_press_action; 208 bool is_press_action;
213 int state; 209 uint8_t state;
214} tap; 210} tap;
215 211
216enum { 212enum {
217 SINGLE_TAP = 1, 213 SINGLE_TAP = 1,
218 SINGLE_HOLD = 2, 214 SINGLE_HOLD,
219 DOUBLE_TAP = 3, 215 DOUBLE_TAP,
220 DOUBLE_HOLD = 4, 216 DOUBLE_HOLD,
221 DOUBLE_SINGLE_TAP = 5, //send two single taps 217 DOUBLE_SINGLE_TAP, // Send two single taps
222 TRIPLE_TAP = 6, 218 TRIPLE_TAP,
223 TRIPLE_HOLD = 7 219 TRIPLE_HOLD
224}; 220};
225 221
226//Tap dance enums 222// Tap dance enums
227enum { 223enum {
228 X_CTL = 0, 224 X_CTL,
229 SOME_OTHER_DANCE 225 SOME_OTHER_DANCE
230}; 226};
231 227
232int cur_dance (qk_tap_dance_state_t *state); 228uint8_t cur_dance(qk_tap_dance_state_t *state);
233
234//for the x tap dance. Put it here so it can be used in any keymap
235void x_finished (qk_tap_dance_state_t *state, void *user_data);
236void x_reset (qk_tap_dance_state_t *state, void *user_data);
237 229
230// For the x tap dance. Put it here so it can be used in any keymap
231void x_finished(qk_tap_dance_state_t *state, void *user_data);
232void x_reset(qk_tap_dance_state_t *state, void *user_data);
238``` 233```
239 234
240Now, at the bottom of your `keymap.c` file, you'll need to add the following: 235Now, at the bottom of your `keymap.c` file, you'll need to add the following:
@@ -267,65 +262,62 @@ Now, at the bottom of your `keymap.c` file, you'll need to add the following:
267 * For the third point, there does exist the 'DOUBLE_SINGLE_TAP', however this is not fully tested 262 * For the third point, there does exist the 'DOUBLE_SINGLE_TAP', however this is not fully tested
268 * 263 *
269 */ 264 */
270int cur_dance (qk_tap_dance_state_t *state) { 265uint8_t cur_dance(qk_tap_dance_state_t *state) {
271 if (state->count == 1) { 266 if (state->count == 1) {
272 if (state->interrupted || !state->pressed) return SINGLE_TAP; 267 if (state->interrupted || !state->pressed) return SINGLE_TAP;
273 //key has not been interrupted, but they key is still held. Means you want to send a 'HOLD'. 268 // Key has not been interrupted, but the key is still held. Means you want to send a 'HOLD'.
274 else return SINGLE_HOLD; 269 else return SINGLE_HOLD;
275 } 270 } else if (state->count == 2) {
276 else if (state->count == 2) { 271 // DOUBLE_SINGLE_TAP is to distinguish between typing "pepper", and actually wanting a double tap
277 /* 272 // action when hitting 'pp'. Suggested use case for this return value is when you want to send two
278 * DOUBLE_SINGLE_TAP is to distinguish between typing "pepper", and actually wanting a double tap 273 // keystrokes of the key, and not the 'double tap' action/macro.
279 * action when hitting 'pp'. Suggested use case for this return value is when you want to send two 274 if (state->interrupted) return DOUBLE_SINGLE_TAP;
280 * keystrokes of the key, and not the 'double tap' action/macro. 275 else if (state->pressed) return DOUBLE_HOLD;
281 */ 276 else return DOUBLE_TAP;
282 if (state->interrupted) return DOUBLE_SINGLE_TAP; 277 }
283 else if (state->pressed) return DOUBLE_HOLD; 278
284 else return DOUBLE_TAP; 279 // Assumes no one is trying to type the same letter three times (at least not quickly).
285 } 280 // If your tap dance key is 'KC_W', and you want to type "www." quickly - then you will need to add
286 //Assumes no one is trying to type the same letter three times (at least not quickly). 281 // an exception here to return a 'TRIPLE_SINGLE_TAP', and define that enum just like 'DOUBLE_SINGLE_TAP'
287 //If your tap dance key is 'KC_W', and you want to type "www." quickly - then you will need to add 282 if (state->count == 3) {
288 //an exception here to return a 'TRIPLE_SINGLE_TAP', and define that enum just like 'DOUBLE_SINGLE_TAP' 283 if (state->interrupted || !state->pressed) return TRIPLE_TAP;
289 if (state->count == 3) { 284 else return TRIPLE_HOLD;
290 if (state->interrupted || !state->pressed) return TRIPLE_TAP; 285 } else return 8; // Magic number. At some point this method will expand to work for more presses
291 else return TRIPLE_HOLD;
292 }
293 else return 8; //magic number. At some point this method will expand to work for more presses
294} 286}
295 287
296//instanalize an instance of 'tap' for the 'x' tap dance. 288// Create an instance of 'tap' for the 'x' tap dance.
297static tap xtap_state = { 289static tap xtap_state = {
298 .is_press_action = true, 290 .is_press_action = true,
299 .state = 0 291 .state = 0
300}; 292};
301 293
302void x_finished (qk_tap_dance_state_t *state, void *user_data) { 294void x_finished(qk_tap_dance_state_t *state, void *user_data) {
303 xtap_state.state = cur_dance(state); 295 xtap_state.state = cur_dance(state);
304 switch (xtap_state.state) { 296 switch (xtap_state.state) {
305 case SINGLE_TAP: register_code(KC_X); break; 297 case SINGLE_TAP: register_code(KC_X); break;
306 case SINGLE_HOLD: register_code(KC_LCTRL); break; 298 case SINGLE_HOLD: register_code(KC_LCTRL); break;
307 case DOUBLE_TAP: register_code(KC_ESC); break; 299 case DOUBLE_TAP: register_code(KC_ESC); break;
308 case DOUBLE_HOLD: register_code(KC_LALT); break; 300 case DOUBLE_HOLD: register_code(KC_LALT); break;
309 case DOUBLE_SINGLE_TAP: register_code(KC_X); unregister_code(KC_X); register_code(KC_X); 301 // Last case is for fast typing. Assuming your key is `f`:
310 //Last case is for fast typing. Assuming your key is `f`: 302 // For example, when typing the word `buffer`, and you want to make sure that you send `ff` and not `Esc`.
311 //For example, when typing the word `buffer`, and you want to make sure that you send `ff` and not `Esc`. 303 // In order to type `ff` when typing fast, the next character will have to be hit within the `TAPPING_TERM`, which by default is 200ms.
312 //In order to type `ff` when typing fast, the next character will have to be hit within the `TAPPING_TERM`, which by default is 200ms. 304 case DOUBLE_SINGLE_TAP: tap_code(KC_X); register_code(KC_X);
313 } 305 }
314} 306}
315 307
316void x_reset (qk_tap_dance_state_t *state, void *user_data) { 308void x_reset(qk_tap_dance_state_t *state, void *user_data) {
317 switch (xtap_state.state) { 309 switch (xtap_state.state) {
318 case SINGLE_TAP: unregister_code(KC_X); break; 310 case SINGLE_TAP: unregister_code(KC_X); break;
319 case SINGLE_HOLD: unregister_code(KC_LCTRL); break; 311 case SINGLE_HOLD: unregister_code(KC_LCTRL); break;
320 case DOUBLE_TAP: unregister_code(KC_ESC); break; 312 case DOUBLE_TAP: unregister_code(KC_ESC); break;
321 case DOUBLE_HOLD: unregister_code(KC_LALT); 313 case DOUBLE_HOLD: unregister_code(KC_LALT);
322 case DOUBLE_SINGLE_TAP: unregister_code(KC_X); 314 case DOUBLE_SINGLE_TAP: unregister_code(KC_X);
323 } 315 }
324 xtap_state.state = 0; 316 xtap_state.state = 0;
325} 317}
326 318
327qk_tap_dance_action_t tap_dance_actions[] = { 319qk_tap_dance_action_t tap_dance_actions[] = {
328 [X_CTL] = ACTION_TAP_DANCE_FN_ADVANCED(NULL,x_finished, x_reset) 320 [X_CTL] = ACTION_TAP_DANCE_FN_ADVANCED(NULL, x_finished, x_reset)
329}; 321};
330``` 322```
331 323
@@ -335,90 +327,91 @@ If you want to implement this in your userspace, then you may want to check out
335 327
336> In this configuration "hold" takes place **after** tap dance timeout (see `ACTION_TAP_DANCE_FN_ADVANCED_TIME`). To achieve instant hold, remove `state->interrupted` checks in conditions. As a result you may use comfortable longer tapping periods to have more time for taps and not to wait too long for holds (try starting with doubled `TAPPING_TERM`). 328> In this configuration "hold" takes place **after** tap dance timeout (see `ACTION_TAP_DANCE_FN_ADVANCED_TIME`). To achieve instant hold, remove `state->interrupted` checks in conditions. As a result you may use comfortable longer tapping periods to have more time for taps and not to wait too long for holds (try starting with doubled `TAPPING_TERM`).
337 329
338### Example 5: Using tap dance for advanced mod-tap and layer-tap keys :id=example-5-using-tap-dance-for-advanced-mod-tap-and-layer-tap-keys 330#### Example 5: Using tap dance for advanced mod-tap and layer-tap keys :id=example-5
339 331
340Tap dance can be used to emulate `MT()` and `LT()` behavior when the tapped code is not a basic keycode. This is useful to send tapped keycodes that normally require `Shift`, such as parentheses or curly braces—or other modified keycodes, such as `Control + X`. 332Tap dance can be used to emulate `MT()` and `LT()` behavior when the tapped code is not a basic keycode. This is useful to send tapped keycodes that normally require `Shift`, such as parentheses or curly braces—or other modified keycodes, such as `Control + X`.
341 333
342Below your layers and custom keycodes, add the following: 334Below your layers and custom keycodes, add the following:
343 335
344```c 336```c
345// tapdance keycodes 337// Tap Dance keycodes
346enum td_keycodes { 338enum td_keycodes {
347 ALT_LP // Our example key: `LALT` when held, `(` when tapped. Add additional keycodes for each tapdance. 339 ALT_LP // Our example key: `LALT` when held, `(` when tapped. Add additional keycodes for each tapdance.
348}; 340};
349 341
350// define a type containing as many tapdance states as you need 342// Define a type containing as many tapdance states as you need
351typedef enum { 343typedef enum {
352 SINGLE_TAP, 344 SINGLE_TAP,
353 SINGLE_HOLD, 345 SINGLE_HOLD,
354 DOUBLE_SINGLE_TAP 346 DOUBLE_SINGLE_TAP
355} td_state_t; 347} td_state_t;
356 348
357// create a global instance of the tapdance state type 349// Create a global instance of the tapdance state type
358static td_state_t td_state; 350static td_state_t td_state;
359 351
360// declare your tapdance functions: 352// Declare your tapdance functions:
361 353
362// function to determine the current tapdance state 354// Function to determine the current tapdance state
363int cur_dance (qk_tap_dance_state_t *state); 355uint8_t cur_dance(qk_tap_dance_state_t *state);
364 356
365// `finished` and `reset` functions for each tapdance keycode 357// `finished` and `reset` functions for each tapdance keycode
366void altlp_finished (qk_tap_dance_state_t *state, void *user_data); 358void altlp_finished(qk_tap_dance_state_t *state, void *user_data);
367void altlp_reset (qk_tap_dance_state_t *state, void *user_data); 359void altlp_reset(qk_tap_dance_state_t *state, void *user_data);
368``` 360```
369 361
370Below your `LAYOUT`, define each of the tapdance functions: 362Below your `LAYOUT`, define each of the tapdance functions:
371 363
372```c 364```c
373// determine the tapdance state to return 365// Determine the tapdance state to return
374int cur_dance (qk_tap_dance_state_t *state) { 366uint8_t cur_dance(qk_tap_dance_state_t *state) {
375 if (state->count == 1) { 367 if (state->count == 1) {
376 if (state->interrupted || !state->pressed) { return SINGLE_TAP; } 368 if (state->interrupted || !state->pressed) return SINGLE_TAP;
377 else { return SINGLE_HOLD; } 369 else return SINGLE_HOLD;
378 } 370 }
379 if (state->count == 2) { return DOUBLE_SINGLE_TAP; } 371
380 else { return 3; } // any number higher than the maximum state value you return above 372 if (state->count == 2) return DOUBLE_SINGLE_TAP;
373 else return 3; // Any number higher than the maximum state value you return above
381} 374}
382 375
383// handle the possible states for each tapdance keycode you define: 376// Handle the possible states for each tapdance keycode you define:
384 377
385void altlp_finished (qk_tap_dance_state_t *state, void *user_data) { 378void altlp_finished(qk_tap_dance_state_t *state, void *user_data) {
386 td_state = cur_dance(state); 379 td_state = cur_dance(state);
387 switch (td_state) { 380 switch (td_state) {
388 case SINGLE_TAP: 381 case SINGLE_TAP:
389 register_code16(KC_LPRN); 382 register_code16(KC_LPRN);
390 break; 383 break;
391 case SINGLE_HOLD: 384 case SINGLE_HOLD:
392 register_mods(MOD_BIT(KC_LALT)); // for a layer-tap key, use `layer_on(_MY_LAYER)` here 385 register_mods(MOD_BIT(KC_LALT)); // For a layer-tap key, use `layer_on(_MY_LAYER)` here
393 break; 386 break;
394 case DOUBLE_SINGLE_TAP: // allow nesting of 2 parens `((` within tapping term 387 case DOUBLE_SINGLE_TAP: // Allow nesting of 2 parens `((` within tapping term
395 tap_code16(KC_LPRN); 388 tap_code16(KC_LPRN);
396 register_code16(KC_LPRN); 389 register_code16(KC_LPRN);
397 } 390 }
398} 391}
399 392
400void altlp_reset (qk_tap_dance_state_t *state, void *user_data) { 393void altlp_reset(qk_tap_dance_state_t *state, void *user_data) {
401 switch (td_state) { 394 switch (td_state) {
402 case SINGLE_TAP: 395 case SINGLE_TAP:
403 unregister_code16(KC_LPRN); 396 unregister_code16(KC_LPRN);
404 break; 397 break;
405 case SINGLE_HOLD: 398 case SINGLE_HOLD:
406 unregister_mods(MOD_BIT(KC_LALT)); // for a layer-tap key, use `layer_off(_MY_LAYER)` here 399 unregister_mods(MOD_BIT(KC_LALT)); // For a layer-tap key, use `layer_off(_MY_LAYER)` here
407 break; 400 break;
408 case DOUBLE_SINGLE_TAP: 401 case DOUBLE_SINGLE_TAP:
409 unregister_code16(KC_LPRN); 402 unregister_code16(KC_LPRN);
410 } 403 }
411} 404}
412 405
413// define `ACTION_TAP_DANCE_FN_ADVANCED()` for each tapdance keycode, passing in `finished` and `reset` functions 406// Define `ACTION_TAP_DANCE_FN_ADVANCED()` for each tapdance keycode, passing in `finished` and `reset` functions
414qk_tap_dance_action_t tap_dance_actions[] = { 407qk_tap_dance_action_t tap_dance_actions[] = {
415 [ALT_LP] = ACTION_TAP_DANCE_FN_ADVANCED(NULL, altlp_finished, altlp_reset) 408 [ALT_LP] = ACTION_TAP_DANCE_FN_ADVANCED(NULL, altlp_finished, altlp_reset)
416}; 409};
417``` 410```
418 411
419Wrap each tapdance keycode in `TD()` when including it in your keymap, e.g. `TD(ALT_LP)`. 412Wrap each tapdance keycode in `TD()` when including it in your keymap, e.g. `TD(ALT_LP)`.
420 413
421### Example 6: Using tap dance for momentary-layer-switch and layer-toggle keys 414#### Example 6: Using tap dance for momentary-layer-switch and layer-toggle keys :id=example-6
422 415
423Tap Dance can be used to mimic MO(layer) and TG(layer) functionality. For this example, we will set up a key to function as `KC_QUOT` on single-tap, as `MO(_MY_LAYER)` on single-hold, and `TG(_MY_LAYER)` on double-tap. 416Tap Dance can be used to mimic MO(layer) and TG(layer) functionality. For this example, we will set up a key to function as `KC_QUOT` on single-tap, as `MO(_MY_LAYER)` on single-hold, and `TG(_MY_LAYER)` on double-tap.
424 417
@@ -426,97 +419,92 @@ The first step is to include the following code towards the beginning of your `k
426 419
427```c 420```c
428typedef struct { 421typedef struct {
429 bool is_press_action; 422 bool is_press_action;
430 int state; 423 uint8_t state;
431} tap; 424} tap;
432 425
433//Define a type for as many tap dance states as you need 426// Define a type for as many tap dance states as you need
434enum { 427enum {
435 SINGLE_TAP = 1, 428 SINGLE_TAP = 1,
436 SINGLE_HOLD = 2, 429 SINGLE_HOLD,
437 DOUBLE_TAP = 3 430 DOUBLE_TAP
438}; 431};
439 432
440enum { 433enum {
441 QUOT_LAYR = 0 //Our custom tap dance key; add any other tap dance keys to this enum 434 QUOT_LAYR, // Our custom tap dance key; add any other tap dance keys to this enum
442}; 435};
443 436
444//Declare the functions to be used with your tap dance key(s) 437// Declare the functions to be used with your tap dance key(s)
445 438
446//Function associated with all tap dances 439// Function associated with all tap dances
447int cur_dance (qk_tap_dance_state_t *state); 440uint8_t cur_dance(qk_tap_dance_state_t *state);
448 441
449//Functions associated with individual tap dances 442// Functions associated with individual tap dances
450void ql_finished (qk_tap_dance_state_t *state, void *user_data); 443void ql_finished(qk_tap_dance_state_t *state, void *user_data);
451void ql_reset (qk_tap_dance_state_t *state, void *user_data); 444void ql_reset(qk_tap_dance_state_t *state, void *user_data);
452``` 445```
453 446
454Towards the bottom of your `keymap.c`, include the following code: 447Towards the bottom of your `keymap.c`, include the following code:
455 448
456```c 449```c
457//Determine the current tap dance state 450// Determine the current tap dance state
458int cur_dance (qk_tap_dance_state_t *state) { 451uint8_t cur_dance(qk_tap_dance_state_t *state) {
459 if (state->count == 1) { 452 if (state->count == 1) {
460 if (!state->pressed) { 453 if (!state->pressed) return SINGLE_TAP;
461 return SINGLE_TAP; 454 else return SINGLE_HOLD;
462 } else { 455 } else if (state->count == 2) return DOUBLE_TAP;
463 return SINGLE_HOLD; 456 else return 8;
464 }
465 } else if (state->count == 2) {
466 return DOUBLE_TAP;
467 }
468 else return 8;
469} 457}
470 458
471//Initialize tap structure associated with example tap dance key 459// Initialize tap structure associated with example tap dance key
472static tap ql_tap_state = { 460static tap ql_tap_state = {
473 .is_press_action = true, 461 .is_press_action = true,
474 .state = 0 462 .state = 0
475}; 463};
476 464
477//Functions that control what our tap dance key does 465// Functions that control what our tap dance key does
478void ql_finished (qk_tap_dance_state_t *state, void *user_data) { 466void ql_finished(qk_tap_dance_state_t *state, void *user_data) {
479 ql_tap_state.state = cur_dance(state); 467 ql_tap_state.state = cur_dance(state);
480 switch (ql_tap_state.state) { 468 switch (ql_tap_state.state) {
481 case SINGLE_TAP: 469 case SINGLE_TAP:
482 tap_code(KC_QUOT); 470 tap_code(KC_QUOT);
483 break; 471 break;
484 case SINGLE_HOLD: 472 case SINGLE_HOLD:
485 layer_on(_MY_LAYER); 473 layer_on(_MY_LAYER);
486 break; 474 break;
487 case DOUBLE_TAP: 475 case DOUBLE_TAP:
488 //check to see if the layer is already set 476 // Check to see if the layer is already set
489 if (layer_state_is(_MY_LAYER)) { 477 if (layer_state_is(_MY_LAYER)) {
490 //if already set, then switch it off 478 // If already set, then switch it off
491 layer_off(_MY_LAYER); 479 layer_off(_MY_LAYER);
492 } else { 480 } else {
493 //if not already set, then switch the layer on 481 // If not already set, then switch the layer on
494 layer_on(_MY_LAYER); 482 layer_on(_MY_LAYER);
495 } 483 }
496 break; 484 break;
497 } 485 }
498} 486}
499 487
500void ql_reset (qk_tap_dance_state_t *state, void *user_data) { 488void ql_reset(qk_tap_dance_state_t *state, void *user_data) {
501 //if the key was held down and now is released then switch off the layer 489 // If the key was held down and now is released then switch off the layer
502 if (ql_tap_state.state==SINGLE_HOLD) { 490 if (ql_tap_state.state == SINGLE_HOLD) {
503 layer_off(_MY_LAYER); 491 layer_off(_MY_LAYER);
504 } 492 }
505 ql_tap_state.state = 0; 493 ql_tap_state.state = 0;
506} 494}
507 495
508//Associate our tap dance key with its functionality 496// Associate our tap dance key with its functionality
509qk_tap_dance_action_t tap_dance_actions[] = { 497qk_tap_dance_action_t tap_dance_actions[] = {
510 [QUOT_LAYR] = ACTION_TAP_DANCE_FN_ADVANCED_TIME(NULL, ql_finished, ql_reset, 275) 498 [QUOT_LAYR] = ACTION_TAP_DANCE_FN_ADVANCED_TIME(NULL, ql_finished, ql_reset, 275)
511}; 499};
512``` 500```
513 501
514The above code is similar to that used in previous examples. The one point to note is that we need to be able to check which layers are active at any time so we can toggle them if needed. To do this we use the `layer_state_is( layer )` function which returns `true` if the given `layer` is active. 502The above code is similar to that used in previous examples. The one point to note is that we need to be able to check which layers are active at any time so we can toggle them if needed. To do this we use the `layer_state_is(layer)` function which returns `true` if the given `layer` is active.
515 503
516The use of `cur_dance()` and `ql_tap_state` mirrors the above examples. 504The use of `cur_dance()` and `ql_tap_state` mirrors the above examples.
517 505
518The `case:SINGLE_TAP` in `ql_finished` is similar to the above examples. The `case:SINGLE_HOLD` works in conjunction with `ql_reset()` to switch to `_MY_LAYER` while the tap dance key is held, and to switch away from `_MY_LAYER` when the key is released. This mirrors the use of `MO(_MY_LAYER)`. The `case:DOUBLE_TAP` works by checking whether `_MY_LAYER` is the active layer, and toggling it on or off accordingly. This mirrors the use of `TG(_MY_LAYER)`. 506The `case:SINGLE_TAP` in `ql_finished` is similar to the above examples. The `SINGLE_HOLD` case works in conjunction with `ql_reset()` to switch to `_MY_LAYER` while the tap dance key is held, and to switch away from `_MY_LAYER` when the key is released. This mirrors the use of `MO(_MY_LAYER)`. The `DOUBLE_TAP` case works by checking whether `_MY_LAYER` is the active layer, and toggling it on or off accordingly. This mirrors the use of `TG(_MY_LAYER)`.
519 507
520`tap_dance_actions[]` works similar to the above examples. Note that I used `ACTION_TAP_DANCE_FN_ADVANCED_TIME()` instead of `ACTION_TAP_DANCE_FN_ADVANCED()`. This is because I like my `TAPPING_TERM` to be short (~175ms) for my non-tap-dance keys but find that this is too quick for me to reliably complete tap dance actions - thus the increased time of 275ms here. 508`tap_dance_actions[]` works similar to the above examples. Note that I used `ACTION_TAP_DANCE_FN_ADVANCED_TIME()` instead of `ACTION_TAP_DANCE_FN_ADVANCED()`. This is because I like my `TAPPING_TERM` to be short (\~175ms) for my non-tap-dance keys but find that this is too quick for me to reliably complete tap dance actions - thus the increased time of 275ms here.
521 509
522Finally, to get this tap dance key working, be sure to include `TD(QUOT_LAYR)` in your `keymaps[]`. 510Finally, to get this tap dance key working, be sure to include `TD(QUOT_LAYR)` in your `keymaps[]`.