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.

250 lines
10 KiB

  1. # Tap Dance: A Single Key Can Do 3, 5, or 100 Different Things
  2. <!-- FIXME: Break this up into multiple sections -->
  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. To 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.
  6. With `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.
  7. The 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.
  8. But lets start with how to use it, first!
  9. 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. 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.
  10. This array specifies what actions shall be taken when a tap-dance key is in action. Currently, there are three possible options:
  11. * `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.
  12. * `ACTION_TAP_DANCE_FN(fn)`: Calls the specified function - defined in the user keymap - with the final tap count of the tap dance action.
  13. * `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.
  14. The 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.
  15. And that's the bulk of it!
  16. And now, on to the explanation of how it works!
  17. The 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.
  18. This 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.
  19. Our next stop is `matrix_scan_tap_dance()`. This handles the timeout of tap-dance keys.
  20. For 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.
  21. # Examples
  22. ## Simple Example
  23. Here's a simple example for a single definition:
  24. 1. In your `rules.mk`, add `TAP_DANCE_ENABLE = yes`
  25. 2. In your `config.h` (which you can copy from `qmk_firmware/keyboards/planck/config.h` to your keymap directory), add `#define TAPPING_TERM 200`
  26. 3. In your `keymap.c` file, define the variables and definitions, then add to your keymap:
  27. ```c
  28. //Tap Dance Declarations
  29. enum {
  30. TD_ESC_CAPS = 0
  31. };
  32. //Tap Dance Definitions
  33. qk_tap_dance_action_t tap_dance_actions[] = {
  34. //Tap once for Esc, twice for Caps Lock
  35. [TD_ESC_CAPS] = ACTION_TAP_DANCE_DOUBLE(KC_ESC, KC_CAPS)
  36. // Other declarations would go here, separated by commas, if you have them
  37. };
  38. //In Layer declaration, add tap dance item in place of a key code
  39. TD(TD_ESC_CAPS)
  40. ```
  41. ## Complex Examples
  42. This section details several complex tap dance examples.
  43. All the enums used in the examples are declared like this:
  44. ```c
  45. // Enums defined for all examples:
  46. enum {
  47. CT_SE = 0,
  48. CT_CLN,
  49. CT_EGG,
  50. CT_FLSH,
  51. X_TAP_DANCE
  52. };
  53. ```
  54. ### Example 1: Send `:` on Single Tap, `;` on Double Tap
  55. ```c
  56. void dance_cln_finished (qk_tap_dance_state_t *state, void *user_data) {
  57. if (state->count == 1) {
  58. register_code (KC_RSFT);
  59. register_code (KC_SCLN);
  60. } else {
  61. register_code (KC_SCLN);
  62. }
  63. }
  64. void dance_cln_reset (qk_tap_dance_state_t *state, void *user_data) {
  65. if (state->count == 1) {
  66. unregister_code (KC_RSFT);
  67. unregister_code (KC_SCLN);
  68. } else {
  69. unregister_code (KC_SCLN);
  70. }
  71. }
  72. //All tap dance functions would go here. Only showing this one.
  73. qk_tap_dance_action_t tap_dance_actions[] = {
  74. [CT_CLN] = ACTION_TAP_DANCE_FN_ADVANCED (NULL, dance_cln_finished, dance_cln_reset)
  75. };
  76. ```
  77. ### Example 2: Send "Safety Dance!" After 100 Taps
  78. ```c
  79. void dance_egg (qk_tap_dance_state_t *state, void *user_data) {
  80. if (state->count >= 100) {
  81. SEND_STRING ("Safety dance!");
  82. reset_tap_dance (state);
  83. }
  84. }
  85. qk_tap_dance_action_t tap_dance_actions[] = {
  86. [CT_EGG] = ACTION_TAP_DANCE_FN (dance_egg)
  87. };
  88. ```
  89. ### Example 3: Turn LED Lights On Then Off, One at a Time
  90. ```c
  91. // on each tap, light up one led, from right to left
  92. // on the forth tap, turn them off from right to left
  93. void dance_flsh_each(qk_tap_dance_state_t *state, void *user_data) {
  94. switch (state->count) {
  95. case 1:
  96. ergodox_right_led_3_on();
  97. break;
  98. case 2:
  99. ergodox_right_led_2_on();
  100. break;
  101. case 3:
  102. ergodox_right_led_1_on();
  103. break;
  104. case 4:
  105. ergodox_right_led_3_off();
  106. _delay_ms(50);
  107. ergodox_right_led_2_off();
  108. _delay_ms(50);
  109. ergodox_right_led_1_off();
  110. }
  111. }
  112. // on the fourth tap, set the keyboard on flash state
  113. void dance_flsh_finished(qk_tap_dance_state_t *state, void *user_data) {
  114. if (state->count >= 4) {
  115. reset_keyboard();
  116. reset_tap_dance(state);
  117. }
  118. }
  119. // if the flash state didn't happen, then turn off LEDs, left to right
  120. void dance_flsh_reset(qk_tap_dance_state_t *state, void *user_data) {
  121. ergodox_right_led_1_off();
  122. _delay_ms(50);
  123. ergodox_right_led_2_off();
  124. _delay_ms(50);
  125. ergodox_right_led_3_off();
  126. }
  127. //All tap dances now put together. Example 3 is "CT_FLASH"
  128. qk_tap_dance_action_t tap_dance_actions[] = {
  129. [CT_SE] = ACTION_TAP_DANCE_DOUBLE (KC_SPC, KC_ENT)
  130. ,[CT_CLN] = ACTION_TAP_DANCE_FN_ADVANCED (NULL, dance_cln_finished, dance_cln_reset)
  131. ,[CT_EGG] = ACTION_TAP_DANCE_FN (dance_egg)
  132. ,[CT_FLSH] = ACTION_TAP_DANCE_FN_ADVANCED (dance_flsh_each, dance_flsh_finished, dance_flsh_reset)
  133. };
  134. ```
  135. ### Example 4: 'Quad Function Tap-Dance'
  136. By [DanielGGordon](https://github.com/danielggordon)
  137. Allow one key to have 4 (or more) functions, depending on number of presses, and if the key is held or tapped.
  138. Below is a specific example:
  139. * Tap = Send `x`
  140. * Hold = Send `Control`
  141. * Double Tap = Send `Escape`
  142. * Double Tap and Hold = Send `Alt`
  143. The following example can be easily expanded to more than 4 quite easily:
  144. ```c
  145. //**************** Definitions needed for quad function to work *********************//
  146. //Enums used to clearly convey the state of the tap dance
  147. enum {
  148. SINGLE_TAP = 1,
  149. SINGLE_HOLD = 2,
  150. DOUBLE_TAP = 3,
  151. DOUBLE_HOLD = 4,
  152. DOUBLE_SINGLE_TAP = 5 //send SINGLE_TAP twice - NOT DOUBLE_TAP
  153. // Add more enums here if you want for triple, quadruple, etc.
  154. };
  155. typedef struct {
  156. bool is_press_action;
  157. int state;
  158. } tap;
  159. int cur_dance (qk_tap_dance_state_t *state) {
  160. if (state->count == 1) {
  161. //If count = 1, and it has been interrupted - it doesn't matter if it is pressed or not: Send SINGLE_TAP
  162. if (state->interrupted || state->pressed==0) return SINGLE_TAP;
  163. else return SINGLE_HOLD;
  164. }
  165. //If count = 2, and it has been interrupted - assume that user is trying to type the letter associated
  166. //with single tap. In example below, that means to send `xx` instead of `Escape`.
  167. else if (state->count == 2) {
  168. if (state->interrupted) return DOUBLE_SINGLE_TAP;
  169. else if (state->pressed) return DOUBLE_HOLD;
  170. else return DOUBLE_TAP;
  171. }
  172. else return 6; //magic number. At some point this method will expand to work for more presses
  173. }
  174. //**************** Definitions needed for quad function to work *********************//
  175. //instanalize an instance of 'tap' for the 'x' tap dance.
  176. static tap xtap_state = {
  177. .is_press_action = true,
  178. .state = 0
  179. };
  180. void x_finished (qk_tap_dance_state_t *state, void *user_data) {
  181. xtap_state.state = cur_dance(state);
  182. switch (xtap_state.state) {
  183. case SINGLE_TAP: register_code(KC_X); break;
  184. case SINGLE_HOLD: register_code(KC_LCTRL); break;
  185. case DOUBLE_TAP: register_code(KC_ESC); break;
  186. case DOUBLE_HOLD: register_code(KC_LALT); break;
  187. case DOUBLE_SINGLE_TAP: register_code(KC_X); unregister_code(KC_X); register_code(KC_X);
  188. //Last case is for fast typing. Assuming your key is `f`:
  189. //For example, when typing the word `buffer`, and you want to make sure that you send `ff` and not `Esc`.
  190. //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.
  191. }
  192. }
  193. void x_reset (qk_tap_dance_state_t *state, void *user_data) {
  194. switch (xtap_state.state) {
  195. case SINGLE_TAP: unregister_code(KC_X); break;
  196. case SINGLE_HOLD: unregister_code(KC_LCTRL); break;
  197. case DOUBLE_TAP: unregister_code(KC_ESC); break;
  198. case DOUBLE_HOLD: unregister_code(KC_LALT);
  199. case DOUBLE_SINGLE_TAP: unregister_code(KC_X);
  200. }
  201. xtap_state.state = 0;
  202. }
  203. ```
  204. And then simply add this to your list of tap dance functions:
  205. `[X_TAP_DANCE] = ACTION_TAP_DANCE_FN_ADVANCED(NULL, x_finished, x_reset)`