diff options
| author | Pablo MartÃnez <58857054+elpekenin@users.noreply.github.com> | 2023-04-07 12:41:53 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-04-07 20:41:53 +1000 |
| commit | 369c5a213dc4cf805b7dd2e2393901b33e67e500 (patch) | |
| tree | ff2eb8925cdb89a782d8e5fa772eaee0080c763a /docs/feature_layers.md | |
| parent | f076458cd05c52892a95125fdd6b65ce1dc6f6cc (diff) | |
Add layer-cycle example (#19069)
Co-authored-by: Drashna Jaelre <drashna@live.com>
Diffstat (limited to 'docs/feature_layers.md')
| -rw-r--r-- | docs/feature_layers.md | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/docs/feature_layers.md b/docs/feature_layers.md index f8cb53eda4..8503603ffe 100644 --- a/docs/feature_layers.md +++ b/docs/feature_layers.md | |||
| @@ -127,6 +127,54 @@ layer_state_t layer_state_set_user(layer_state_t state) { | |||
| 127 | } | 127 | } |
| 128 | ``` | 128 | ``` |
| 129 | 129 | ||
| 130 | ### Example: Keycode to cycle through layers | ||
| 131 | |||
| 132 | This example shows how to implement a custom keycode to cycle through a range of layers. | ||
| 133 | |||
| 134 | ```c | ||
| 135 | // Define the keycode, `QK_USER` avoids collisions with existing keycodes | ||
| 136 | enum keycodes { | ||
| 137 | KC_CYCLE_LAYERS = QK_USER, | ||
| 138 | }; | ||
| 139 | |||
| 140 | // 1st layer on the cycle | ||
| 141 | #define LAYER_CYCLE_START 0 | ||
| 142 | // Last layer on the cycle | ||
| 143 | #define LAYER_CYCLE_END 4 | ||
| 144 | |||
| 145 | // Add the behaviour of this new keycode | ||
| 146 | bool process_record_user(uint16_t keycode, keyrecord_t *record) { | ||
| 147 | switch (keycode) { | ||
| 148 | case KC_CYCLE_LAYERS: | ||
| 149 | // Our logic will happen on presses, nothing is done on releases | ||
| 150 | if (!record->event.pressed) { | ||
| 151 | // We've already handled the keycode (doing nothing), let QMK know so no further code is run unnecessarily | ||
| 152 | return false; | ||
| 153 | } | ||
| 154 | |||
| 155 | uint8_t current_layer = get_highest_layer(layer_state); | ||
| 156 | |||
| 157 | // Check if we are within the range, if not quit | ||
| 158 | if (curent_layer > LAYER_CYCLE_END || current_layer < LAYER_CYCLE_START) { | ||
| 159 | return false; | ||
| 160 | } | ||
| 161 | |||
| 162 | uint8_t next_layer = current_layer + 1; | ||
| 163 | if (next_layer > LAYER_CYCLE_END) { | ||
| 164 | next_layer = LAYER_CYCLE_START; | ||
| 165 | } | ||
| 166 | layer_move(next_layer); | ||
| 167 | return false; | ||
| 168 | |||
| 169 | // Process other keycodes normally | ||
| 170 | default: | ||
| 171 | return true; | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | // Place `KC_CYCLE_LAYERS` as a keycode in your keymap | ||
| 176 | ``` | ||
| 177 | |||
| 130 | Use the `IS_LAYER_ON_STATE(state, layer)` and `IS_LAYER_OFF_STATE(state, layer)` macros to check the status of a particular layer. | 178 | Use the `IS_LAYER_ON_STATE(state, layer)` and `IS_LAYER_OFF_STATE(state, layer)` macros to check the status of a particular layer. |
| 131 | 179 | ||
| 132 | Outside of `layer_state_set_*` functions, you can use the `IS_LAYER_ON(layer)` and `IS_LAYER_OFF(layer)` macros to check global layer state. | 180 | Outside of `layer_state_set_*` functions, you can use the `IS_LAYER_ON(layer)` and `IS_LAYER_OFF(layer)` macros to check global layer state. |
