Skip to content

Commit ab2ee51

Browse files
committed
Layer Lock key.
This commit adds a new "Layer Lock" feature, a key that locks the current layer to stay on assuming it was activated by a `MO(layer)`, `LT(layer, key)`, `OSL(layer)`, or `TT(layer)` key. Pressing the Layer Lock key again unlocks and turns off the layer.
1 parent 0a178bf commit ab2ee51

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ Who knew a keyboard could do so much?
3232
Click](https://getreuer.info/posts/keyboards/mouse-turbo-click/index.html)
3333
– QMK macro that clicks the mouse rapidly
3434

35+
* [Layer Lock key](https://getreuer.info/posts/keyboards/layer-lock/index.html)
36+
– QMK macro to stay in the current layer
37+
3538

3639
## My keymap
3740

features/layer_lock.c

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
//
16+
// For full documentation, see
17+
// https://getreuer.info/posts/keyboards/layer-lock
18+
19+
#include "layer_lock.h"
20+
21+
// The current lock state. The kth bit is on if layer k is locked.
22+
static layer_state_t locked_layers = 0;
23+
24+
bool process_layer_lock(uint16_t keycode, keyrecord_t* record,
25+
uint16_t lock_keycode) {
26+
// The intention is that locked layers remain on. If something outside of
27+
// this feature turned any locked layers off, unlock them.
28+
if ((locked_layers & ~layer_state) != 0) {
29+
layer_lock_set_user(locked_layers &= layer_state);
30+
}
31+
32+
if (keycode == lock_keycode) {
33+
if (record->event.pressed) { // The layer lock key was pressed.
34+
layer_lock_invert(get_highest_layer(layer_state));
35+
}
36+
return false;
37+
}
38+
39+
switch (keycode) {
40+
case QK_MOMENTARY ... QK_MOMENTARY_MAX: // `MO(layer)` keys.
41+
case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX: { // `TT(layer)`.
42+
const uint8_t layer = keycode & 255;
43+
if (is_layer_locked(layer)) {
44+
// Event on `MO` or `TT` key where layer is locked.
45+
if (record->event.pressed) { // On press, unlock the layer.
46+
layer_lock_invert(layer);
47+
}
48+
return false; // Skip default handling.
49+
}
50+
} break;
51+
52+
#ifndef NO_ACTION_TAPPING
53+
case QK_LAYER_TAP ... QK_LAYER_TAP_MAX: // `LT(layer, key)` keys.
54+
if (record->tap.count == 0 && !record->event.pressed &&
55+
is_layer_locked((keycode >> 8) & 15)) {
56+
// Release event on a held layer-tap key where the layer is locked.
57+
return false; // Skip default handling so that layer stays on.
58+
}
59+
break;
60+
#endif // NO_ACTION_TAPPING
61+
}
62+
63+
return true;
64+
}
65+
66+
bool is_layer_locked(uint8_t layer) {
67+
return locked_layers & ((layer_state_t)1 << layer);
68+
}
69+
70+
void layer_lock_invert(uint8_t layer) {
71+
const layer_state_t mask = (layer_state_t)1 << layer;
72+
if ((locked_layers & mask) == 0) { // Layer is being locked.
73+
#ifndef NO_ACTION_ONESHOT
74+
if (layer == get_oneshot_layer()) {
75+
reset_oneshot_layer(); // Reset so that OSL doesn't turn layer off.
76+
}
77+
#endif // NO_ACTION_ONESHOT
78+
layer_on(layer);
79+
} else { // Layer is being unlocked.
80+
layer_off(layer);
81+
}
82+
layer_lock_set_user(locked_layers ^= mask);
83+
}
84+
85+
// Implement layer_lock_on/off by deferring to layer_lock_invert.
86+
void layer_lock_on(uint8_t layer) {
87+
if (!is_layer_locked(layer)) { layer_lock_invert(layer); }
88+
}
89+
90+
void layer_lock_off(uint8_t layer) {
91+
if (is_layer_locked(layer)) { layer_lock_invert(layer); }
92+
}
93+
94+
__attribute__((weak)) void layer_lock_set_user(layer_state_t locked_layers) {}
95+

features/layer_lock.h

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
//
16+
// Layer Lock, a key to stay in the current layer.
17+
//
18+
// Layers are often accessed by holding a button, e.g. with a momentary layer
19+
// switch `MO(layer)` or layer tap `LT(layer, key)` key. But you may sometimes
20+
// want to "lock" or "toggle" the layer so that it stays on without having to
21+
// hold down a button. One way to do that is with a tap-toggle `TT` layer key,
22+
// but here is an alternative.
23+
//
24+
// This library implements a "Layer Lock key". When tapped, it "locks" the
25+
// highest layer to stay active, assuming the layer was activated by one of the
26+
// following keys:
27+
//
28+
// * `MO(layer)` momentary layer switch
29+
// * `LT(layer, key)` layer tap
30+
// * `OSL(layer)` one-shot layer
31+
// * `TT(layer)` layer tap toggle
32+
//
33+
// Tapping the Layer Lock key again unlocks and turns off the layer.
34+
//
35+
// NOTE: When a layer is "locked", other layer keys such as `TO(layer)` or
36+
// manually calling `layer_off(layer)` will override and unlock the layer.
37+
//
38+
// For full documentation, see
39+
// https://getreuer.info/posts/keyboards/layer-lock
40+
41+
#pragma once
42+
43+
#include QMK_KEYBOARD_H
44+
45+
// In your keymap, define a custom keycode to use for Layer Lock. Then handle
46+
// Layer Lock from your `process_record_user` function by calling
47+
// `process_layer_lock`, passing your custom keycode for the `lock_keycode` arg:
48+
//
49+
// #include "features/layer_lock.h"
50+
//
51+
// bool process_record_user(uint16_t keycode, keyrecord_t* record) {
52+
// if (!process_layer_lock(keycode, record, LLOCK)) { return false; }
53+
// // Your macros ...
54+
//
55+
// return true;
56+
// }
57+
bool process_layer_lock(uint16_t keycode, keyrecord_t* record,
58+
uint16_t lock_keycode);
59+
60+
// Returns true if `layer` is currently locked.
61+
bool is_layer_locked(uint8_t layer);
62+
63+
// Locks and turns on `layer`.
64+
void layer_lock_on(uint8_t layer);
65+
66+
// Unlocks and turns off `layer`.
67+
void layer_lock_off(uint8_t layer);
68+
69+
// Toggles whether `layer` is locked.
70+
void layer_lock_invert(uint8_t layer);
71+
72+
// An optional callback that gets called when a layer is locked or unlocked.
73+
// This is useful to represent the current lock state, e.g. by setting an LED or
74+
// playing a sound. In your keymap, define
75+
//
76+
// void layer_lock_set_user(layer_state_t locked_layers) {
77+
// // Do something like `set_led(is_layer_locked(NAV));`
78+
// }
79+
void layer_lock_set_user(layer_state_t locked_layers);
80+

0 commit comments

Comments
 (0)