You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

563 lines
25 KiB

  1. # Tap Dance: A Single Key Can Do 3, 5, or 100 Different Things
  2. ## Introduction :id=introduction
  3. Hit 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. With 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.
  5. ## How to Use Tap Dance :id=how-to-use
  6. First, 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.
  7. Optionally, you might want to set a custom `TAPPING_TERM` time by adding something like this in your `config.h` file:
  8. ```c
  9. #define TAPPING_TERM 175
  10. #define TAPPING_TERM_PER_KEY
  11. ```
  12. The `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.md#tapping_term), which may be needed because `TAPPING_TERM` affects not just tap-dance keys.
  13. Next, 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.
  14. After 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:
  15. * `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.
  16. * `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).
  17. * `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).
  18. * `ACTION_TAP_DANCE_FN(fn)`: Calls the specified function - defined in the user keymap - with the final tap count of the tap dance action.
  19. * `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.
  20. * `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`).
  21. The 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.
  22. !> Keep in mind that only [basic keycodes](keycodes_basic.md) are supported here. Custom keycodes are not supported.
  23. Similar to the first option, the second and third option are good for simple layer-switching cases.
  24. For 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.
  25. ## Implementation Details :id=implementation
  26. Well, 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!
  27. Let'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)`.
  28. To 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.
  29. This 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.
  30. ## Examples :id=examples
  31. ### Simple Example: Send `ESC` on Single Tap, `CAPS_LOCK` on Double Tap :id=simple-example
  32. Here's a simple example for a single definition:
  33. 1. In your `rules.mk`, add `TAP_DANCE_ENABLE = yes`
  34. 2. In your `keymap.c` file, define the variables and definitions, then add to your keymap:
  35. ```c
  36. // Tap Dance declarations
  37. enum {
  38. TD_ESC_CAPS,
  39. };
  40. // Tap Dance definitions
  41. tap_dance_action_t tap_dance_actions[] = {
  42. // Tap once for Escape, twice for Caps Lock
  43. [TD_ESC_CAPS] = ACTION_TAP_DANCE_DOUBLE(KC_ESC, KC_CAPS),
  44. };
  45. // Add tap dance item to your keymap in place of a keycode
  46. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  47. // ...
  48. TD(TD_ESC_CAPS)
  49. // ...
  50. };
  51. ```
  52. ### Complex Examples :id=complex-examples
  53. This section details several complex tap dance examples.
  54. All the enums used in the examples are declared like this:
  55. ```c
  56. // Enums defined for all examples:
  57. enum {
  58. TD_ESC_CAPS,
  59. CT_EGG,
  60. CT_FLSH,
  61. CT_CLN,
  62. X_CTL,
  63. };
  64. ```
  65. #### Example 1: Send "Safety Dance!" After 100 Taps :id=example-1
  66. ```c
  67. void dance_egg(tap_dance_state_t *state, void *user_data) {
  68. if (state->count >= 100) {
  69. SEND_STRING("Safety dance!");
  70. reset_tap_dance(state);
  71. }
  72. }
  73. tap_dance_action_t tap_dance_actions[] = {
  74. [CT_EGG] = ACTION_TAP_DANCE_FN(dance_egg),
  75. };
  76. ```
  77. #### Example 2: Turn LED Lights On Then Off, One at a Time :id=example-2
  78. ```c
  79. // On each tap, light up one LED, from right to left
  80. // On the fourth tap, turn them off from right to left
  81. void dance_flsh_each(tap_dance_state_t *state, void *user_data) {
  82. switch (state->count) {
  83. case 1:
  84. ergodox_right_led_3_on();
  85. break;
  86. case 2:
  87. ergodox_right_led_2_on();
  88. break;
  89. case 3:
  90. ergodox_right_led_1_on();
  91. break;
  92. case 4:
  93. ergodox_right_led_3_off();
  94. wait_ms(50);
  95. ergodox_right_led_2_off();
  96. wait_ms(50);
  97. ergodox_right_led_1_off();
  98. }
  99. }
  100. // On the fourth tap, set the keyboard on flash state
  101. void dance_flsh_finished(tap_dance_state_t *state, void *user_data) {
  102. if (state->count >= 4) {
  103. reset_keyboard();
  104. }
  105. }
  106. // If the flash state didn't happen, then turn off LEDs, left to right
  107. void dance_flsh_reset(tap_dance_state_t *state, void *user_data) {
  108. ergodox_right_led_1_off();
  109. wait_ms(50);
  110. ergodox_right_led_2_off();
  111. wait_ms(50);
  112. ergodox_right_led_3_off();
  113. }
  114. // All tap dances now put together. Example 2 is "CT_FLSH"
  115. tap_dance_action_t tap_dance_actions[] = {
  116. [TD_ESC_CAPS] = ACTION_TAP_DANCE_DOUBLE(KC_ESC, KC_CAPS),
  117. [CT_EGG] = ACTION_TAP_DANCE_FN(dance_egg),
  118. [CT_FLSH] = ACTION_TAP_DANCE_FN_ADVANCED(dance_flsh_each, dance_flsh_finished, dance_flsh_reset)
  119. };
  120. ```
  121. #### Example 3: Send `:` on Tap, `;` on Hold :id=example-3
  122. With 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()`.
  123. ```c
  124. typedef struct {
  125. uint16_t tap;
  126. uint16_t hold;
  127. uint16_t held;
  128. } tap_dance_tap_hold_t;
  129. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  130. tap_dance_action_t *action;
  131. switch (keycode) {
  132. case TD(CT_CLN): // list all tap dance keycodes with tap-hold configurations
  133. action = &tap_dance_actions[QK_TAP_DANCE_GET_INDEX(keycode)];
  134. if (!record->event.pressed && action->state.count && !action->state.finished) {
  135. tap_dance_tap_hold_t *tap_hold = (tap_dance_tap_hold_t *)action->user_data;
  136. tap_code16(tap_hold->tap);
  137. }
  138. }
  139. return true;
  140. }
  141. void tap_dance_tap_hold_finished(tap_dance_state_t *state, void *user_data) {
  142. tap_dance_tap_hold_t *tap_hold = (tap_dance_tap_hold_t *)user_data;
  143. if (state->pressed) {
  144. if (state->count == 1
  145. #ifndef PERMISSIVE_HOLD
  146. && !state->interrupted
  147. #endif
  148. ) {
  149. register_code16(tap_hold->hold);
  150. tap_hold->held = tap_hold->hold;
  151. } else {
  152. register_code16(tap_hold->tap);
  153. tap_hold->held = tap_hold->tap;
  154. }
  155. }
  156. }
  157. void tap_dance_tap_hold_reset(tap_dance_state_t *state, void *user_data) {
  158. tap_dance_tap_hold_t *tap_hold = (tap_dance_tap_hold_t *)user_data;
  159. if (tap_hold->held) {
  160. unregister_code16(tap_hold->held);
  161. tap_hold->held = 0;
  162. }
  163. }
  164. #define ACTION_TAP_DANCE_TAP_HOLD(tap, hold) \
  165. { .fn = {NULL, tap_dance_tap_hold_finished, tap_dance_tap_hold_reset}, .user_data = (void *)&((tap_dance_tap_hold_t){tap, hold, 0}), }
  166. tap_dance_action_t tap_dance_actions[] = {
  167. [CT_CLN] = ACTION_TAP_DANCE_TAP_HOLD(KC_COLN, KC_SCLN),
  168. };
  169. ```
  170. #### Example 4: 'Quad Function Tap-Dance' :id=example-4
  171. By [DanielGGordon](https://github.com/danielggordon)
  172. Allow one key to have 4 (or more) functions, depending on number of presses, and if the key is held or tapped.
  173. Below is a specific example:
  174. * Tap = Send `x`
  175. * Hold = Send `Control`
  176. * Double Tap = Send `Escape`
  177. * Double Tap and Hold = Send `Alt`
  178. You will need a few things that can be used for 'Quad Function Tap-Dance'.
  179. You'll need to add these to the top of your `keymap.c` file, before your keymap.
  180. ```c
  181. typedef enum {
  182. TD_NONE,
  183. TD_UNKNOWN,
  184. TD_SINGLE_TAP,
  185. TD_SINGLE_HOLD,
  186. TD_DOUBLE_TAP,
  187. TD_DOUBLE_HOLD,
  188. TD_DOUBLE_SINGLE_TAP, // Send two single taps
  189. TD_TRIPLE_TAP,
  190. TD_TRIPLE_HOLD
  191. } td_state_t;
  192. typedef struct {
  193. bool is_press_action;
  194. td_state_t state;
  195. } td_tap_t;
  196. // Tap dance enums
  197. enum {
  198. X_CTL,
  199. SOME_OTHER_DANCE
  200. };
  201. td_state_t cur_dance(tap_dance_state_t *state);
  202. // For the x tap dance. Put it here so it can be used in any keymap
  203. void x_finished(tap_dance_state_t *state, void *user_data);
  204. void x_reset(tap_dance_state_t *state, void *user_data);
  205. ```
  206. Now, at the bottom of your `keymap.c` file, you'll need to add the following:
  207. ```c
  208. /* Return an integer that corresponds to what kind of tap dance should be executed.
  209. *
  210. * How to figure out tap dance state: interrupted and pressed.
  211. *
  212. * Interrupted: If the state of a dance is "interrupted", that means that another key has been hit
  213. * under the tapping term. This is typically indicitive that you are trying to "tap" the key.
  214. *
  215. * Pressed: Whether or not the key is still being pressed. If this value is true, that means the tapping term
  216. * has ended, but the key is still being pressed down. This generally means the key is being "held".
  217. *
  218. * One thing that is currenlty not possible with qmk software in regards to tap dance is to mimic the "permissive hold"
  219. * feature. In general, advanced tap dances do not work well if they are used with commonly typed letters.
  220. * For example "A". Tap dances are best used on non-letter keys that are not hit while typing letters.
  221. *
  222. * Good places to put an advanced tap dance:
  223. * z,q,x,j,k,v,b, any function key, home/end, comma, semi-colon
  224. *
  225. * Criteria for "good placement" of a tap dance key:
  226. * Not a key that is hit frequently in a sentence
  227. * Not a key that is used frequently to double tap, for example 'tab' is often double tapped in a terminal, or
  228. * in a web form. So 'tab' would be a poor choice for a tap dance.
  229. * Letters used in common words as a double. For example 'p' in 'pepper'. If a tap dance function existed on the
  230. * letter 'p', the word 'pepper' would be quite frustating to type.
  231. *
  232. * For the third point, there does exist the 'TD_DOUBLE_SINGLE_TAP', however this is not fully tested
  233. *
  234. */
  235. td_state_t cur_dance(tap_dance_state_t *state) {
  236. if (state->count == 1) {
  237. if (state->interrupted || !state->pressed) return TD_SINGLE_TAP;
  238. // Key has not been interrupted, but the key is still held. Means you want to send a 'HOLD'.
  239. else return TD_SINGLE_HOLD;
  240. } else if (state->count == 2) {
  241. // TD_DOUBLE_SINGLE_TAP is to distinguish between typing "pepper", and actually wanting a double tap
  242. // action when hitting 'pp'. Suggested use case for this return value is when you want to send two
  243. // keystrokes of the key, and not the 'double tap' action/macro.
  244. if (state->interrupted) return TD_DOUBLE_SINGLE_TAP;
  245. else if (state->pressed) return TD_DOUBLE_HOLD;
  246. else return TD_DOUBLE_TAP;
  247. }
  248. // Assumes no one is trying to type the same letter three times (at least not quickly).
  249. // If your tap dance key is 'KC_W', and you want to type "www." quickly - then you will need to add
  250. // an exception here to return a 'TD_TRIPLE_SINGLE_TAP', and define that enum just like 'TD_DOUBLE_SINGLE_TAP'
  251. if (state->count == 3) {
  252. if (state->interrupted || !state->pressed) return TD_TRIPLE_TAP;
  253. else return TD_TRIPLE_HOLD;
  254. } else return TD_UNKNOWN;
  255. }
  256. // Create an instance of 'td_tap_t' for the 'x' tap dance.
  257. static td_tap_t xtap_state = {
  258. .is_press_action = true,
  259. .state = TD_NONE
  260. };
  261. void x_finished(tap_dance_state_t *state, void *user_data) {
  262. xtap_state.state = cur_dance(state);
  263. switch (xtap_state.state) {
  264. case TD_SINGLE_TAP: register_code(KC_X); break;
  265. case TD_SINGLE_HOLD: register_code(KC_LCTL); break;
  266. case TD_DOUBLE_TAP: register_code(KC_ESC); break;
  267. case TD_DOUBLE_HOLD: register_code(KC_LALT); break;
  268. // Last case is for fast typing. Assuming your key is `f`:
  269. // For example, when typing the word `buffer`, and you want to make sure that you send `ff` and not `Esc`.
  270. // 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.
  271. case TD_DOUBLE_SINGLE_TAP: tap_code(KC_X); register_code(KC_X); break;
  272. default: break;
  273. }
  274. }
  275. void x_reset(tap_dance_state_t *state, void *user_data) {
  276. switch (xtap_state.state) {
  277. case TD_SINGLE_TAP: unregister_code(KC_X); break;
  278. case TD_SINGLE_HOLD: unregister_code(KC_LCTL); break;
  279. case TD_DOUBLE_TAP: unregister_code(KC_ESC); break;
  280. case TD_DOUBLE_HOLD: unregister_code(KC_LALT); break;
  281. case TD_DOUBLE_SINGLE_TAP: unregister_code(KC_X); break;
  282. default: break;
  283. }
  284. xtap_state.state = TD_NONE;
  285. }
  286. tap_dance_action_t tap_dance_actions[] = {
  287. [X_CTL] = ACTION_TAP_DANCE_FN_ADVANCED(NULL, x_finished, x_reset)
  288. };
  289. ```
  290. And then simply use `TD(X_CTL)` anywhere in your keymap.
  291. > In 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`).
  292. #### Example 5: Using tap dance for advanced mod-tap and layer-tap keys :id=example-5
  293. Tap 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`.
  294. Below your layers and custom keycodes, add the following:
  295. ```c
  296. // Tap Dance keycodes
  297. enum td_keycodes {
  298. ALT_LP // Our example key: `LALT` when held, `(` when tapped. Add additional keycodes for each tapdance.
  299. };
  300. // Define a type containing as many tapdance states as you need
  301. typedef enum {
  302. TD_NONE,
  303. TD_UNKNOWN,
  304. TD_SINGLE_TAP,
  305. TD_SINGLE_HOLD,
  306. TD_DOUBLE_SINGLE_TAP
  307. } td_state_t;
  308. // Create a global instance of the tapdance state type
  309. static td_state_t td_state;
  310. // Declare your tapdance functions:
  311. // Function to determine the current tapdance state
  312. td_state_t cur_dance(tap_dance_state_t *state);
  313. // `finished` and `reset` functions for each tapdance keycode
  314. void altlp_finished(tap_dance_state_t *state, void *user_data);
  315. void altlp_reset(tap_dance_state_t *state, void *user_data);
  316. ```
  317. Below your `LAYOUT`, define each of the tapdance functions:
  318. ```c
  319. // Determine the tapdance state to return
  320. td_state_t cur_dance(tap_dance_state_t *state) {
  321. if (state->count == 1) {
  322. if (state->interrupted || !state->pressed) return TD_SINGLE_TAP;
  323. else return TD_SINGLE_HOLD;
  324. }
  325. if (state->count == 2) return TD_DOUBLE_SINGLE_TAP;
  326. else return TD_UNKNOWN; // Any number higher than the maximum state value you return above
  327. }
  328. // Handle the possible states for each tapdance keycode you define:
  329. void altlp_finished(tap_dance_state_t *state, void *user_data) {
  330. td_state = cur_dance(state);
  331. switch (td_state) {
  332. case TD_SINGLE_TAP:
  333. register_code16(KC_LPRN);
  334. break;
  335. case TD_SINGLE_HOLD:
  336. register_mods(MOD_BIT(KC_LALT)); // For a layer-tap key, use `layer_on(_MY_LAYER)` here
  337. break;
  338. case TD_DOUBLE_SINGLE_TAP: // Allow nesting of 2 parens `((` within tapping term
  339. tap_code16(KC_LPRN);
  340. register_code16(KC_LPRN);
  341. break;
  342. default:
  343. break;
  344. }
  345. }
  346. void altlp_reset(tap_dance_state_t *state, void *user_data) {
  347. switch (td_state) {
  348. case TD_SINGLE_TAP:
  349. unregister_code16(KC_LPRN);
  350. break;
  351. case TD_SINGLE_HOLD:
  352. unregister_mods(MOD_BIT(KC_LALT)); // For a layer-tap key, use `layer_off(_MY_LAYER)` here
  353. break;
  354. case TD_DOUBLE_SINGLE_TAP:
  355. unregister_code16(KC_LPRN);
  356. break;
  357. default:
  358. break;
  359. }
  360. }
  361. // Define `ACTION_TAP_DANCE_FN_ADVANCED()` for each tapdance keycode, passing in `finished` and `reset` functions
  362. tap_dance_action_t tap_dance_actions[] = {
  363. [ALT_LP] = ACTION_TAP_DANCE_FN_ADVANCED(NULL, altlp_finished, altlp_reset)
  364. };
  365. ```
  366. Wrap each tapdance keycode in `TD()` when including it in your keymap, e.g. `TD(ALT_LP)`.
  367. #### Example 6: Using tap dance for momentary-layer-switch and layer-toggle keys :id=example-6
  368. Tap 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.
  369. The first step is to include the following code towards the beginning of your `keymap.c`:
  370. ```c
  371. // Define a type for as many tap dance states as you need
  372. typedef enum {
  373. TD_NONE,
  374. TD_UNKNOWN,
  375. TD_SINGLE_TAP,
  376. TD_SINGLE_HOLD,
  377. TD_DOUBLE_TAP
  378. } td_state_t;
  379. typedef struct {
  380. bool is_press_action;
  381. td_state_t state;
  382. } td_tap_t;
  383. enum {
  384. QUOT_LAYR, // Our custom tap dance key; add any other tap dance keys to this enum
  385. };
  386. // Declare the functions to be used with your tap dance key(s)
  387. // Function associated with all tap dances
  388. td_state_t cur_dance(tap_dance_state_t *state);
  389. // Functions associated with individual tap dances
  390. void ql_finished(tap_dance_state_t *state, void *user_data);
  391. void ql_reset(tap_dance_state_t *state, void *user_data);
  392. ```
  393. Towards the bottom of your `keymap.c`, include the following code:
  394. ```c
  395. // Determine the current tap dance state
  396. td_state_t cur_dance(tap_dance_state_t *state) {
  397. if (state->count == 1) {
  398. if (!state->pressed) return TD_SINGLE_TAP;
  399. else return TD_SINGLE_HOLD;
  400. } else if (state->count == 2) return TD_DOUBLE_TAP;
  401. else return TD_UNKNOWN;
  402. }
  403. // Initialize tap structure associated with example tap dance key
  404. static td_tap_t ql_tap_state = {
  405. .is_press_action = true,
  406. .state = TD_NONE
  407. };
  408. // Functions that control what our tap dance key does
  409. void ql_finished(tap_dance_state_t *state, void *user_data) {
  410. ql_tap_state.state = cur_dance(state);
  411. switch (ql_tap_state.state) {
  412. case TD_SINGLE_TAP:
  413. tap_code(KC_QUOT);
  414. break;
  415. case TD_SINGLE_HOLD:
  416. layer_on(_MY_LAYER);
  417. break;
  418. case TD_DOUBLE_TAP:
  419. // Check to see if the layer is already set
  420. if (layer_state_is(_MY_LAYER)) {
  421. // If already set, then switch it off
  422. layer_off(_MY_LAYER);
  423. } else {
  424. // If not already set, then switch the layer on
  425. layer_on(_MY_LAYER);
  426. }
  427. break;
  428. default:
  429. break;
  430. }
  431. }
  432. void ql_reset(tap_dance_state_t *state, void *user_data) {
  433. // If the key was held down and now is released then switch off the layer
  434. if (ql_tap_state.state == TD_SINGLE_HOLD) {
  435. layer_off(_MY_LAYER);
  436. }
  437. ql_tap_state.state = TD_NONE;
  438. }
  439. // Associate our tap dance key with its functionality
  440. tap_dance_action_t tap_dance_actions[] = {
  441. [QUOT_LAYR] = ACTION_TAP_DANCE_FN_ADVANCED(NULL, ql_finished, ql_reset)
  442. };
  443. // Set a long-ish tapping term for tap-dance keys
  444. uint16_t get_tapping_term(uint16_t keycode, keyrecord_t *record) {
  445. switch (keycode) {
  446. case QK_TAP_DANCE ... QK_TAP_DANCE_MAX:
  447. return 275;
  448. default:
  449. return TAPPING_TERM;
  450. }
  451. }
  452. ```
  453. The 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.
  454. The use of `cur_dance()` and `ql_tap_state` mirrors the above examples.
  455. The `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)`.
  456. `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`.
  457. Finally, to get this tap dance key working, be sure to include `TD(QUOT_LAYR)` in your `keymaps[]`.