logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

layer_lock.md (4743B)


  1. # Layer Lock
  2. Some [layer switches](../feature_layers#switching-and-toggling-layers) access
  3. the layer by holding the key, including momentary layer `MO(layer)` and layer
  4. tap `LT(layer, key)` keys. You may sometimes need to stay on the layer for a
  5. long period of time. Layer Lock "locks" the current layer to stay on, supposing
  6. it was accessed by one of:
  7. * `MO(layer)` momentary layer switch
  8. * `LT(layer, key)` layer tap
  9. * `OSL(layer)` one-shot layer
  10. * `TT(layer)` layer tap toggle
  11. * `LM(layer, mod)` layer-mod key (the layer is locked, but not the mods)
  12. Press the Layer Lock key again to unlock the layer. Additionally, when a layer
  13. is locked, layer switch keys that turn off the layer such as `TO(other_layer)`
  14. will unlock it.
  15. ## How do I enable Layer Lock
  16. In your rules.mk, add:
  17. ```make
  18. LAYER_LOCK_ENABLE = yes
  19. ```
  20. Pick a key in your keymap on a layer you intend to lock, and assign it the
  21. keycode `QK_LAYER_LOCK` (short alias `QK_LLCK`). Note that locking the base
  22. layer has no effect, so typically, this key is used on layers above the base
  23. layer.
  24. ## Example use
  25. Consider a keymap with the following base layer.
  26. ![Base layer with a MO(NAV) key.](https://i.imgur.com/DkEhj9x.png)
  27. The highlighted key is a momentary layer switch `MO(NAV)`. Holding it accesses a
  28. navigation layer.
  29. ![Nav layer with a Layer Lock key.](https://i.imgur.com/2wUZNWk.png)
  30. Holding the NAV key is fine for brief use, but awkward to continue holding when
  31. using navigation functions continuously. The Layer Lock key comes to the rescue:
  32. 1. Hold the NAV key, activating the navigation layer.
  33. 2. Tap Layer Lock.
  34. 3. Release NAV. The navigation layer stays on.
  35. 4. Make use of the arrow keys, etc.
  36. 5. Tap Layer Lock or NAV again to turn the navigation layer back off.
  37. A variation that would also work is to put the Layer Lock key on the base layer
  38. and make other layers transparent (`KC_TRNS`) in that position. Pressing the
  39. Layer Lock key locks (or unlocks) the highest active layer, regardless of which
  40. layer the Layer Lock key is on.
  41. ## Idle timeout
  42. Optionally, Layer Lock may be configured to unlock if the keyboard is idle
  43. for some time. In config.h, define `LAYER_LOCK_IDLE_TIMEOUT` in units of
  44. milliseconds:
  45. ```c
  46. #define LAYER_LOCK_IDLE_TIMEOUT 60000 // Turn off after 60 seconds.
  47. ```
  48. ## Functions
  49. Use the following functions to query and manipulate the layer lock state.
  50. | Function | Description |
  51. |----------------------------|------------------------------------|
  52. | `is_layer_locked(layer)` | Checks whether `layer` is locked. |
  53. | `layer_lock_on(layer)` | Locks and turns on `layer`. |
  54. | `layer_lock_off(layer)` | Unlocks and turns off `layer`. |
  55. | `layer_lock_invert(layer)` | Toggles whether `layer` is locked. |
  56. ## Representing the current Layer Lock state
  57. There is an optional callback `layer_lock_set_user()` that gets called when a
  58. layer is locked or unlocked. This is useful to represent the current lock state
  59. for instance by setting an LED. In keymap.c, define
  60. ```c
  61. bool layer_lock_set_user(layer_state_t locked_layers) {
  62. // Do something like `set_led(is_layer_locked(NAV));`
  63. return true;
  64. }
  65. ```
  66. The argument `locked_layers` is a bitfield in which the kth bit is on if the kth
  67. layer is locked. Alternatively, you can use `is_layer_locked(layer)` to check if
  68. a given layer is locked.
  69. ## Combine Layer Lock with a mod-tap
  70. It is possible to create a [mod-tap MT key](../mod_tap) that acts as a modifier
  71. on hold and Layer Lock on tap. Since Layer Lock is not a [basic
  72. keycode](../keycodes_basic), attempting `MT(mod, QK_LLCK)` is invalid does not
  73. work directly, yet this effect can be achieved through [changing the tap
  74. function](../mod_tap#changing-tap-function). For example, the following
  75. implements a `SFTLLCK` key that acts as Shift on hold and Layer Lock on tap:
  76. ```c
  77. #define SFTLLCK LSFT_T(KC_0)
  78. // Use SFTLLCK in your keymap...
  79. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  80. switch (keycode) {
  81. case SFTLLCK:
  82. if (record->tap.count) {
  83. if (record->event.pressed) {
  84. // Toggle the lock on the highest layer.
  85. layer_lock_invert(get_highest_layer(layer_state));
  86. }
  87. return false;
  88. }
  89. break;
  90. // Other macros...
  91. }
  92. return true;
  93. }
  94. ```
  95. In the above, `KC_0` is an arbitrary placeholder for the tapping keycode. This
  96. keycode will never be sent, so any basic keycode will do. In
  97. `process_record_user()`, the tap press event is changed to toggle the lock on
  98. the highest layer. Layer Lock can be combined with a [layer-tap LT
  99. key](../feature_layers#switching-and-toggling-layers) similarly.