Add explicit id tags to externally linked headers
[jackhill/qmk/firmware.git] / docs / feature_tap_dance.md
CommitLineData
7b0356d1 1# Tap Dance: A Single Key Can Do 3, 5, or 100 Different Things
995c3141 2
a747953d 3## Introduction
995c3141
JH
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:
5
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.
7
a747953d 8## Explanatory Comparison with `ACTION_FUNCTION_TAP`
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.
995c3141 14
a747953d 15## How to Use Tap Dance
16But enough of the generalities; lets look at how to actually use Tap Dance!
995c3141 17
a747953d 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.
995c3141 19
a747953d 20Optionally, you might want to set a custom `TAPPING_TERM` time by adding something like this in you `config.h`:
995c3141 21
a747953d 22```
23#define TAPPING_TERM 175
24```
995c3141 25
a747953d 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.
27
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.
29
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:
995c3141
JH
31
32* `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.
f069e9fc
DJ
33* `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).
34 * This is the same as `ACTION_TAP_DANCE_DUAL_ROLE`, but renamed to something that is clearer about its functionality. Both names will work.
35* `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).
995c3141 36* `ACTION_TAP_DANCE_FN(fn)`: Calls the specified function - defined in the user keymap - with the final tap count of the tap dance action.
a87b36d7 37* `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.
38* `ACTION_TAP_DANCE_FN_ADVANCED_TIME(on_each_tap_fn, on_dance_finished_fn, on_dance_reset_fn, tap_specific_tapping_term)`: This functions identically to the `ACTION_TAP_DANCE_FN_ADVANCED` function, but uses a custom tapping term for it, instead of the predefined `TAPPING_TERM`.
995c3141 39
a747953d 40The 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.
995c3141 41
d210ac7a
DJ
42!> Keep in mind that only [basic keycodes](keycodes_basic.md) are supported here. Custom keycodes are not supported.
43
a747953d 44Similar to the first option, the second option is good for simple layer-switching cases.
45
46For more complicated cases, use the third or fourth options (examples of each are listed below).
47
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.
995c3141 49
a747953d 50## Implementation Details
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!
995c3141 52
a747953d 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.
995c3141 54
a747953d 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.
995c3141
JH
56
57Our next stop is `matrix_scan_tap_dance()`. This handles the timeout of tap-dance keys.
58
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.
60
d8e29b53 61# Examples
62
63## Simple Example
995c3141
JH
64
65Here's a simple example for a single definition:
66
03de0c85 671. In your `rules.mk`, add `TAP_DANCE_ENABLE = yes`
995c3141
JH
682. In your `config.h` (which you can copy from `qmk_firmware/keyboards/planck/config.h` to your keymap directory), add `#define TAPPING_TERM 200`
693. In your `keymap.c` file, define the variables and definitions, then add to your keymap:
70
71```c
72//Tap Dance Declarations
73enum {
74 TD_ESC_CAPS = 0
75};
76
77//Tap Dance Definitions
78qk_tap_dance_action_t tap_dance_actions[] = {
79 //Tap once for Esc, twice for Caps Lock
80 [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};
83
84//In Layer declaration, add tap dance item in place of a key code
85TD(TD_ESC_CAPS)
86```
87
383e508b 88## Complex Examples
d8e29b53 89
383e508b
DG
90This section details several complex tap dance examples.
91All the enums used in the examples are declared like this:
995c3141
JH
92
93```c
383e508b 94// Enums defined for all examples:
995c3141
JH
95enum {
96 CT_SE = 0,
97 CT_CLN,
98 CT_EGG,
99 CT_FLSH,
383e508b 100 X_TAP_DANCE
995c3141 101};
383e508b 102```
7b0356d1 103### Example 1: Send `:` on Single Tap, `;` on Double Tap
383e508b 104```c
995c3141
JH
105void dance_cln_finished (qk_tap_dance_state_t *state, void *user_data) {
106 if (state->count == 1) {
107 register_code (KC_RSFT);
108 register_code (KC_SCLN);
109 } else {
110 register_code (KC_SCLN);
111 }
112}
113
114void dance_cln_reset (qk_tap_dance_state_t *state, void *user_data) {
115 if (state->count == 1) {
116 unregister_code (KC_RSFT);
117 unregister_code (KC_SCLN);
118 } else {
119 unregister_code (KC_SCLN);
120 }
121}
122
383e508b
DG
123//All tap dance functions would go here. Only showing this one.
124qk_tap_dance_action_t tap_dance_actions[] = {
125 [CT_CLN] = ACTION_TAP_DANCE_FN_ADVANCED (NULL, dance_cln_finished, dance_cln_reset)
126};
127```
7b0356d1 128### Example 2: Send "Safety Dance!" After 100 Taps
383e508b 129```c
995c3141
JH
130void dance_egg (qk_tap_dance_state_t *state, void *user_data) {
131 if (state->count >= 100) {
132 SEND_STRING ("Safety dance!");
133 reset_tap_dance (state);
134 }
135}
136
383e508b
DG
137qk_tap_dance_action_t tap_dance_actions[] = {
138 [CT_EGG] = ACTION_TAP_DANCE_FN (dance_egg)
139};
140```
141
7b0356d1 142### Example 3: Turn LED Lights On Then Off, One at a Time
383e508b
DG
143
144```c
995c3141
JH
145// on each tap, light up one led, from right to left
146// on the forth tap, turn them off from right to left
147void dance_flsh_each(qk_tap_dance_state_t *state, void *user_data) {
148 switch (state->count) {
149 case 1:
150 ergodox_right_led_3_on();
151 break;
152 case 2:
153 ergodox_right_led_2_on();
154 break;
155 case 3:
156 ergodox_right_led_1_on();
157 break;
158 case 4:
159 ergodox_right_led_3_off();
160 _delay_ms(50);
161 ergodox_right_led_2_off();
162 _delay_ms(50);
163 ergodox_right_led_1_off();
164 }
165}
166
167// on the fourth tap, set the keyboard on flash state
168void dance_flsh_finished(qk_tap_dance_state_t *state, void *user_data) {
169 if (state->count >= 4) {
170 reset_keyboard();
171 reset_tap_dance(state);
172 }
173}
174
af37bb2f 175// if the flash state didn't happen, then turn off LEDs, left to right
995c3141
JH
176void dance_flsh_reset(qk_tap_dance_state_t *state, void *user_data) {
177 ergodox_right_led_1_off();
178 _delay_ms(50);
179 ergodox_right_led_2_off();
180 _delay_ms(50);
181 ergodox_right_led_3_off();
182}
183
383e508b 184//All tap dances now put together. Example 3 is "CT_FLASH"
995c3141
JH
185qk_tap_dance_action_t tap_dance_actions[] = {
186 [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)
188 ,[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)
190};
191```
383e508b
DG
192
193### Example 4: 'Quad Function Tap-Dance'
194
725aa5b8 195By [DanielGGordon](https://github.com/danielggordon)
383e508b
DG
196
197Allow one key to have 4 (or more) functions, depending on number of presses, and if the key is held or tapped.
198Below is a specific example:
199* Tap = Send `x`
200* Hold = Send `Control`
201* Double Tap = Send `Escape`
202* Double Tap and Hold = Send `Alt`
203
e6be4484
DG
204## Setup
205
641bbeb4 206You will need a few things that can be used for 'Quad Function Tap-Dance'.
e6be4484 207
641bbeb4 208You'll need to add these to the top of your `keymap.c` file, before your keymap.
e6be4484 209
383e508b 210```c
e6be4484
DG
211typedef struct {
212 bool is_press_action;
213 int state;
bb86d8a0 214} tap;
e6be4484 215
383e508b
DG
216enum {
217 SINGLE_TAP = 1,
218 SINGLE_HOLD = 2,
219 DOUBLE_TAP = 3,
bb53635f 220 DOUBLE_HOLD = 4,
e6be4484
DG
221 DOUBLE_SINGLE_TAP = 5, //send two single taps
222 TRIPLE_TAP = 6,
223 TRIPLE_HOLD = 7
383e508b
DG
224};
225
e6be4484
DG
226//Tap dance enums
227enum {
bb86d8a0
EO
228 X_CTL = 0,
229 SOME_OTHER_DANCE
230};
e6be4484
DG
231
232int 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);
e6be4484 237
641bbeb4 238```
383e508b 239
641bbeb4 240Now, at the bottom of your `keymap.c` file, you'll need to add the following:
e6be4484
DG
241
242```c
e6be4484
DG
243/* Return an integer that corresponds to what kind of tap dance should be executed.
244 *
245 * How to figure out tap dance state: interrupted and pressed.
246 *
247 * Interrupted: If the state of a dance dance is "interrupted", that means that another key has been hit
248 * under the tapping term. This is typically indicitive that you are trying to "tap" the key.
249 *
250 * Pressed: Whether or not the key is still being pressed. If this value is true, that means the tapping term
251 * has ended, but the key is still being pressed down. This generally means the key is being "held".
252 *
253 * One thing that is currenlty not possible with qmk software in regards to tap dance is to mimic the "permissive hold"
254 * feature. In general, advanced tap dances do not work well if they are used with commonly typed letters.
255 * For example "A". Tap dances are best used on non-letter keys that are not hit while typing letters.
256 *
257 * Good places to put an advanced tap dance:
258 * z,q,x,j,k,v,b, any function key, home/end, comma, semi-colon
259 *
260 * Criteria for "good placement" of a tap dance key:
261 * Not a key that is hit frequently in a sentence
262 * Not a key that is used frequently to double tap, for example 'tab' is often double tapped in a terminal, or
263 * in a web form. So 'tab' would be a poor choice for a tap dance.
264 * Letters used in common words as a double. For example 'p' in 'pepper'. If a tap dance function existed on the
265 * letter 'p', the word 'pepper' would be quite frustating to type.
266 *
267 * For the third point, there does exist the 'DOUBLE_SINGLE_TAP', however this is not fully tested
268 *
269 */
383e508b 270int cur_dance (qk_tap_dance_state_t *state) {
725aa5b8 271 if (state->count == 1) {
e6be4484
DG
272 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'.
725aa5b8
DG
274 else return SINGLE_HOLD;
275 }
4c1164c4 276 else if (state->count == 2) {
e6be4484
DG
277 /*
278 * DOUBLE_SINGLE_TAP is to distinguish between typing "pepper", and actually wanting a double tap
279 * action when hitting 'pp'. Suggested use case for this return value is when you want to send two
280 * keystrokes of the key, and not the 'double tap' action/macro.
281 */
725aa5b8
DG
282 if (state->interrupted) return DOUBLE_SINGLE_TAP;
283 else if (state->pressed) return DOUBLE_HOLD;
284 else return DOUBLE_TAP;
bb53635f 285 }
e6be4484
DG
286 //Assumes no one is trying to type the same letter three times (at least not quickly).
287 //If your tap dance key is 'KC_W', and you want to type "www." quickly - then you will need to add
288 //an exception here to return a 'TRIPLE_SINGLE_TAP', and define that enum just like 'DOUBLE_SINGLE_TAP'
289 if (state->count == 3) {
290 if (state->interrupted || !state->pressed) return TRIPLE_TAP;
291 else return TRIPLE_HOLD;
292 }
293 else return 8; //magic number. At some point this method will expand to work for more presses
383e508b 294}
725aa5b8 295
383e508b 296//instanalize an instance of 'tap' for the 'x' tap dance.
bb53635f 297static tap xtap_state = {
383e508b
DG
298 .is_press_action = true,
299 .state = 0
300};
301
302void x_finished (qk_tap_dance_state_t *state, void *user_data) {
303 xtap_state.state = cur_dance(state);
304 switch (xtap_state.state) {
305 case SINGLE_TAP: register_code(KC_X); break;
306 case SINGLE_HOLD: register_code(KC_LCTRL); break;
307 case DOUBLE_TAP: register_code(KC_ESC); break;
725aa5b8
DG
308 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);
310 //Last case is for fast typing. Assuming your key is `f`:
311 //For example, when typing the word `buffer`, and you want to make sure that you send `ff` and not `Esc`.
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.
383e508b
DG
313 }
314}
315
316void x_reset (qk_tap_dance_state_t *state, void *user_data) {
317 switch (xtap_state.state) {
318 case SINGLE_TAP: unregister_code(KC_X); break;
319 case SINGLE_HOLD: unregister_code(KC_LCTRL); break;
320 case DOUBLE_TAP: unregister_code(KC_ESC); break;
321 case DOUBLE_HOLD: unregister_code(KC_LALT);
725aa5b8 322 case DOUBLE_SINGLE_TAP: unregister_code(KC_X);
383e508b
DG
323 }
324 xtap_state.state = 0;
325}
e6be4484
DG
326
327qk_tap_dance_action_t tap_dance_actions[] = {
328 [X_CTL] = ACTION_TAP_DANCE_FN_ADVANCED(NULL,x_finished, x_reset)
329};
383e508b 330```
e6be4484 331
641bbeb4
DJ
332And then simply use `TD(X_CTL)` anywhere in your keymap.
333
334If you want to implement this in your userspace, then you may want to check out how [DanielGGordon](https://github.com/qmk/qmk_firmware/tree/master/users/gordon) has implemented this in their userspace.
6d4f6f3f 335
634b277b
RA
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`).
337
54c58ea6 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
6d4f6f3f
DP
339
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`.
341
342Below your layers and custom keycodes, add the following:
343
344```c
345// tapdance keycodes
346enum td_keycodes {
347 ALT_LP // Our example key: `LALT` when held, `(` when tapped. Add additional keycodes for each tapdance.
348};
349
350// define a type containing as many tapdance states as you need
351typedef enum {
352 SINGLE_TAP,
353 SINGLE_HOLD,
354 DOUBLE_SINGLE_TAP
355} td_state_t;
356
357// create a global instance of the tapdance state type
358static td_state_t td_state;
359
360// declare your tapdance functions:
361
362// function to determine the current tapdance state
363int cur_dance (qk_tap_dance_state_t *state);
364
365// `finished` and `reset` functions for each tapdance keycode
366void altlp_finished (qk_tap_dance_state_t *state, void *user_data);
367void altlp_reset (qk_tap_dance_state_t *state, void *user_data);
368```
369
370Below your `LAYOUT`, define each of the tapdance functions:
371
372```c
373// determine the tapdance state to return
374int cur_dance (qk_tap_dance_state_t *state) {
375 if (state->count == 1) {
376 if (state->interrupted || !state->pressed) { return SINGLE_TAP; }
377 else { return SINGLE_HOLD; }
378 }
379 if (state->count == 2) { return DOUBLE_SINGLE_TAP; }
380 else { return 3; } // any number higher than the maximum state value you return above
381}
382
383// handle the possible states for each tapdance keycode you define:
384
385void altlp_finished (qk_tap_dance_state_t *state, void *user_data) {
386 td_state = cur_dance(state);
387 switch (td_state) {
388 case SINGLE_TAP:
389 register_code16(KC_LPRN);
390 break;
391 case SINGLE_HOLD:
392 register_mods(MOD_BIT(KC_LALT)); // for a layer-tap key, use `layer_on(_MY_LAYER)` here
393 break;
394 case DOUBLE_SINGLE_TAP: // allow nesting of 2 parens `((` within tapping term
395 tap_code16(KC_LPRN);
396 register_code16(KC_LPRN);
397 }
398}
399
400void altlp_reset (qk_tap_dance_state_t *state, void *user_data) {
401 switch (td_state) {
402 case SINGLE_TAP:
403 unregister_code16(KC_LPRN);
404 break;
405 case SINGLE_HOLD:
406 unregister_mods(MOD_BIT(KC_LALT)); // for a layer-tap key, use `layer_off(_MY_LAYER)` here
407 break;
408 case DOUBLE_SINGLE_TAP:
409 unregister_code16(KC_LPRN);
410 }
411}
412
413// define `ACTION_TAP_DANCE_FN_ADVANCED()` for each tapdance keycode, passing in `finished` and `reset` functions
414qk_tap_dance_action_t tap_dance_actions[] = {
415 [ALT_LP] = ACTION_TAP_DANCE_FN_ADVANCED(NULL, altlp_finished, altlp_reset)
416};
417```
418
419Wrap each tapdance keycode in `TD()` when including it in your keymap, e.g. `TD(ALT_LP)`.
a747953d 420
421### Example 6: Using tap dance for momentary-layer-switch and layer-toggle keys
422
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.
424
425The first step is to include the following code towards the beginning of your `keymap.c`:
426
e2ec5790 427```c
a747953d 428typedef struct {
429 bool is_press_action;
430 int state;
431} tap;
432
433//Define a type for as many tap dance states as you need
434enum {
435 SINGLE_TAP = 1,
436 SINGLE_HOLD = 2,
437 DOUBLE_TAP = 3
438};
439
440enum {
441 QUOT_LAYR = 0 //Our custom tap dance key; add any other tap dance keys to this enum
442};
443
444//Declare the functions to be used with your tap dance key(s)
445
446//Function associated with all tap dances
447int cur_dance (qk_tap_dance_state_t *state);
448
449//Functions associated with individual tap dances
450void ql_finished (qk_tap_dance_state_t *state, void *user_data);
451void ql_reset (qk_tap_dance_state_t *state, void *user_data);
a747953d 452```
453
a747953d 454Towards the bottom of your `keymap.c`, include the following code:
455
e2ec5790 456```c
a747953d 457//Determine the current tap dance state
458int cur_dance (qk_tap_dance_state_t *state) {
459 if (state->count == 1) {
e2ec5790
ED
460 if (!state->pressed) {
461 return SINGLE_TAP;
462 } else {
463 return SINGLE_HOLD;
464 }
465 } else if (state->count == 2) {
466 return DOUBLE_TAP;
467 }
a747953d 468 else return 8;
469}
470
471//Initialize tap structure associated with example tap dance key
472static tap ql_tap_state = {
473 .is_press_action = true,
474 .state = 0
475};
476
477//Functions that control what our tap dance key does
478void ql_finished (qk_tap_dance_state_t *state, void *user_data) {
479 ql_tap_state.state = cur_dance(state);
480 switch (ql_tap_state.state) {
e2ec5790
ED
481 case SINGLE_TAP:
482 tap_code(KC_QUOT);
483 break;
484 case SINGLE_HOLD:
485 layer_on(_MY_LAYER);
486 break;
a747953d 487 case DOUBLE_TAP:
e2ec5790
ED
488 //check to see if the layer is already set
489 if (layer_state_is(_MY_LAYER)) {
490 //if already set, then switch it off
491 layer_off(_MY_LAYER);
492 } else {
493 //if not already set, then switch the layer on
494 layer_on(_MY_LAYER);
495 }
496 break;
a747953d 497 }
498}
499
500void ql_reset (qk_tap_dance_state_t *state, void *user_data) {
e2ec5790
ED
501 //if the key was held down and now is released then switch off the layer
502 if (ql_tap_state.state==SINGLE_HOLD) {
503 layer_off(_MY_LAYER);
504 }
a747953d 505 ql_tap_state.state = 0;
506}
507
508//Associate our tap dance key with its functionality
509qk_tap_dance_action_t tap_dance_actions[] = {
510 [QUOT_LAYR] = ACTION_TAP_DANCE_FN_ADVANCED_TIME(NULL, ql_finished, ql_reset, 275)
511};
512```
513
e2ec5790 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.
a747953d 515
516The use of `cur_dance()` and `ql_tap_state` mirrors the above examples.
517
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)`.
519
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.
521
522Finally, to get this tap dance key working, be sure to include `TD(QUOT_LAYR)` in your `keymaps[]`.