You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

254 lines
25 KiB

  1. # Key Overrides :id=key-overrides
  2. Key overrides allow you to override modifier-key combinations to send a different modifier-key combination or perform completely custom actions. Don't want `shift` + `1` to type `!` on your computer? Use a key override to make your keyboard type something different when you press `shift` + `1`. The general behavior is like this: If `modifiers w` + `key x` are pressed, replace these keys with `modifiers y` + `key z` in the keyboard report.
  3. You can use key overrides in a similar way to momentary layer/fn keys to activate custom keycodes/shortcuts, with a number of benefits: You completely keep the original use of the modifier keys, while being able to save space by removing fn keys from your keyboard. You can also easily configure _combinations of modifiers_ to trigger different actions than individual modifiers, and much more. The possibilities are quite vast and this documentation contains a few examples for inspiration throughout.
  4. ##### A few more examples to get started: You could use key overrides to...
  5. - Send `brightness up/down` when pressing `ctrl` + `volume up/down`.
  6. - Send `delete` when pressing `shift` + `backspace`.
  7. - Create custom shortcuts or change existing ones: E.g. Send `ctrl`+`shift`+`z` when `ctrl`+`y` is pressed.
  8. - Run custom code when `ctrl` + `alt` + `esc` is pressed.
  9. ## Setup :id=setup
  10. To enable this feature, you need to add `KEY_OVERRIDE_ENABLE = yes` to your `rules.mk`.
  11. Then, in your `keymap.c` file, you'll need to define the array `key_overrides`, which defines all key overrides to be used. Each override is a value of type `key_override_t`. The array `key_overrides` is `NULL`-terminated and contains pointers to `key_override_t` values (`const key_override_t **`).
  12. ## Creating Key Overrides :id=creating-key-overrides
  13. The `key_override_t` struct has many options that allow you to precisely tune your overrides. The full reference is shown below. Instead of manually creating a `key_override_t` value, it is recommended to use these dedicated initializers:
  14. #### `ko_make_basic(modifiers, key, replacement)`
  15. Returns a `key_override_t`, which sends `replacement` (can be a key-modifier combination), when `key` and `modifiers` are all pressed down. This override still activates if any additional modifiers not specified in `modifiers` are also pressed down. See `ko_make_with_layers_and_negmods` to customize this behavior.
  16. #### `ko_make_with_layers(modifiers, key, replacement, layers)`
  17. Additionally takes a bitmask `layers` that defines on which layers the override is used.
  18. #### `ko_make_with_layers_and_negmods(modifiers, key, replacement, layers, negative_mods)`
  19. Additionally takes a bitmask `negative_mods` that defines which modifiers may not be pressed for this override to activate.
  20. #### `ko_make_with_layers_negmods_and_options(modifiers, key, replacement, layers, negative_mods, options)`
  21. Additionally takes a bitmask `options` that specifies additional options. See `ko_option_t` for available options.
  22. For more customization possibilities, you may directly create a `key_override_t`, which allows you to customize even more behavior. Read further below for details and examples.
  23. ## Simple Example :id=simple-example
  24. This shows how the mentioned example of sending `delete` when `shift` + `backspace` are pressed is realized:
  25. ```c
  26. const key_override_t delete_key_override = ko_make_basic(MOD_MASK_SHIFT, KC_BSPC, KC_DEL);
  27. // This globally defines all key overrides to be used
  28. const key_override_t **key_overrides = (const key_override_t *[]){
  29. &delete_key_override,
  30. NULL // Null terminate the array of overrides!
  31. };
  32. ```
  33. ## Intermediate Difficulty Examples :id=intermediate-difficulty-examples
  34. ### Media Controls & Screen Brightness :id=media-controls-amp-screen-brightness
  35. In this example a single key is configured to control media, volume and screen brightness by using key overrides.
  36. - The key is set to send `play/pause` in the keymap.
  37. The following key overrides will be configured:
  38. - `Ctrl` + `play/pause` will send `next track`.
  39. - `Ctrl` + `Shift` + `play/pause` will send `previous track`.
  40. - `Alt` + `play/pause` will send `volume up`.
  41. - `Alt` + `Shift` + `play/pause` will send `volume down`.
  42. - `Ctrl` + `Alt` + `play/pause` will send `brightness up`.
  43. - `Ctrl` + `Alt` + `Shift` + `play/pause` will send `brightness down`.
  44. ```c
  45. const key_override_t next_track_override =
  46. ko_make_with_layers_negmods_and_options(
  47. MOD_MASK_CTRL, // Trigger modifiers: ctrl
  48. KC_MPLY, // Trigger key: play/pause
  49. KC_MNXT, // Replacement key
  50. ~0, // Activate on all layers
  51. MOD_MASK_SA, // Do not activate when shift or alt are pressed
  52. ko_option_no_reregister_trigger); // Specifies that the play key is not registered again after lifting ctrl
  53. const key_override_t prev_track_override = ko_make_with_layers_negmods_and_options(MOD_MASK_CS, KC_MPLY,
  54. KC_MPRV, ~0, MOD_MASK_ALT, ko_option_no_reregister_trigger);
  55. const key_override_t vol_up_override = ko_make_with_layers_negmods_and_options(MOD_MASK_ALT, KC_MPLY,
  56. KC_VOLU, ~0, MOD_MASK_CS, ko_option_no_reregister_trigger);
  57. const key_override_t vol_down_override = ko_make_with_layers_negmods_and_options(MOD_MASK_SA, KC_MPLY,
  58. KC_VOLD, ~0, MOD_MASK_CTRL, ko_option_no_reregister_trigger);
  59. const key_override_t brightness_up_override = ko_make_with_layers_negmods_and_options(MOD_MASK_CA, KC_MPLY,
  60. KC_BRIU, ~0, MOD_MASK_SHIFT, ko_option_no_reregister_trigger);
  61. const key_override_t brightness_down_override = ko_make_basic(MOD_MASK_CSA, KC_MPLY, KC_BRID);
  62. // This globally defines all key overrides to be used
  63. const key_override_t **key_overrides = (const key_override_t *[]){
  64. &next_track_override,
  65. &prev_track_override,
  66. &vol_up_override,
  67. &vol_down_override,
  68. &brightness_up_override,
  69. &brightness_down_override,
  70. NULL
  71. };
  72. ```
  73. ### Flexible macOS-friendly Grave Escape :id=flexible-macos-friendly-grave-escape
  74. The [Grave Escape feature](feature_grave_esc.md) is limited in its configurability and has [bugs when used on macOS](feature_grave_esc.md#caveats). Key overrides can be used to achieve a similar functionality as Grave Escape, but with more customization and without bugs on macOS.
  75. ```c
  76. // Shift + esc = ~
  77. const key_override_t tilde_esc_override = ko_make_basic(MOD_MASK_SHIFT, KC_ESC, S(KC_GRV));
  78. // GUI + esc = `
  79. const key_override_t grave_esc_override = ko_make_basic(MOD_MASK_GUI, KC_ESC, KC_GRV);
  80. const key_override_t **key_overrides = (const key_override_t *[]){
  81. &tilde_esc_override,
  82. &grave_esc_override,
  83. NULL
  84. };
  85. ```
  86. In addition to not encountering unexpected bugs on macOS, you can also change the behavior as you wish. Instead setting `GUI` + `ESC` = `` ` `` you may change it to an arbitrary other modifier, for example `Ctrl` + `ESC` = `` ` ``.
  87. ## Advanced Examples :id=advanced-examples
  88. ### Modifiers as Layer Keys :id=modifiers-as-layer-keys
  89. Do you really need a dedicated key to toggle your fn layer? With key overrides, perhaps not. This example shows how you can configure to use `rGUI` + `rAlt` (right GUI and right alt) to access a momentary layer like an fn layer. With this you completely eliminate the need to use a dedicated layer key. Of course the choice of modifier keys can be changed as needed, `rGUI` + `rAlt` is just an example here.
  90. ```c
  91. // This is called when the override activates and deactivates. Enable the fn layer on activation and disable on deactivation
  92. bool momentary_layer(bool key_down, void *layer) {
  93. if (key_down) {
  94. layer_on((uint8_t)(uintptr_t)layer);
  95. } else {
  96. layer_off((uint8_t)(uintptr_t)layer);
  97. }
  98. return false;
  99. }
  100. const key_override_t fn_override = {.trigger_mods = MOD_BIT(KC_RGUI) | MOD_BIT(KC_RCTL), //
  101. .layers = ~(1 << LAYER_FN), //
  102. .suppressed_mods = MOD_BIT(KC_RGUI) | MOD_BIT(KC_RCTL), //
  103. .options = ko_option_no_unregister_on_other_key_down, //
  104. .negative_mod_mask = (uint8_t) ~(MOD_BIT(KC_RGUI) | MOD_BIT(KC_RCTL)), //
  105. .custom_action = momentary_layer, //
  106. .context = (void *)LAYER_FN, //
  107. .trigger = KC_NO, //
  108. .replacement = KC_NO, //
  109. .enabled = NULL};
  110. ```
  111. ## Keycodes :id=keycodes
  112. |Keycode |Aliases |Description |
  113. |------------------------|---------|----------------------|
  114. |`QK_KEY_OVERRIDE_TOGGLE`|`KO_TOGG`|Toggle key overrides |
  115. |`QK_KEY_OVERRIDE_ON` |`KO_ON` |Turn on key overrides |
  116. |`QK_KEY_OVERRIDE_OFF` |`KO_OFF` |Turn off key overrides|
  117. ## Reference for `key_override_t` :id=reference-for-key_override_t
  118. Advanced users may need more customization than what is offered by the simple `ko_make` initializers. For this, directly create a `key_override_t` value and set all members. Below is a reference for all members of `key_override_t`.
  119. | Member | Description |
  120. |--------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
  121. | `uint16_t trigger` | The non-modifier keycode that triggers the override. This keycode, and the necessary modifiers (`trigger_mods`) must be pressed to activate this override. Set this to the keycode of the key that should activate the override. Set to `KC_NO` to require only the necessary modifiers to be pressed and no non-modifier. |
  122. | `uint8_t trigger_mods` | Which mods need to be down for activation. If both sides of a modifier are set (e.g. left ctrl and right ctrl) then only one is required to be pressed (e.g. left ctrl suffices). Use the `MOD_MASK_XXX` and `MOD_BIT()` macros for this. |
  123. | `layer_state_t layers` | This is a BITMASK (!), defining which layers this override applies to. To use this override on layer i set the ith bit `(1 << i)`. |
  124. | `uint8_t negative_mod_mask` | Which modifiers cannot be down. It must hold that `(active_modifiers & negative_mod_mask) == 0`, otherwise the key override will not be activated. An active override will be deactivated once this is no longer true. |
  125. | `uint8_t suppressed_mods` | Modifiers to 'suppress' while the override is active. To suppress a modifier means that even though the modifier key is held down, the host OS sees the modifier as not pressed. Can be used to suppress the trigger modifiers, as a trivial example. |
  126. | `uint16_t replacement` | The complex keycode to send as replacement when this override is triggered. This can be a simple keycode, a key-modifier combination (e.g. `C(KC_A)`), or `KC_NO` (to register no replacement keycode). Use in combination with suppressed_mods to get the correct modifiers to be sent. |
  127. | `ko_option_t options` | Options controlling the behavior of the override, such as what actions are allowed to activate the override. |
  128. | `bool (*custom_action)(bool activated, void *context)` | If not NULL, this function will be called right before the replacement key is registered, along with the provided context and a flag indicating whether the override was activated or deactivated. This function allows you to run some custom actions for specific key overrides. If you return `false`, the replacement key is not registered/unregistered as it would normally. Return `true` to register and unregister the override normally. |
  129. | `void *context` | A context that will be passed to the custom action function. |
  130. | `bool *enabled` | If this points to false this override will not be used. Set to NULL to always have this override enabled. |
  131. ## Reference for `ko_option_t` :id=reference-for-ko_option_t
  132. Bitfield with various options controlling the behavior of a key override.
  133. | Value | Description |
  134. |------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
  135. | `ko_option_activation_trigger_down` | Allow activating when the trigger key is pressed down. |
  136. | `ko_option_activation_required_mod_down` | Allow activating when a necessary modifier is pressed down. |
  137. | `ko_option_activation_negative_mod_up` | Allow activating when a negative modifier is released. |
  138. | `ko_option_one_mod` | If set, any of the modifiers in `trigger_mods` will be enough to activate the override (logical OR of modifiers). If not set, all the modifiers in `trigger_mods` have to be pressed (logical AND of modifiers). |
  139. | `ko_option_no_unregister_on_other_key_down` | If set, the override will not deactivate when another key is pressed down. Use only if you really know you need this. |
  140. | `ko_option_no_reregister_trigger` | If set, the trigger key will never be registered again after the override is deactivated. |
  141. | `ko_options_default` | The default options used by the `ko_make_xxx` functions |
  142. ## For Advanced Users: Inner Workings :id=for-advanced-users-inner-workings
  143. This section explains how a key override works in detail, explaining where each member of `key_override_t` comes into play. Understanding this is essential to be able to take full advantage of all the options offered by key overrides.
  144. #### Activation :id=activation
  145. When the necessary keys are pressed (`trigger_mods` + `trigger`), the override is 'activated' and the replacement key is registered in the keyboard report (`replacement`), while the `trigger` key is removed from the keyboard report. The trigger modifiers may also be removed from the keyboard report upon activation of an override (`suppressed_mods`). The override will not activate if any of the `negative_modifiers` are pressed.
  146. Overrides can activate in three different cases:
  147. 1. The trigger key is pressed down and necessary modifiers are already down.
  148. 2. A necessary modifier is pressed down, while the trigger key and other necessary modifiers are already down.
  149. 3. A negative modifier is released, while all necessary modifiers and the trigger key are already down.
  150. Use the `option` member to customize which of these events are allowed to activate your overrides (default: all three).
  151. In any case, a key override can only activate if the `trigger` key is the _last_ non-modifier key that was pressed down. This emulates the behavior of how standard OSes (macOS, Windows, Linux) handle normal key input (to understand: Hold down `a`, then also hold down `b`, then hold down `shift`; `B` will be typed but not `A`).
  152. #### Deactivation :id=deactivation
  153. An override is 'deactivated' when one of the trigger keys (`trigger_mods`, `trigger`) is lifted, another non-modifier key is pressed down, or one of the `negative_modifiers` is pressed down. When an override deactivates, the `replacement` key is removed from the keyboard report, while the `suppressed_mods` that are still held down are re-added to the keyboard report. By default, the `trigger` key is re-added to the keyboard report if it is still held down and no other non-modifier key has been pressed since. This again emulates the behavior of how standard OSes handle normal key input (To understand: hold down `a`, then also hold down `b`, then also `shift`, then release `b`; `A` will not be typed even though you are holding the `a` and `shift` keys). Use the `option` field `ko_option_no_reregister_trigger` to prevent re-registering the trigger key in all cases.
  154. #### Key Repeat Delay :id=key-repeat-delay
  155. A third way in which standard OS-handling of modifier-key input is emulated in key overrides is with a ['key repeat delay'](https://www.dummies.com/computers/pcs/set-your-keyboards-repeat-delay-and-repeat-rate/). To explain what this is, let's look at how normal keyboard input is handled by mainstream OSes again: If you hold down `a`, followed by `shift`, you will see the letter `a` is first typed, then for a short moment nothing is typed and then repeating `A`s are typed. Take note that, although shift is pressed down just after `a` is pressed, it takes a moment until `A` is typed. This is caused by the aforementioned key repeat delay, and it is a feature that prevents unwanted repeated characters from being typed.
  156. This applies equally to releasing a modifier: When you hold `shift`, then press `a`, the letter `A` is typed. Now if you release `shift` first, followed by `a` shortly after, you will not see the letter `a` being typed, even though for a short moment of time you were just holding down the key `a`. This is because no modified characters are typed until the key repeat delay has passed.
  157. This exact behavior is implemented in key overrides as well: If a key override for `shift` + `a` = `b` exists, and `a` is pressed and held, followed by `shift`, you will not immediately see the letter `b` being typed. Instead, this event is deferred for a short moment, until the key repeat delay has passed, measured from the moment when the trigger key (`a`) was pressed down.
  158. The duration of the key repeat delay is controlled with the `KEY_OVERRIDE_REPEAT_DELAY` macro. Define this value in your `config.h` file to change it. It is 500ms by default.
  159. ## Difference to Combos :id=difference-to-combos
  160. Note that key overrides are very different from [combos](https://docs.qmk.fm/#/feature_combo). Combos require that you press down several keys almost _at the same time_ and can work with any combination of non-modifier keys. Key overrides work like keyboard shortcuts (e.g. `ctrl` + `z`): They take combinations of _multiple_ modifiers and _one_ non-modifier key to then perform some custom action. Key overrides are implemented with much care to behave just like normal keyboard shortcuts would in regards to the order of pressed keys, timing, and interaction with other pressed keys. There are a number of optional settings that can be used to really fine-tune the behavior of each key override as well. Using key overrides also does not delay key input for regular key presses, which inherently happens in combos and may be undesirable.
  161. ## Solution to the problem of flashing modifiers :id=neutralize-flashing-modifiers
  162. If the programs you use bind an action to taps of modifier keys (e.g. tapping left GUI to bring up the applications menu or tapping left Alt to focus the menu bar), you may find that using key overrides with suppressed mods falsely triggers those actions. To counteract this, you can define a `DUMMY_MOD_NEUTRALIZER_KEYCODE` in `config.h` that will get sent in between the register and unregister events of a suppressed modifier. That way, the programs on your computer will no longer interpret the mod suppression induced by key overrides as a lone tap of a modifier key and will thus not falsely trigger the undesired action.
  163. Naturally, for this technique to be effective, you must choose a `DUMMY_MOD_NEUTRALIZER_KEYCODE` for which no keyboard shortcuts are bound to. Recommended values are: `KC_RIGHT_CTRL` or `KC_F18`.
  164. Please note that `DUMMY_MOD_NEUTRALIZER_KEYCODE` must be a basic, unmodified, HID keycode so values like `KC_NO`, `KC_TRANSPARENT` or `KC_PIPE` aka `S(KC_BACKSLASH)` are not permitted.
  165. By default, only left Alt and left GUI are neutralized. If you want to change the list of applicable modifier masks, use the following in your `config.h`:
  166. ```c
  167. #define MODS_TO_NEUTRALIZE { <mod_mask_1>, <mod_mask_2>, ... }
  168. ```
  169. Examples:
  170. ```c
  171. #define DUMMY_MOD_NEUTRALIZER_KEYCODE KC_RIGHT_CTRL
  172. // Neutralize left alt and left GUI (Default value)
  173. #define MODS_TO_NEUTRALIZE { MOD_BIT(KC_LEFT_ALT), MOD_BIT(KC_LEFT_GUI) }
  174. // Neutralize left alt, left GUI, right GUI and left Control+Shift
  175. #define MODS_TO_NEUTRALIZE { MOD_BIT(KC_LEFT_ALT), MOD_BIT(KC_LEFT_GUI), MOD_BIT(KC_RIGHT_GUI), MOD_BIT(KC_LEFT_CTRL)|MOD_BIT(KC_LEFT_SHIFT) }
  176. ```
  177. !> Do not use `MOD_xxx` constants like `MOD_LSFT` or `MOD_RALT`, since they're 5-bit packed bit-arrays while `MODS_TO_NEUTRALIZE` expects a list of 8-bit packed bit-arrays. Use `MOD_BIT(<kc>)` or `MOD_MASK_xxx` instead.