summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorRyan <fauxpark@gmail.com>2023-02-13 03:19:02 +1100
committerGitHub <noreply@github.com>2023-02-13 03:19:02 +1100
commitbbf7a20b33de2d203518687cb5cd1aa85005ea27 (patch)
treeee1a5c412a02021d085c81a26321c3424eca7022 /docs
parentd10350cd2ceb2b9d80522cdec3ea908118f7fd35 (diff)
Refactor Leader key feature (#19632)
Co-authored-by: Drashna Jaelre <drashna@live.com>
Diffstat (limited to 'docs')
-rw-r--r--docs/feature_leader_key.md338
1 files changed, 235 insertions, 103 deletions
diff --git a/docs/feature_leader_key.md b/docs/feature_leader_key.md
index d3dc9a56db..72a6818dd1 100644
--- a/docs/feature_leader_key.md
+++ b/docs/feature_leader_key.md
@@ -1,165 +1,297 @@
-# The Leader Key: A New Kind of Modifier
+# The Leader Key: A New Kind of Modifier :id=the-leader-key
-If you've ever used Vim, you know what a Leader key is. If not, you're about to discover a wonderful concept. :) Instead of hitting Alt+Shift+W for example (holding down three keys at the same time), what if you could hit a _sequence_ of keys instead? So you'd hit our special modifier (the Leader key), followed by W and then C (just a rapid succession of keys), and something would happen.
+If you're a Vim user, you probably know what a Leader key is. In contrast to [Combos](feature_combo.md), the Leader key allows you to hit a *sequence* of up to five keys instead, which triggers some custom functionality once complete.
-That's what `QK_LEAD` does. Here's an example:
+## Usage :id=usage
-1. Pick a key on your keyboard you want to use as the Leader key. Assign it the keycode `QK_LEAD`. This key would be dedicated just for this -- it's a single action key, can't be used for anything else.
-2. Include the line `#define LEADER_TIMEOUT 300` in your `config.h`. This sets the timeout for the `QK_LEAD` key. Specifically, when you press the `QK_LEAD` key, you only have a certain amount of time to complete the Leader Key sequence. The `300` here sets that to 300ms, and you can increase this value to give you more time to hit the sequence. But any keys pressed during this timeout are intercepted and not sent, so you may want to keep this value low.
- * By default, this timeout is how long after pressing `QK_LEAD` to complete your entire sequence. This may be very low for some people. So you may want to increase this timeout. Optionally, you may want to enable the `LEADER_PER_KEY_TIMING` option, which resets the timeout after each key is tapped. This allows you to maintain a low value here, but still be able to use the longer sequences. To enable this option, add `#define LEADER_PER_KEY_TIMING` to your `config.h`.
-3. Within your `matrix_scan_user` function, add something like this:
+Add the following to your `rules.mk`:
-```c
-LEADER_EXTERNS();
-
-void matrix_scan_user(void) {
- LEADER_DICTIONARY() {
- leading = false;
- leader_end();
-
- SEQ_ONE_KEY(KC_F) {
- // Anything you can do in a macro.
- SEND_STRING("QMK is awesome.");
- }
- SEQ_TWO_KEYS(KC_D, KC_D) {
- SEND_STRING(SS_LCTL("a") SS_LCTL("c"));
- }
- SEQ_THREE_KEYS(KC_D, KC_D, KC_S) {
- SEND_STRING("https://start.duckduckgo.com\n");
- }
- SEQ_TWO_KEYS(KC_A, KC_S) {
- register_code(KC_LGUI);
- register_code(KC_S);
- unregister_code(KC_S);
- unregister_code(KC_LGUI);
- }
- }
-}
+```make
+LEADER_ENABLE = yes
```
-As you can see, you have a few functions. You can use `SEQ_ONE_KEY` for single-key sequences (Leader followed by just one key), and `SEQ_TWO_KEYS`, `SEQ_THREE_KEYS` up to `SEQ_FIVE_KEYS` for longer sequences.
+Then add the `QK_LEAD` keycode to your keymap.
-Each of these accepts one or more keycodes as arguments. This is an important point: You can use keycodes from **any layer on your keyboard**. That layer would need to be active for the leader macro to fire, obviously.
+## Callbacks :id=callbacks
-## Adding Leader Key Support
+These callbacks are invoked when the leader sequence begins and ends. In the latter you can implement your custom functionality based on the contents of the sequence buffer.
-To enable Leader Key, add the following line to your keymap's `rules.mk`:
+```c
+void leader_start_user(void) {
+ // Do something when the leader key is pressed
+}
-```make
-LEADER_ENABLE = yes
+void leader_end_user(void) {
+ if (leader_sequence_one_key(KC_F)) {
+ // Leader, f => Types the below string
+ SEND_STRING("QMK is awesome.");
+ } else if (leader_sequence_two_keys(KC_D, KC_D)) {
+ // Leader, d, d => Ctrl+A, Ctrl+C
+ SEND_STRING(SS_LCTL("a") SS_LCTL("c"));
+ } else if (leader_sequence_three_keys(KC_D, KC_D, KC_S)) {
+ // Leader, d, d, s => Types the below string
+ SEND_STRING("https://start.duckduckgo.com\n");
+ } else if (leader_sequence_two_keys(KC_A, KC_S)) {
+ // Leader, a, s => GUI+S
+ tap_code16(LGUI(KC_S));
+ }
+}
```
-Place the following macro in your `keymap.c` or user space source file, before any functional code. It handles declaration of external variables that will be referenced by Leader Key codes that follows:
+## Basic Configuration :id=basic-configuration
+
+### Timeout :id=timeout
+
+This is the amount of time you have to complete a sequence once the leader key has been pressed. The default value is 300 milliseconds, but you can change this by adding the following to your `config.h`:
```c
-LEADER_EXTERNS();
+#define LEADER_TIMEOUT 350
```
-## Per Key Timing on Leader keys
+### Per-Key Timeout :id=per-key-timeout
-Rather than relying on an incredibly high timeout for long leader key strings or those of us without 200wpm typing skills, we can enable per key timing to ensure that each key pressed provides us with more time to finish our stroke. This is incredibly helpful with leader key emulation of tap dance (read: multiple taps of the same key like C, C, C).
+Rather than relying on an incredibly high timeout for long leader key strings or those of us without 200 wpm typing skills, you can enable per-key timing to ensure that each key pressed provides you with more time to finish the sequence. This is incredibly helpful with leader key emulation of tap dance (such as multiple taps of the same key like C, C, C).
+
+To enable this, add the following to your `config.h`:
-In order to enable this, place this in your `config.h`:
```c
#define LEADER_PER_KEY_TIMING
```
-After this, it's recommended that you lower your `LEADER_TIMEOUT` to something less that 300ms.
+After this, it's recommended that you lower your timeout below 300 ms:
```c
#define LEADER_TIMEOUT 250
```
-Now, something like this won't seem impossible to do without a 1000MS leader key timeout:
+Now, something like this won't seem impossible to do without a 1000 millisecond timeout:
```c
-SEQ_THREE_KEYS(KC_C, KC_C, KC_C) {
- SEND_STRING("Per key timing is great!!!");
+if (leader_sequence_three_keys(KC_C, KC_C, KC_C)) {
+ SEND_STRING("Per key timing is great!!!");
}
```
-## Infinite Leader key timeout
+### Disabling Initial Timeout :id=disabling-initial-timeout
+
+Sometimes your leader key may be too far away from the rest of the keys in the sequence. Imagine that your leader key is one of your outer top right keys - you may need to reposition your hand just to reach your leader key. This can make typing the entire sequence on time hard difficult if you are able to type most of the sequence fast. For example, if your sequence is `Leader + asd`, typing `asd` fast is very easy once you have your hands in your home row, but starting the sequence in time after moving your hand out of the home row to reach the leader key and back is not.
-Sometimes your leader key is not on a comfortable place as the rest of keys on your sequence. Imagine that your leader key is one of your outer top right keys, you may need to reposition your hand just to reach your leader key.
-This can make typing the entire sequence on time hard even if you are able to type most of the sequence fast. For example, if your sequence is `Leader + asd` typing `asd` fast is very easy once you have your hands in your home row. However starting the sequence in time after moving your hand out of the home row to reach the leader key and back is not.
-To remove the stress this situation produces to your hands you can enable an infinite timeout just for the leader key. This means that after you hit the leader key you will have an infinite amount of time to start the rest of the sequence, allowing you to proper position your hands on the best position to type the rest of the sequence comfortably.
-This infinite timeout only affects the leader key, so in our previous example of `Leader + asd` you will have an infinite amount of time between `Leader` and `a`, but once you start the sequence the timeout you have configured (global or per key) will work normally.
-This way you can configure a very short `LEADER_TIMEOUT` but still have plenty of time to position your hands.
+To remove the stress this situation produces to your hands, you can disable the timeout just for the leader key. Add the following to your `config.h`:
-In order to enable this, place this in your `config.h`:
```c
#define LEADER_NO_TIMEOUT
```
-## Strict Key Processing
-
-By default, the Leader Key feature will filter the keycode out of [`Mod-Tap`](mod_tap.md) and [`Layer Tap`](feature_layers.md#switching-and-toggling-layers) functions when checking for the Leader sequences. That means if you're using `LT(3, KC_A)`, it will pick this up as `KC_A` for the sequence, rather than `LT(3, KC_A)`, giving a more expected behavior for newer users.
+Now, after you hit the leader key, you will have an infinite amount of time to start the rest of the sequence, allowing you to properly position your hands to type the rest of the sequence comfortably. This way you can configure a very short `LEADER_TIMEOUT`, but still have plenty of time to position your hands.
-While, this may be fine for most, if you want to specify the whole keycode (eg, `LT(3, KC_A)` from the example above) in the sequence, you can enable this by adding `#define LEADER_KEY_STRICT_KEY_PROCESSING` to your `config.h` file. This will then disable the filtering, and you'll need to specify the whole keycode.
+### Strict Key Processing :id=strict-key-processing
-## Customization
+By default, only the "tap keycode" portions of [Mod-Taps](mod_tap.md) and [Layer Taps](feature_layers.md#switching-and-toggling-layers) are added to the sequence buffer. This means if you press eg. `LT(3, KC_A)` as part of a sequence, `KC_A` will be added to the buffer, rather than the entire `LT(3, KC_A)` keycode.
-The Leader Key feature has some additional customization to how the Leader Key feature works. It has two functions that can be called at certain parts of the process. Namely `leader_start_user()` and `leader_end_user()`.
+This gives a more expected behaviour for most users, however you may want to change this.
-The `leader_start_user()` function is called when you tap the `QK_LEAD` key, and the `leader_end_user()` function is called when either the leader sequence is completed, or the leader timeout is hit.
-
-You can add these functions to your code (`keymap.c` usually) to add feedback to the Leader sequences (such as beeping or playing music).
+To enable this, add the following to your `config.h`:
```c
-void leader_start_user(void) {
- // sequence started
-}
-
-void leader_end_user(void) {
- // sequence ended (no success/failure detection)
-}
+#define LEADER_KEY_STRICT_KEY_PROCESSING
```
-### Example
+## Example :id=example
-This example will play the Mario "One Up" sound when you hit `QK_LEAD` to start the Leader Sequence, and will play "All Star" if it completes successfully or "Rick Roll" you if it fails.
+This example will play the Mario "One Up" sound when you hit `QK_LEAD` to start the leader sequence. When the sequence ends, it will play "All Star" if it completes successfully or "Rick Roll" you if it fails (in other words, no sequence matched).
```c
-bool did_leader_succeed;
#ifdef AUDIO_ENABLE
-float leader_start[][2] = SONG(ONE_UP_SOUND );
-float leader_succeed[][2] = SONG(ALL_STAR);
-float leader_fail[][2] = SONG(RICK_ROLL);
+float leader_start_song[][2] = SONG(ONE_UP_SOUND);
+float leader_succeed_song[][2] = SONG(ALL_STAR);
+float leader_fail_song[][2] = SONG(RICK_ROLL);
#endif
-LEADER_EXTERNS();
-
-void matrix_scan_user(void) {
- LEADER_DICTIONARY() {
- did_leader_succeed = leading = false;
-
- SEQ_ONE_KEY(KC_E) {
- // Anything you can do in a macro.
- SEND_STRING(SS_LCTL(SS_LSFT("t")));
- did_leader_succeed = true;
- } else
- SEQ_TWO_KEYS(KC_E, KC_D) {
- SEND_STRING(SS_LGUI("r") "cmd\n" SS_LCTL("c"));
- did_leader_succeed = true;
- }
- leader_end();
- }
-}
void leader_start_user(void) {
#ifdef AUDIO_ENABLE
- PLAY_SONG(leader_start);
+ PLAY_SONG(leader_start_song);
#endif
}
void leader_end_user(void) {
- if (did_leader_succeed) {
-#ifdef AUDIO_ENABLE
- PLAY_SONG(leader_succeed);
-#endif
- } else {
+ bool did_leader_succeed = false;
+
+ if (leader_sequence_one_key(KC_E)) {
+ SEND_STRING(SS_LCTL(SS_LSFT("t")));
+ did_leader_succeed = true;
+ } else if (leader_sequence_two_keys(KC_E, KC_D)) {
+ SEND_STRING(SS_LGUI("r") "cmd\n" SS_LCTL("c"));
+ did_leader_succeed = true;
+ }
+
#ifdef AUDIO_ENABLE
- PLAY_SONG(leader_fail);
+ if (did_leader_succeed) {
+ PLAY_SONG(leader_succeed_song);
+ } else {
+ PLAY_SONG(leader_fail_song);
+ }
#endif
- }
}
```
+
+## Keycodes :id=keycodes
+
+|Key |Aliases |Description |
+|-----------------------|---------|-------------------------|
+|`QK_LEADER` |`QK_LEAD`|Begin the leader sequence|
+
+## API :id=api
+
+### `void leader_start_user(void)` :id=api-leader-start-user
+
+User callback, invoked when the leader sequence begins.
+
+---
+
+### `void leader_end_user(void)` :id=api-leader-end-user
+
+User callback, invoked when the leader sequence ends.
+
+---
+
+### `void leader_start(void)` :id=api-leader-start
+
+Begin the leader sequence, resetting the buffer and timer.
+
+---
+
+### `void leader_end(void)` :id=api-leader-end
+
+End the leader sequence.
+
+---
+
+### `bool leader_sequence_active(void)` :id=api-leader-sequence-active
+
+Whether the leader sequence is active.
+
+---
+
+### `bool leader_sequence_add(uint16_t keycode)` :id=api-leader-sequence-add
+
+Add the given keycode to the sequence buffer.
+
+If `LEADER_NO_TIMEOUT` is defined, the timer is reset if the buffer is empty.
+
+#### Arguments :id=api-leader-sequence-add-arguments
+
+ - `uint16_t keycode`
+ The keycode to add.
+
+#### Return Value :id=api-leader-sequence-add-return
+
+`true` if the keycode was added, `false` if the buffer is full.
+
+---
+
+### `bool leader_sequence_timed_out(void)` :id=api-leader-sequence-timed-out
+
+Whether the leader sequence has reached the timeout.
+
+If `LEADER_NO_TIMEOUT` is defined, the buffer must also contain at least one key.
+
+---
+
+### `bool leader_reset_timer(void)` :id=api-leader-reset-timer
+
+Reset the leader sequence timer.
+
+---
+
+### `bool leader_sequence_one_key(uint16_t kc)` :id=api-leader-sequence-one-key
+
+Check the sequence buffer for the given keycode.
+
+#### Arguments :id=api-leader-sequence-one-key-arguments
+
+ - `uint16_t kc`
+ The keycode to check.
+
+#### Return Value :id=api-leader-sequence-one-key-return
+
+`true` if the sequence buffer matches.
+
+---
+
+### `bool leader_sequence_two_keys(uint16_t kc1, uint16_t kc2)` :id=api-leader-sequence-two-keys
+
+Check the sequence buffer for the given keycodes.
+
+#### Arguments :id=api-leader-sequence-two-keys-arguments
+
+ - `uint16_t kc1`
+ The first keycode to check.
+ - `uint16_t kc2`
+ The second keycode to check.
+
+#### Return Value :id=api-leader-sequence-two-keys-return
+
+`true` if the sequence buffer matches.
+
+---
+
+### `bool leader_sequence_three_keys(uint16_t kc1, uint16_t kc2, uint16_t kc3)` :id=api-leader-sequence-three-keys
+
+Check the sequence buffer for the given keycodes.
+
+#### Arguments :id=api-leader-sequence-three-keys-arguments
+
+ - `uint16_t kc1`
+ The first keycode to check.
+ - `uint16_t kc2`
+ The second keycode to check.
+ - `uint16_t kc3`
+ The third keycode to check.
+
+#### Return Value :id=api-leader-sequence-three-keys-return
+
+`true` if the sequence buffer matches.
+
+---
+
+### `bool leader_sequence_four_keys(uint16_t kc1, uint16_t kc2, uint16_t kc3, uint16_t kc4)` :id=api-leader-sequence-four-keys
+
+Check the sequence buffer for the given keycodes.
+
+#### Arguments :id=api-leader-sequence-four-keys-arguments
+
+ - `uint16_t kc1`
+ The first keycode to check.
+ - `uint16_t kc2`
+ The second keycode to check.
+ - `uint16_t kc3`
+ The third keycode to check.
+ - `uint16_t kc4`
+ The fourth keycode to check.
+
+#### Return Value :id=api-leader-sequence-four-keys-return
+
+`true` if the sequence buffer matches.
+
+---
+
+### `bool leader_sequence_five_keys(uint16_t kc1, uint16_t kc2, uint16_t kc3, uint16_t kc4, uint16_t kc5)` :id=api-leader-sequence-five-keys
+
+Check the sequence buffer for the given keycodes.
+
+#### Arguments :id=api-leader-sequence-five-keys-arguments
+
+ - `uint16_t kc1`
+ The first keycode to check.
+ - `uint16_t kc2`
+ The second keycode to check.
+ - `uint16_t kc3`
+ The third keycode to check.
+ - `uint16_t kc4`
+ The fourth keycode to check.
+ - `uint16_t kc5`
+ The fifth keycode to check.
+
+#### Return Value :id=api-leader-sequence-five-keys-return
+
+`true` if the sequence buffer matches.