aboutsummaryrefslogtreecommitdiff
path: root/docs/Tap-Dance.md
diff options
context:
space:
mode:
Diffstat (limited to 'docs/Tap-Dance.md')
-rw-r--r--docs/Tap-Dance.md144
1 files changed, 144 insertions, 0 deletions
diff --git a/docs/Tap-Dance.md b/docs/Tap-Dance.md
new file mode 100644
index 000000000..25827a648
--- /dev/null
+++ b/docs/Tap-Dance.md
@@ -0,0 +1,144 @@
1# Tap Dance: A single key can do 3, 5, or 100 different things
2
3Hit 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:
4
5With 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.
6
7To make it clear how this is different from `ACTION_FUNCTION_TAP`, lets explore a certain setup! We want one key to send `Space` on single tap, but `Enter` on double-tap.
8
9With `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 send first. Thus, `SPC a` will result in `a SPC` being sent, if they are typed within `TAPPING_TERM`. With the tap dance feature, that'll come out as `SPC a`, correctly.
10
11The implementation hooks into two parts of the system, to achieve this: into `process_record_quantum()`, and the matrix scan. We need the latter to be able to time out a tap sequence even when a key is not being pressed, so `SPC` alone will time out and register after `TAPPING_TERM` time.
12
13But lets start with how to use it, first!
14
15First, you will need `TAP_DANCE_ENABLE=yes` in your `Makefile`, because the feature is disabled by default. This adds a little less than 1k to the firmware size. Next, 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.
16
17This array specifies what actions shall be taken when a tap-dance key is in action. Currently, there are three possible options:
18
19* `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.
20* `ACTION_TAP_DANCE_FN(fn)`: Calls the specified function - defined in the user keymap - with the final tap count of the tap dance action.
21* `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 on when the dance action finishes (like the previous option), and the last function when the tap dance action resets.
22
23The first option is enough for a lot of cases, that just want dual roles. For example, `ACTION_TAP_DANCE(KC_SPC, KC_ENT)` will result in `Space` being sent on single-tap, `Enter` otherwise.
24
25And that's the bulk of it!
26
27And now, on to the explanation of how it works!
28
29The 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 the timer.
30
31This means that you have `TAPPING_TERM` time to tap the key again, you do not have to input all the taps within that timeframe. This allows for longer tap counts, with minimal impact on responsiveness.
32
33Our next stop is `matrix_scan_tap_dance()`. This handles the timeout of tap-dance keys.
34
35For 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.
36
37### Examples
38
39Here's a simple example for a single definition:
40
411. In your `makefile`, add `TAP_DANCE_ENABLE = yes`
422. In your `config.h` (which you can copy from `qmk_firmware/keyboards/planck/config.h` to your keymap directory), add `#define TAPPING_TERM 200`
433. In your `keymap.c` file, define the variables and definitions, then add to your keymap:
44
45```c
46//Tap Dance Declarations
47enum {
48 TD_ESC_CAPS = 0
49};
50
51//Tap Dance Definitions
52qk_tap_dance_action_t tap_dance_actions[] = {
53 //Tap once for Esc, twice for Caps Lock
54 [TD_ESC_CAPS] = ACTION_TAP_DANCE_DOUBLE(KC_ESC, KC_CAPS)
55// Other declarations would go here, separated by commas, if you have them
56};
57
58//In Layer declaration, add tap dance item in place of a key code
59TD(TD_ESC_CAPS)
60```
61
62Here's a more complex example involving custom actions:
63
64```c
65enum {
66 CT_SE = 0,
67 CT_CLN,
68 CT_EGG,
69 CT_FLSH,
70};
71
72/* Have the above three on the keymap, TD(CT_SE), etc... */
73
74void dance_cln_finished (qk_tap_dance_state_t *state, void *user_data) {
75 if (state->count == 1) {
76 register_code (KC_RSFT);
77 register_code (KC_SCLN);
78 } else {
79 register_code (KC_SCLN);
80 }
81}
82
83void dance_cln_reset (qk_tap_dance_state_t *state, void *user_data) {
84 if (state->count == 1) {
85 unregister_code (KC_RSFT);
86 unregister_code (KC_SCLN);
87 } else {
88 unregister_code (KC_SCLN);
89 }
90}
91
92void dance_egg (qk_tap_dance_state_t *state, void *user_data) {
93 if (state->count >= 100) {
94 SEND_STRING ("Safety dance!");
95 reset_tap_dance (state);
96 }
97}
98
99// on each tap, light up one led, from right to left
100// on the forth tap, turn them off from right to left
101void dance_flsh_each(qk_tap_dance_state_t *state, void *user_data) {
102 switch (state->count) {
103 case 1:
104 ergodox_right_led_3_on();
105 break;
106 case 2:
107 ergodox_right_led_2_on();
108 break;
109 case 3:
110 ergodox_right_led_1_on();
111 break;
112 case 4:
113 ergodox_right_led_3_off();
114 _delay_ms(50);
115 ergodox_right_led_2_off();
116 _delay_ms(50);
117 ergodox_right_led_1_off();
118 }
119}
120
121// on the fourth tap, set the keyboard on flash state
122void dance_flsh_finished(qk_tap_dance_state_t *state, void *user_data) {
123 if (state->count >= 4) {
124 reset_keyboard();
125 reset_tap_dance(state);
126 }
127}
128
129// if the flash state didnt happen, then turn off leds, left to right
130void dance_flsh_reset(qk_tap_dance_state_t *state, void *user_data) {
131 ergodox_right_led_1_off();
132 _delay_ms(50);
133 ergodox_right_led_2_off();
134 _delay_ms(50);
135 ergodox_right_led_3_off();
136}
137
138qk_tap_dance_action_t tap_dance_actions[] = {
139 [CT_SE] = ACTION_TAP_DANCE_DOUBLE (KC_SPC, KC_ENT)
140 ,[CT_CLN] = ACTION_TAP_DANCE_FN_ADVANCED (NULL, dance_cln_finished, dance_cln_reset)
141 ,[CT_EGG] = ACTION_TAP_DANCE_FN (dance_egg)
142 ,[CT_FLSH] = ACTION_TAP_DANCE_FN_ADVANCED (dance_flsh_each, dance_flsh_finished, dance_flsh_reset)
143};
144```