aboutsummaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorthomas-d-11 <41303137+thomas-d-11@users.noreply.github.com>2019-07-25 13:53:19 -0500
committerDrashna Jaelre <drashna@live.com>2019-07-25 11:53:19 -0700
commita747953dfae85d3bdfdfe205fc3d4ae6f8d1fe05 (patch)
tree549041b17f6eadad71dbcfcf90f66aebef057019 /docs
parent1ab008eabd10a4c295f7e792033f8504e441f3ac (diff)
downloadqmk_firmware-a747953dfae85d3bdfdfe205fc3d4ae6f8d1fe05.tar.gz
qmk_firmware-a747953dfae85d3bdfdfe205fc3d4ae6f8d1fe05.zip
[Docs] Add Sections and MO(layer)/TG(layer) Example (#6308)
* Add Sections and MO(layer)/TG(layer) Example Major changes: 1. Added sub-section headings to the portion before the examples. 2. Added a new Example 6, that allows MO(layer) and TG(layer) functionality to be embedded within tap dance functions. Minor Changes: 1. Edited some text to better fit with new sub-headings. * Update feature_tap_dance.md * Update feature_tap_dance.md
Diffstat (limited to 'docs')
-rw-r--r--docs/feature_tap_dance.md150
1 files changed, 137 insertions, 13 deletions
diff --git a/docs/feature_tap_dance.md b/docs/feature_tap_dance.md
index b5e5218b0..73e0471a0 100644
--- a/docs/feature_tap_dance.md
+++ b/docs/feature_tap_dance.md
@@ -1,22 +1,33 @@
1# Tap Dance: A Single Key Can Do 3, 5, or 100 Different Things 1# Tap Dance: A Single Key Can Do 3, 5, or 100 Different Things
2 2
3<!-- FIXME: Break this up into multiple sections --> 3## 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: 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:
6 5
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. 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.
8 7
9To make it clear how this is different from `ACTION_FUNCTION_TAP`, let's explore a certain setup! We want one key to send `Space` on single tap, but `Enter` on double-tap. 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.
10 14
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 they are typed within `TAPPING_TERM`. With the tap dance feature, that'll come out as `SPC a`, correctly. 15## How to Use Tap Dance
16But enough of the generalities; lets look at how to actually use Tap Dance!
12 17
13The 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. 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.
14 19
15But lets start with how to use it, first! 20Optionally, you might want to set a custom `TAPPING_TERM` time by adding something like this in you `config.h`:
16 21
17First, 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. 22```
23#define TAPPING_TERM 175
24```
18 25
19This array specifies what actions shall be taken when a tap-dance key is in action. Currently, there are five possible options: 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:
20 31
21* `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. 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.
22* `ACTION_TAP_DANCE_DUAL_ROLE(kc, layer)`: Sends the `kc` keycode when tapped once, or moves to `layer`. (this functions like the `TO` layer keycode). 33* `ACTION_TAP_DANCE_DUAL_ROLE(kc, layer)`: Sends the `kc` keycode when tapped once, or moves to `layer`. (this functions like the `TO` layer keycode).
@@ -24,17 +35,22 @@ This array specifies what actions shall be taken when a tap-dance key is in acti
24* `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. 35* `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.
25* `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`. 36* `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`.
26 37
27The 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. 38The 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.
28 39
29!> Keep in mind that only [basic keycodes](keycodes_basic.md) are supported here. Custom keycodes are not supported. 40!> Keep in mind that only [basic keycodes](keycodes_basic.md) are supported here. Custom keycodes are not supported.
30 41
31And that's the bulk of it! 42Similar to the first option, the second option is good for simple layer-switching cases.
43
44For more complicated cases, use the third or fourth options (examples of each are listed below).
45
46Finally, 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.
32 47
33And now, on to the explanation of how it works! 48## Implementation Details
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!
34 50
35The 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. 51The 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.
36 52
37This 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. 53This 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.
38 54
39Our next stop is `matrix_scan_tap_dance()`. This handles the timeout of tap-dance keys. 55Our next stop is `matrix_scan_tap_dance()`. This handles the timeout of tap-dance keys.
40 56
@@ -397,3 +413,111 @@ qk_tap_dance_action_t tap_dance_actions[] = {
397``` 413```
398 414
399Wrap each tapdance keycode in `TD()` when including it in your keymap, e.g. `TD(ALT_LP)`. 415Wrap each tapdance keycode in `TD()` when including it in your keymap, e.g. `TD(ALT_LP)`.
416
417### Example 6: Using tap dance for momentary-layer-switch and layer-toggle keys
418
419Tap 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.
420
421The first step is to include the following code towards the beginning of your `keymap.c`:
422
423```
424typedef struct {
425 bool is_press_action;
426 int state;
427} tap;
428
429//Define a type for as many tap dance states as you need
430enum {
431 SINGLE_TAP = 1,
432 SINGLE_HOLD = 2,
433 DOUBLE_TAP = 3
434};
435
436enum {
437 QUOT_LAYR = 0 //Our custom tap dance key; add any other tap dance keys to this enum
438};
439
440//Declare the functions to be used with your tap dance key(s)
441
442//Function associated with all tap dances
443int cur_dance (qk_tap_dance_state_t *state);
444
445//Functions associated with individual tap dances
446void ql_finished (qk_tap_dance_state_t *state, void *user_data);
447void ql_reset (qk_tap_dance_state_t *state, void *user_data);
448
449//Declare variable to track which layer is active
450int active_layer;
451```
452
453The above code is similar to that used in previous examples. The one point to note is that you need to declare a variable to keep track of what layer is currently the active layer. We'll see why shortly.
454
455Towards the bottom of your `keymap.c`, include the following code:
456
457```
458//Update active_layer
459uint32_t layer_state_set_user(uint32_t state) {
460 switch (biton32(state)) {
461 case 1:
462 active_layer = 1;
463 break;
464 case 2:
465 active_layer = 2;
466 break;
467 case 3:
468 active_layer = 3;
469 break;
470 default:
471 active_layer = 0;
472 break;
473 }
474 return state;
475}
476
477//Determine the current tap dance state
478int cur_dance (qk_tap_dance_state_t *state) {
479 if (state->count == 1) {
480 if (!state->pressed) {return SINGLE_TAP;}
481 else return SINGLE_HOLD;
482 } else if (state->count == 2) {return DOUBLE_TAP;}
483 else return 8;
484}
485
486//Initialize tap structure associated with example tap dance key
487static tap ql_tap_state = {
488 .is_press_action = true,
489 .state = 0
490};
491
492//Functions that control what our tap dance key does
493void ql_finished (qk_tap_dance_state_t *state, void *user_data) {
494 ql_tap_state.state = cur_dance(state);
495 switch (ql_tap_state.state) {
496 case SINGLE_TAP: tap_code(KC_QUOT); break;
497 case SINGLE_HOLD: layer_on(_MY_LAYER); break;
498 case DOUBLE_TAP:
499 if (active_layer==_MY_LAYER) {layer_off(_MY_LAYER);}
500 else layer_on(_MY_LAYER);
501 }
502}
503
504void ql_reset (qk_tap_dance_state_t *state, void *user_data) {
505 if (ql_tap_state.state==SINGLE_HOLD) {layer_off(_MY_LAYER);}
506 ql_tap_state.state = 0;
507}
508
509//Associate our tap dance key with its functionality
510qk_tap_dance_action_t tap_dance_actions[] = {
511 [QUOT_LAYR] = ACTION_TAP_DANCE_FN_ADVANCED_TIME(NULL, ql_finished, ql_reset, 275)
512};
513```
514
515The is where the real logic of our tap dance key gets worked out. Since `layer_state_set_user()` is called on any layer switch, we use it to update `active_layer`. Our example is assuming that your `keymap.c` includes 4 layers, so adjust the switch statement here to fit your actual number of layers.
516
517The use of `cur_dance()` and `ql_tap_state` mirrors the above examples.
518
519The `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)`.
520
521`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.
522
523Finally, to get this tap dance key working, be sure to include `TD(QUOT_LAYR)` in your `keymaps[]`.