keyboard stuff
1# Tap Dance: A Single Key Can Do 3, 5, or 100 Different Things
2
3## Introduction {#introduction}
4
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:
6
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.
8
9## How to Use Tap Dance {#how-to-use}
10
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.
12
13Optionally, you might want to set a custom `TAPPING_TERM` time by adding something like this in your `config.h` file:
14
15```c
16#define TAPPING_TERM 175
17#define TAPPING_TERM_PER_KEY
18```
19
20The `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. The `TAPPING_TERM_PER_KEY` definition is only needed if you control the tapping term through a [custom `get_tapping_term` function](../tap_hold#tapping_term), which may be needed because `TAPPING_TERM` affects not just tap-dance keys.
21
22Next, you will want to define some tap-dance keys, which is easiest to do with the `TD()` macro. That macro takes a number which will later be used as an index into the `tap_dance_actions` array and turns it into a tap-dance keycode.
23
24After 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:
25
26* `ACTION_TAP_DANCE_DOUBLE(kc1, kc2)`: Sends the `kc1` keycode when tapped once, `kc2` otherwise. When the key is held, the appropriate keycode is registered: `kc1` when pressed and held, `kc2` when tapped once, then pressed and held.
27* `ACTION_TAP_DANCE_LAYER_MOVE(kc, layer)`: Sends the `kc` keycode when tapped once, or moves to `layer`. (this functions like the `TO` layer keycode).
28* `ACTION_TAP_DANCE_LAYER_TOGGLE(kc, layer)`: Sends the `kc` keycode when tapped once, or toggles the state of `layer`. (this functions like the `TG` layer keycode).
29* `ACTION_TAP_DANCE_FN(fn)`: Calls the specified function - defined in the user keymap - with the final tap count of the tap dance action.
30* `ACTION_TAP_DANCE_FN_ADVANCED(on_each_tap_fn, on_dance_finished_fn, on_dance_reset_fn)`: Calls the first specified function - defined in the user keymap - on every tap, the second function when the dance action finishes (like the previous option), and the last function when the tap dance action resets.
31* `ACTION_TAP_DANCE_FN_ADVANCED_WITH_RELEASE(on_each_tap_fn, on_each_release_fn, on_dance_finished_fn, on_dance_reset_fn)`: This macro is identical to `ACTION_TAP_DANCE_FN_ADVANCED` with the addition of `on_each_release_fn` which is invoked every time the key for the tap dance is released. It is worth noting that `on_each_release_fn` will still be called even when the key is released after the dance finishes (e.g. if the key is released after being pressed and held for longer than the `TAPPING_TERM`).
32
33The first option is enough for a lot of cases, that just want dual roles. For example, `ACTION_TAP_DANCE_DOUBLE(KC_SPC, KC_ENT)` will result in `Space` being sent on single-tap, `Enter` otherwise.
34
35::: warning
36Keep in mind that only [basic keycodes](../keycodes_basic) are supported here. Custom keycodes are not supported.
37:::
38
39Similar to the first option, the second and third option are good for simple layer-switching cases.
40
41For more complicated cases, like blink the LEDs, fiddle with the backlighting, and so on, use the fourth or fifth option. Examples of each are listed below.
42
43::: tip
44If too many tap dances are active at the same time, later ones won't have any effect. You need to increase `TAP_DANCE_MAX_SIMULTANEOUS` by adding `#define TAP_DANCE_MAX_SIMULTANEOUS 5` (or higher) to your keymap's `config.h` file if you expect that users may hold down many tap dance keys simultaneously. By default, only 3 tap dance keys can be used together at the same time.
45:::
46
47## Implementation Details {#implementation}
48
49Well, 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!
50
51Let's go over the three functions mentioned in `ACTION_TAP_DANCE_FN_ADVANCED` in a little more detail. They all receive the same two arguments: a pointer to a structure that holds all dance related state information, and a pointer to a use case specific state variable. The three functions differ in when they are called. The first, `on_each_tap_fn()`, is called every time the tap dance key is *pressed*. Before it is called, the counter is incremented and the timer is reset. The second function, `on_dance_finished_fn()`, is called when the tap dance is interrupted or ends because `TAPPING_TERM` milliseconds have passed since the last tap. When the `finished` field of the dance state structure is set to `true`, the `on_dance_finished_fn()` is skipped. After `on_dance_finished_fn()` was called or would have been called, but no sooner than when the tap dance key is *released*, `on_dance_reset_fn()` is called. It is possible to end a tap dance immediately, skipping `on_dance_finished_fn()`, but not `on_dance_reset_fn`, by calling `reset_tap_dance(state)`.
52
53To accomplish this logic, the tap dance mechanics use three entry points. The main entry point is `process_tap_dance()`, called from `process_record_quantum()` *after* `process_record_kb()` and `process_record_user()`. This function is responsible for calling `on_each_tap_fn()` and `on_dance_reset_fn()`. In order to handle interruptions of a tap dance, another entry point, `preprocess_tap_dance()` is run right at the beginning of `process_record_quantum()`. 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. Finally, `tap_dance_task()` periodically checks whether `TAPPING_TERM` has passed since the last key press and finishes a tap dance if that is the case.
54
55This means that you have `TAPPING_TERM` time to tap the key again; you do not have to input all the taps within a single `TAPPING_TERM` timeframe. This allows for longer tap counts, with minimal impact on responsiveness.
56
57## Examples {#examples}
58
59### Simple Example: Send `ESC` on Single Tap, `CAPS_LOCK` on Double Tap {#simple-example}
60
61Here's a simple example for a single definition:
62
631. In your `rules.mk`, add `TAP_DANCE_ENABLE = yes`
642. In your `keymap.c` file, define the variables and definitions, then add to your keymap:
65
66```c
67// Tap Dance declarations
68enum {
69 TD_ESC_CAPS,
70};
71
72// Tap Dance definitions
73tap_dance_action_t tap_dance_actions[] = {
74 // Tap once for Escape, twice for Caps Lock
75 [TD_ESC_CAPS] = ACTION_TAP_DANCE_DOUBLE(KC_ESC, KC_CAPS),
76};
77
78// Add tap dance item to your keymap in place of a keycode
79const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
80 // ...
81 TD(TD_ESC_CAPS)
82 // ...
83};
84```
85
86### Complex Examples {#complex-examples}
87
88This section details several complex tap dance examples.
89All the enums used in the examples are declared like this:
90
91```c
92// Enums defined for all examples:
93enum {
94 TD_ESC_CAPS,
95 CT_EGG,
96 CT_FLSH,
97 CT_CLN,
98 X_CTL,
99};
100```
101
102#### Example 1: Send "Safety Dance!" After 100 Taps {#example-1}
103
104```c
105void dance_egg(tap_dance_state_t *state, void *user_data) {
106 if (state->count >= 100) {
107 SEND_STRING("Safety dance!");
108 reset_tap_dance(state);
109 }
110}
111
112tap_dance_action_t tap_dance_actions[] = {
113 [CT_EGG] = ACTION_TAP_DANCE_FN(dance_egg),
114};
115```
116
117#### Example 2: Turn LED Lights On Then Off, One at a Time {#example-2}
118
119```c
120// On each tap, light up one LED, from right to left
121// On the fourth tap, turn them off from right to left
122void dance_flsh_each(tap_dance_state_t *state, void *user_data) {
123 switch (state->count) {
124 case 1:
125 ergodox_right_led_3_on();
126 break;
127 case 2:
128 ergodox_right_led_2_on();
129 break;
130 case 3:
131 ergodox_right_led_1_on();
132 break;
133 case 4:
134 ergodox_right_led_3_off();
135 wait_ms(50);
136 ergodox_right_led_2_off();
137 wait_ms(50);
138 ergodox_right_led_1_off();
139 }
140}
141
142// On the fourth tap, set the keyboard on flash state
143void dance_flsh_finished(tap_dance_state_t *state, void *user_data) {
144 if (state->count >= 4) {
145 reset_keyboard();
146 }
147}
148
149// If the flash state didn't happen, then turn off LEDs, left to right
150void dance_flsh_reset(tap_dance_state_t *state, void *user_data) {
151 ergodox_right_led_1_off();
152 wait_ms(50);
153 ergodox_right_led_2_off();
154 wait_ms(50);
155 ergodox_right_led_3_off();
156}
157
158// All tap dances now put together. Example 2 is "CT_FLSH"
159tap_dance_action_t tap_dance_actions[] = {
160 [TD_ESC_CAPS] = ACTION_TAP_DANCE_DOUBLE(KC_ESC, KC_CAPS),
161 [CT_EGG] = ACTION_TAP_DANCE_FN(dance_egg),
162 [CT_FLSH] = ACTION_TAP_DANCE_FN_ADVANCED(dance_flsh_each, dance_flsh_finished, dance_flsh_reset)
163};
164```
165
166#### Example 3: Send `:` on Tap, `;` on Hold {#example-3}
167
168With a little effort, powerful tap-hold configurations can be implemented as tap dances. To emit taps as early as possible, we need to act on releases of the tap dance key. There is no callback for this in the tap dance framework, so we use `process_record_user()`.
169
170```c
171typedef struct {
172 uint16_t tap;
173 uint16_t hold;
174 uint16_t held;
175} tap_dance_tap_hold_t;
176
177void tap_dance_tap_hold_finished(tap_dance_state_t *state, void *user_data) {
178 tap_dance_tap_hold_t *tap_hold = (tap_dance_tap_hold_t *)user_data;
179
180 if (state->pressed) {
181 if (state->count == 1
182#ifndef PERMISSIVE_HOLD
183 && !state->interrupted
184#endif
185 ) {
186 register_code16(tap_hold->hold);
187 tap_hold->held = tap_hold->hold;
188 } else {
189 register_code16(tap_hold->tap);
190 tap_hold->held = tap_hold->tap;
191 }
192 }
193}
194
195void tap_dance_tap_hold_reset(tap_dance_state_t *state, void *user_data) {
196 tap_dance_tap_hold_t *tap_hold = (tap_dance_tap_hold_t *)user_data;
197
198 if (tap_hold->held) {
199 unregister_code16(tap_hold->held);
200 tap_hold->held = 0;
201 }
202}
203
204#define ACTION_TAP_DANCE_TAP_HOLD(tap, hold) \
205 { \
206 .fn = {NULL, tap_dance_tap_hold_finished, tap_dance_tap_hold_reset}, \
207 .user_data = (void *)&((tap_dance_tap_hold_t){tap, hold, 0}), \
208 }
209
210tap_dance_action_t tap_dance_actions[] = {
211 [CT_CLN] = ACTION_TAP_DANCE_TAP_HOLD(KC_COLN, KC_SCLN),
212};
213
214bool process_record_user(uint16_t keycode, keyrecord_t *record) {
215 tap_dance_action_t *action;
216 tap_dance_state_t* state;
217
218 switch (keycode) {
219 case TD(CT_CLN):
220 action = tap_dance_get(QK_TAP_DANCE_GET_INDEX(keycode));
221 state = tap_dance_get_state(QK_TAP_DANCE_GET_INDEX(keycode));
222 if (!record->event.pressed && state != NULL && state->count && !state->finished) {
223 tap_dance_tap_hold_t *tap_hold = (tap_dance_tap_hold_t *)action->user_data;
224 tap_code16(tap_hold->tap);
225 }
226 }
227 return true;
228}
229```
230
231#### Example 4: 'Quad Function Tap-Dance' {#example-4}
232
233By [DanielGGordon](https://github.com/danielggordon)
234
235Allow one key to have 4 (or more) functions, depending on number of presses, and if the key is held or tapped.
236Below is a specific example:
237* Tap = Send `x`
238* Hold = Send `Control`
239* Double Tap = Send `Escape`
240* Double Tap and Hold = Send `Alt`
241
242You will need a few things that can be used for 'Quad Function Tap-Dance'.
243
244You'll need to add these to the top of your `keymap.c` file, before your keymap.
245
246```c
247typedef enum {
248 TD_NONE,
249 TD_UNKNOWN,
250 TD_SINGLE_TAP,
251 TD_SINGLE_HOLD,
252 TD_DOUBLE_TAP,
253 TD_DOUBLE_HOLD,
254 TD_DOUBLE_SINGLE_TAP, // Send two single taps
255 TD_TRIPLE_TAP,
256 TD_TRIPLE_HOLD
257} td_state_t;
258
259typedef struct {
260 bool is_press_action;
261 td_state_t state;
262} td_tap_t;
263
264// Tap dance enums
265enum {
266 X_CTL,
267 SOME_OTHER_DANCE
268};
269
270td_state_t cur_dance(tap_dance_state_t *state);
271
272// For the x tap dance. Put it here so it can be used in any keymap
273void x_finished(tap_dance_state_t *state, void *user_data);
274void x_reset(tap_dance_state_t *state, void *user_data);
275```
276
277Now, at the bottom of your `keymap.c` file, you'll need to add the following:
278
279```c
280/* Return an integer that corresponds to what kind of tap dance should be executed.
281 *
282 * How to figure out tap dance state: interrupted and pressed.
283 *
284 * Interrupted: If the state of a dance is "interrupted", that means that another key has been hit
285 * under the tapping term. This is typically indicative that you are trying to "tap" the key.
286 *
287 * Pressed: Whether or not the key is still being pressed. If this value is true, that means the tapping term
288 * has ended, but the key is still being pressed down. This generally means the key is being "held".
289 *
290 * One thing that is currently not possible with qmk software in regards to tap dance is to mimic the "permissive hold"
291 * feature. In general, advanced tap dances do not work well if they are used with commonly typed letters.
292 * For example "A". Tap dances are best used on non-letter keys that are not hit while typing letters.
293 *
294 * Good places to put an advanced tap dance:
295 * z,q,x,j,k,v,b, any function key, home/end, comma, semi-colon
296 *
297 * Criteria for "good placement" of a tap dance key:
298 * Not a key that is hit frequently in a sentence
299 * Not a key that is used frequently to double tap, for example 'tab' is often double tapped in a terminal, or
300 * in a web form. So 'tab' would be a poor choice for a tap dance.
301 * Letters used in common words as a double. For example 'p' in 'pepper'. If a tap dance function existed on the
302 * letter 'p', the word 'pepper' would be quite frustrating to type.
303 *
304 * For the third point, there does exist the 'TD_DOUBLE_SINGLE_TAP', however this is not fully tested
305 *
306 */
307td_state_t cur_dance(tap_dance_state_t *state) {
308 if (state->count == 1) {
309 if (state->interrupted || !state->pressed) return TD_SINGLE_TAP;
310 // Key has not been interrupted, but the key is still held. Means you want to send a 'HOLD'.
311 else return TD_SINGLE_HOLD;
312 } else if (state->count == 2) {
313 // TD_DOUBLE_SINGLE_TAP is to distinguish between typing "pepper", and actually wanting a double tap
314 // action when hitting 'pp'. Suggested use case for this return value is when you want to send two
315 // keystrokes of the key, and not the 'double tap' action/macro.
316 if (state->interrupted) return TD_DOUBLE_SINGLE_TAP;
317 else if (state->pressed) return TD_DOUBLE_HOLD;
318 else return TD_DOUBLE_TAP;
319 }
320
321 // Assumes no one is trying to type the same letter three times (at least not quickly).
322 // If your tap dance key is 'KC_W', and you want to type "www." quickly - then you will need to add
323 // an exception here to return a 'TD_TRIPLE_SINGLE_TAP', and define that enum just like 'TD_DOUBLE_SINGLE_TAP'
324 if (state->count == 3) {
325 if (state->interrupted || !state->pressed) return TD_TRIPLE_TAP;
326 else return TD_TRIPLE_HOLD;
327 } else return TD_UNKNOWN;
328}
329
330// Create an instance of 'td_tap_t' for the 'x' tap dance.
331static td_tap_t xtap_state = {
332 .is_press_action = true,
333 .state = TD_NONE
334};
335
336void x_finished(tap_dance_state_t *state, void *user_data) {
337 xtap_state.state = cur_dance(state);
338 switch (xtap_state.state) {
339 case TD_SINGLE_TAP: register_code(KC_X); break;
340 case TD_SINGLE_HOLD: register_code(KC_LCTL); break;
341 case TD_DOUBLE_TAP: register_code(KC_ESC); break;
342 case TD_DOUBLE_HOLD: register_code(KC_LALT); break;
343 // Last case is for fast typing. Assuming your key is `f`:
344 // For example, when typing the word `buffer`, and you want to make sure that you send `ff` and not `Esc`.
345 // 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.
346 case TD_DOUBLE_SINGLE_TAP: tap_code(KC_X); register_code(KC_X); break;
347 default: break;
348 }
349}
350
351void x_reset(tap_dance_state_t *state, void *user_data) {
352 switch (xtap_state.state) {
353 case TD_SINGLE_TAP: unregister_code(KC_X); break;
354 case TD_SINGLE_HOLD: unregister_code(KC_LCTL); break;
355 case TD_DOUBLE_TAP: unregister_code(KC_ESC); break;
356 case TD_DOUBLE_HOLD: unregister_code(KC_LALT); break;
357 case TD_DOUBLE_SINGLE_TAP: unregister_code(KC_X); break;
358 default: break;
359 }
360 xtap_state.state = TD_NONE;
361}
362
363tap_dance_action_t tap_dance_actions[] = {
364 [X_CTL] = ACTION_TAP_DANCE_FN_ADVANCED(NULL, x_finished, x_reset)
365};
366```
367
368And then simply use `TD(X_CTL)` anywhere in your keymap.
369
370::: info
371In this configuration "hold" takes place **after** tap dance timeout. 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`).
372:::
373
374#### Example 5: Using tap dance for advanced mod-tap and layer-tap keys {#example-5}
375
376Tap 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`.
377
378Below your layers and custom keycodes, add the following:
379
380```c
381// Tap Dance keycodes
382enum td_keycodes {
383 ALT_LP // Our example key: `LALT` when held, `(` when tapped. Add additional keycodes for each tapdance.
384};
385
386// Define a type containing as many tapdance states as you need
387typedef enum {
388 TD_NONE,
389 TD_UNKNOWN,
390 TD_SINGLE_TAP,
391 TD_SINGLE_HOLD,
392 TD_DOUBLE_SINGLE_TAP
393} td_state_t;
394
395// Create a global instance of the tapdance state type
396static td_state_t td_state;
397
398// Declare your tapdance functions:
399
400// Function to determine the current tapdance state
401td_state_t cur_dance(tap_dance_state_t *state);
402
403// `finished` and `reset` functions for each tapdance keycode
404void altlp_finished(tap_dance_state_t *state, void *user_data);
405void altlp_reset(tap_dance_state_t *state, void *user_data);
406```
407
408Below your `LAYOUT`, define each of the tapdance functions:
409
410```c
411// Determine the tapdance state to return
412td_state_t cur_dance(tap_dance_state_t *state) {
413 if (state->count == 1) {
414 if (state->interrupted || !state->pressed) return TD_SINGLE_TAP;
415 else return TD_SINGLE_HOLD;
416 }
417
418 if (state->count == 2) return TD_DOUBLE_SINGLE_TAP;
419 else return TD_UNKNOWN; // Any number higher than the maximum state value you return above
420}
421
422// Handle the possible states for each tapdance keycode you define:
423
424void altlp_finished(tap_dance_state_t *state, void *user_data) {
425 td_state = cur_dance(state);
426 switch (td_state) {
427 case TD_SINGLE_TAP:
428 register_code16(KC_LPRN);
429 break;
430 case TD_SINGLE_HOLD:
431 register_mods(MOD_BIT(KC_LALT)); // For a layer-tap key, use `layer_on(_MY_LAYER)` here
432 break;
433 case TD_DOUBLE_SINGLE_TAP: // Allow nesting of 2 parens `((` within tapping term
434 tap_code16(KC_LPRN);
435 register_code16(KC_LPRN);
436 break;
437 default:
438 break;
439 }
440}
441
442void altlp_reset(tap_dance_state_t *state, void *user_data) {
443 switch (td_state) {
444 case TD_SINGLE_TAP:
445 unregister_code16(KC_LPRN);
446 break;
447 case TD_SINGLE_HOLD:
448 unregister_mods(MOD_BIT(KC_LALT)); // For a layer-tap key, use `layer_off(_MY_LAYER)` here
449 break;
450 case TD_DOUBLE_SINGLE_TAP:
451 unregister_code16(KC_LPRN);
452 break;
453 default:
454 break;
455 }
456}
457
458// Define `ACTION_TAP_DANCE_FN_ADVANCED()` for each tapdance keycode, passing in `finished` and `reset` functions
459tap_dance_action_t tap_dance_actions[] = {
460 [ALT_LP] = ACTION_TAP_DANCE_FN_ADVANCED(NULL, altlp_finished, altlp_reset)
461};
462```
463
464Wrap each tapdance keycode in `TD()` when including it in your keymap, e.g. `TD(ALT_LP)`.
465
466#### Example 6: Using tap dance for momentary-layer-switch and layer-toggle keys {#example-6}
467
468Tap 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.
469
470The first step is to include the following code towards the beginning of your `keymap.c`:
471
472```c
473// Define a type for as many tap dance states as you need
474typedef enum {
475 TD_NONE,
476 TD_UNKNOWN,
477 TD_SINGLE_TAP,
478 TD_SINGLE_HOLD,
479 TD_DOUBLE_TAP
480} td_state_t;
481
482typedef struct {
483 bool is_press_action;
484 td_state_t state;
485} td_tap_t;
486
487enum {
488 QUOT_LAYR, // Our custom tap dance key; add any other tap dance keys to this enum
489};
490
491// Declare the functions to be used with your tap dance key(s)
492
493// Function associated with all tap dances
494td_state_t cur_dance(tap_dance_state_t *state);
495
496// Functions associated with individual tap dances
497void ql_finished(tap_dance_state_t *state, void *user_data);
498void ql_reset(tap_dance_state_t *state, void *user_data);
499```
500
501Towards the bottom of your `keymap.c`, include the following code:
502
503```c
504// Determine the current tap dance state
505td_state_t cur_dance(tap_dance_state_t *state) {
506 if (state->count == 1) {
507 if (!state->pressed) return TD_SINGLE_TAP;
508 else return TD_SINGLE_HOLD;
509 } else if (state->count == 2) return TD_DOUBLE_TAP;
510 else return TD_UNKNOWN;
511}
512
513// Initialize tap structure associated with example tap dance key
514static td_tap_t ql_tap_state = {
515 .is_press_action = true,
516 .state = TD_NONE
517};
518
519// Functions that control what our tap dance key does
520void ql_finished(tap_dance_state_t *state, void *user_data) {
521 ql_tap_state.state = cur_dance(state);
522 switch (ql_tap_state.state) {
523 case TD_SINGLE_TAP:
524 tap_code(KC_QUOT);
525 break;
526 case TD_SINGLE_HOLD:
527 layer_on(_MY_LAYER);
528 break;
529 case TD_DOUBLE_TAP:
530 // Check to see if the layer is already set
531 if (layer_state_is(_MY_LAYER)) {
532 // If already set, then switch it off
533 layer_off(_MY_LAYER);
534 } else {
535 // If not already set, then switch the layer on
536 layer_on(_MY_LAYER);
537 }
538 break;
539 default:
540 break;
541 }
542}
543
544void ql_reset(tap_dance_state_t *state, void *user_data) {
545 // If the key was held down and now is released then switch off the layer
546 if (ql_tap_state.state == TD_SINGLE_HOLD) {
547 layer_off(_MY_LAYER);
548 }
549 ql_tap_state.state = TD_NONE;
550}
551
552// Associate our tap dance key with its functionality
553tap_dance_action_t tap_dance_actions[] = {
554 [QUOT_LAYR] = ACTION_TAP_DANCE_FN_ADVANCED(NULL, ql_finished, ql_reset)
555};
556
557// Set a long-ish tapping term for tap-dance keys
558uint16_t get_tapping_term(uint16_t keycode, keyrecord_t *record) {
559 switch (keycode) {
560 case QK_TAP_DANCE ... QK_TAP_DANCE_MAX:
561 return 275;
562 default:
563 return TAPPING_TERM;
564 }
565}
566```
567
568The 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.
569
570The use of `cur_dance()` and `ql_tap_state` mirrors the above examples.
571
572The `case: TD_SINGLE_TAP` in `ql_finished` is similar to the above examples. The `TD_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 `TD_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)`.
573
574`tap_dance_actions[]` works similar to the above examples. Note that, additionally, I set a longer tapping term for the tap dance keys. 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. In order for the per-key tapping terms to take effect, `TAPPING_TERM_PER_KEY` must be defined in your `config.h`.
575
576Finally, to get this tap dance key working, be sure to include `TD(QUOT_LAYR)` in your `keymaps[]`.