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.

395 lines
17 KiB

  1. # Combos
  2. The Combo feature is a chording type solution for adding custom actions. It lets you hit multiple keys at once and produce a different effect. For instance, hitting `A` and `B` within the combo term would hit `ESC` instead, or have it perform even more complex tasks.
  3. To enable this feature, you need to add `COMBO_ENABLE = yes` to your `rules.mk`.
  4. Then, in your `keymap.c` file, you'll need to define a sequence of keys, terminated with `COMBO_END`, and a structure to list the combination of keys, and its resulting action.
  5. ```c
  6. const uint16_t PROGMEM test_combo1[] = {KC_A, KC_B, COMBO_END};
  7. const uint16_t PROGMEM test_combo2[] = {KC_C, KC_D, COMBO_END};
  8. combo_t key_combos[] = {
  9. COMBO(test_combo1, KC_ESC),
  10. COMBO(test_combo2, LCTL(KC_Z)), // keycodes with modifiers are possible too!
  11. };
  12. ```
  13. This will send "Escape" if you hit the A and B keys, and Ctrl+Z when you hit the C and D keys.
  14. ## Mod-Tap Support
  15. [Mod-Tap](mod_tap.md) feature is also supported together with combos. You will need to use the full Mod-Tap keycode in the combo definition, e.g.:
  16. ```c
  17. const uint16_t PROGMEM test_combo1[] = {LSFT_T(KC_A), LT(1, KC_B), COMBO_END};
  18. ```
  19. ## Overlapping Combos
  20. It is possible to overlap combos. Before, with the example below both combos would activate when all three keys were pressed. Now only the three key combo will activate.
  21. ```c
  22. const uint16_t PROGMEM test_combo1[] = {LSFT_T(KC_A), LT(1, KC_B), COMBO_END};
  23. const uint16_t PROGMEM test_combo2[] = {LSFT_T(KC_A), LT(1, KC_B), KC_C, COMBO_END};
  24. combo_t key_combos[] = {
  25. COMBO(test_combo1, KC_ESC)
  26. COMBO(test_combo2, KC_TAB)
  27. };
  28. ```
  29. ## Examples
  30. A long list of combos can be defined in an `enum` list:
  31. ```c
  32. enum combos {
  33. AB_ESC,
  34. JK_TAB,
  35. QW_SFT,
  36. SD_LAYER
  37. };
  38. const uint16_t PROGMEM ab_combo[] = {KC_A, KC_B, COMBO_END};
  39. const uint16_t PROGMEM jk_combo[] = {KC_J, KC_K, COMBO_END};
  40. const uint16_t PROGMEM qw_combo[] = {KC_Q, KC_W, COMBO_END};
  41. const uint16_t PROGMEM sd_combo[] = {KC_S, KC_D, COMBO_END};
  42. combo_t key_combos[] = {
  43. [AB_ESC] = COMBO(ab_combo, KC_ESC),
  44. [JK_TAB] = COMBO(jk_combo, KC_TAB),
  45. [QW_SFT] = COMBO(qw_combo, KC_LSFT),
  46. [SD_LAYER] = COMBO(sd_combo, MO(_LAYER)),
  47. };
  48. ```
  49. For a more complicated implementation, you can use the `process_combo_event` function to add custom handling.
  50. ```c
  51. enum combo_events {
  52. EM_EMAIL,
  53. BSPC_LSFT_CLEAR,
  54. };
  55. const uint16_t PROGMEM email_combo[] = {KC_E, KC_M, COMBO_END};
  56. const uint16_t PROGMEM clear_line_combo[] = {KC_BSPC, KC_LSFT, COMBO_END};
  57. combo_t key_combos[] = {
  58. [EM_EMAIL] = COMBO_ACTION(email_combo),
  59. [BSPC_LSFT_CLEAR] = COMBO_ACTION(clear_line_combo),
  60. };
  61. /* COMBO_ACTION(x) is same as COMBO(x, KC_NO) */
  62. void process_combo_event(uint16_t combo_index, bool pressed) {
  63. switch(combo_index) {
  64. case EM_EMAIL:
  65. if (pressed) {
  66. SEND_STRING("john.doe@example.com");
  67. }
  68. break;
  69. case BSPC_LSFT_CLEAR:
  70. if (pressed) {
  71. tap_code16(KC_END);
  72. tap_code16(S(KC_HOME));
  73. tap_code16(KC_BSPC);
  74. }
  75. break;
  76. }
  77. }
  78. ```
  79. This will send "john.doe@example.com" if you chord E and M together, and clear the current line with Backspace and Left-Shift. You could change this to do stuff like play sounds or change settings.
  80. It is worth noting that `COMBO_ACTION`s are not needed anymore. As of [PR#8591](https://github.com/qmk/qmk_firmware/pull/8591/), it is possible to run your own custom keycodes from combos. Just define the custom keycode, program its functionality in `process_record_user`, and define a combo with `COMBO(<key_array>, <your_custom_keycode>)`. See the first example in [Macros](feature_macros.md).
  81. ## Keycodes
  82. You can enable, disable and toggle the Combo feature on the fly. This is useful if you need to disable them temporarily, such as for a game. The following keycodes are available for use in your `keymap.c`
  83. |Keycode |Aliases |Description |
  84. |-----------------|---------|--------------------------------|
  85. |`QK_COMBO_ON` |`CM_ON` |Turns on Combo feature |
  86. |`QK_COMBO_OFF` |`CM_OFF` |Turns off Combo feature |
  87. |`QK_COMBO_TOGGLE`|`CM_TOGG`|Toggles Combo feature on and off|
  88. ## Advanced Configuration
  89. These configuration settings can be set in your `config.h` file.
  90. ### Combo Term
  91. By default, the timeout for the Combos to be recognized is set to 50ms. This can be changed if accidental combo misfires are happening or if you're having difficulties pressing keys at the same time. For instance, `#define COMBO_TERM 40` would set the timeout period for combos to 40ms.
  92. ### Buffer and state sizes
  93. If you're using long combos, or you have a lot of overlapping combos, you may run into issues with this, as the buffers may not be large enough to accommodate what you're doing. In this case, you can configure the sizes of the buffers used. Be aware, larger combo sizes and larger buffers will increase memory usage!
  94. To configure the amount of keys a combo can be composed of, change the following:
  95. | Keys | Define to be set |
  96. |------|-----------------------------------|
  97. | 6 | `#define EXTRA_SHORT_COMBOS` |
  98. | 8 | QMK Default |
  99. | 16 | `#define EXTRA_LONG_COMBOS` |
  100. | 32 | `#define EXTRA_EXTRA_LONG_COMBOS` |
  101. Defining `EXTRA_SHORT_COMBOS` combines a combo's internal state into just one byte. This can, in some cases, save some memory. If it doesn't, no point using it. If you do, you also have to make sure you don't define combos with more than 6 keys.
  102. Processing combos has two buffers, one for the key presses, another for the combos being activated. Use the following options to configure the sizes of these buffers:
  103. | Define | Default |
  104. |-------------------------------------|------------------------------------------------------|
  105. | `#define COMBO_KEY_BUFFER_LENGTH 8` | 8 (the key amount `(EXTRA_)EXTRA_LONG_COMBOS` gives) |
  106. | `#define COMBO_BUFFER_LENGTH 4` | 4 |
  107. ### Modifier Combos
  108. If a combo resolves to a Modifier, the window for processing the combo can be extended independently from normal combos. By default, this is disabled but can be enabled with `#define COMBO_MUST_HOLD_MODS`, and the time window can be configured with `#define COMBO_HOLD_TERM 150` (default: `TAPPING_TERM`). With `COMBO_MUST_HOLD_MODS`, you cannot tap the combo any more which makes the combo less prone to misfires.
  109. ### Strict key press order
  110. By defining `COMBO_MUST_PRESS_IN_ORDER` combos only activate when the keys are pressed in the same order as they are defined in the key array.
  111. ### Per Combo Timing, Holding, Tapping and Key Press Order
  112. For each combo, it is possible to configure the time window it has to pressed in, if it needs to be held down, if it needs to be tapped, or if its keys need to be pressed in order.
  113. For example, tap-only combos are useful if any (or all) of the underlying keys are mod-tap or layer-tap keys. When you tap the combo, you get the combo result. When you press the combo and hold it down, the combo doesn't activate. Instead the keys are processed separately as if the combo wasn't even there.
  114. In order to use these features, the following configuration options and functions need to be defined. Coming up with useful timings and configuration is left as an exercise for the reader.
  115. | Config Flag | Function | Description |
  116. |-----------------------------|-----------------------------------------------------------|--------------------------------------------------------------------------------------------------------|
  117. | `COMBO_TERM_PER_COMBO` | uint16_t get_combo_term(uint16_t index, combo_t \*combo) | Optional per-combo timeout window. (default: `COMBO_TERM`) |
  118. | `COMBO_MUST_HOLD_PER_COMBO` | bool get_combo_must_hold(uint16_t index, combo_t \*combo) | Controls if a given combo should fire immediately on tap or if it needs to be held. (default: `false`) |
  119. | `COMBO_MUST_TAP_PER_COMBO` | bool get_combo_must_tap(uint16_t index, combo_t \*combo) | Controls if a given combo should fire only if tapped within `COMBO_HOLD_TERM`. (default: `false`) |
  120. | `COMBO_MUST_PRESS_IN_ORDER_PER_COMBO` | bool get_combo_must_press_in_order(uint16_t index, combo_t \*combo) | Controls if a given combo should fire only if its keys are pressed in order. (default: `true`) |
  121. Examples:
  122. ```c
  123. uint16_t get_combo_term(uint16_t index, combo_t *combo) {
  124. // decide by combo->keycode
  125. switch (combo->keycode) {
  126. case KC_X:
  127. return 50;
  128. }
  129. // or with combo index, i.e. its name from enum.
  130. switch (index) {
  131. case COMBO_NAME_HERE:
  132. return 9001;
  133. }
  134. // And if you're feeling adventurous, you can even decide by the keys in the chord,
  135. // i.e. the exact array of keys you defined for the combo.
  136. // This can be useful if your combos have a common key and you want to apply the
  137. // same combo term for all of them.
  138. if (combo->keys[0] == KC_ENT) { // if first key in the array is Enter
  139. return 150;
  140. }
  141. return COMBO_TERM;
  142. }
  143. bool get_combo_must_hold(uint16_t index, combo_t *combo) {
  144. // Same as above, decide by keycode, the combo index, or by the keys in the chord.
  145. if (KEYCODE_IS_MOD(combo->keycode) ||
  146. (combo->keycode >= QK_MOMENTARY && combo->keycode <= QK_MOMENTARY_MAX) // MO(kc) keycodes
  147. ) {
  148. return true;
  149. }
  150. switch (index) {
  151. case COMBO_NAME_HERE:
  152. return true;
  153. }
  154. return false;
  155. }
  156. bool get_combo_must_tap(uint16_t index, combo_t *combo) {
  157. // If you want all combos to be tap-only, just uncomment the next line
  158. // return true
  159. // If you want *all* combos, that have Mod-Tap/Layer-Tap/Momentary keys in its chord, to be tap-only, this is for you:
  160. uint16_t key;
  161. uint8_t idx = 0;
  162. while ((key = pgm_read_word(&combo->keys[idx])) != COMBO_END) {
  163. switch (key) {
  164. case QK_MOD_TAP...QK_MOD_TAP_MAX:
  165. case QK_LAYER_TAP...QK_LAYER_TAP_MAX:
  166. case QK_MOMENTARY...QK_MOMENTARY_MAX:
  167. return true;
  168. }
  169. idx += 1;
  170. }
  171. return false;
  172. }
  173. bool get_combo_must_press_in_order(uint16_t combo_index, combo_t *combo) {
  174. switch (combo_index) {
  175. /* List combos here that you want to only activate if their keys
  176. * are pressed in the same order as they are defined in the combo's key
  177. * array. */
  178. case COMBO_NAME_HERE:
  179. return true;
  180. default:
  181. return false;
  182. }
  183. }
  184. ```
  185. ### Generic hook to (dis)allow a combo activation
  186. By defining `COMBO_SHOULD_TRIGGER` and its companying function `bool combo_should_trigger(uint16_t combo_index, combo_t *combo, uint16_t keycode, keyrecord_t *record)` you can block or allow combos to activate on the conditions of your choice.
  187. For example, you could disallow some combos on the base layer and allow them on another. Or disable combos on the home row when a timer is running.
  188. Examples:
  189. ```c
  190. bool combo_should_trigger(uint16_t combo_index, combo_t *combo, uint16_t keycode, keyrecord_t *record) {
  191. /* Disable combo `SOME_COMBO` on layer `_LAYER_A` */
  192. switch (combo_index) {
  193. case SOME_COMBO:
  194. if (layer_state_is(_LAYER_A)) {
  195. return false;
  196. }
  197. }
  198. return true;
  199. }
  200. ```
  201. ### Combo timer
  202. Normally, the timer is started on the first key press and then reset on every subsequent key press within the `COMBO_TERM`.
  203. Inputting combos is relaxed like this, but also slightly more prone to accidental misfires.
  204. The next two options alter the behaviour of the timer.
  205. #### `#define COMBO_STRICT_TIMER`
  206. With `COMBO_STRICT_TIMER`, the timer is started only on the first key press.
  207. Inputting combos is now less relaxed; you need to make sure the full chord is pressed within the `COMBO_TERM`.
  208. Misfires are less common but if you type multiple combos fast, there is a
  209. chance that the latter ones might not activate properly.
  210. #### `#define COMBO_NO_TIMER`
  211. By defining `COMBO_NO_TIMER`, the timer is disabled completely and combos are activated on the first key release.
  212. This also disables the "must hold" functionalities as they just wouldn't work at all.
  213. ### Customizable key releases
  214. By defining `COMBO_PROCESS_KEY_RELEASE` and implementing the function `bool process_combo_key_release(uint16_t combo_index, combo_t *combo, uint8_t key_index, uint16_t keycode)`, you can run your custom code on each key release after a combo was activated. For example you could change the RGB colors, activate haptics, or alter the modifiers.
  215. You can also release a combo early by returning `true` from the function.
  216. Here's an example where a combo resolves to two modifiers, and on key releases the modifiers are unregistered one by one, depending on which key was released.
  217. ```c
  218. enum combos {
  219. AB_MODS
  220. };
  221. const uint16_t PROGMEM ab_combo[] = {KC_A, KC_B, COMBO_END};
  222. combo_t key_combos[] = {
  223. [AB_MODS] = COMBO(ab_combo, LCTL(KC_LSFT)),
  224. };
  225. bool process_combo_key_release(uint16_t combo_index, combo_t *combo, uint8_t key_index, uint16_t keycode) {
  226. switch (combo_index) {
  227. case AB_MODS:
  228. switch(keycode) {
  229. case KC_A:
  230. unregister_mods(MOD_MASK_CTRL);
  231. break;
  232. case KC_B:
  233. unregister_mods(MOD_MASK_SHIFT);
  234. break;
  235. }
  236. return false; // do not release combo
  237. }
  238. return false;
  239. }
  240. ```
  241. ### Layer independent combos
  242. If you, for example, use multiple base layers for different key layouts, one for QWERTY, and another one for Colemak, you might want your combos to work from the same key positions on all layers. Defining the same combos again for another layout is redundant and takes more memory. The solution is to just check the keycodes from one layer.
  243. With `#define COMBO_ONLY_FROM_LAYER 0` in config.h, the combos' keys are always checked from layer `0`, even if other layers are active.
  244. #### Combo reference layers by layer.
  245. If not using `COMBO_ONLY_FROM_LAYER` it is possible to specify a
  246. combo reference layer for any layer using the `combo_ref_from_layer` hook.
  247. The combo macros automatically create this function from the `COMBO_REF_LAYER()`
  248. entries given.
  249. This function returns the assigned reference layer for the current layer.
  250. if there is no match, it returns the default reference layer if set,
  251. or the current layer otherwise. A default layer can be set with
  252. `DEFAULT_REF_LAYER(_MY_COMBO_REF_LAYER)`
  253. If not set, the default reference layer selection from the automatically generated
  254. `combo-ref-from-layer()` will be the current layer.
  255. The following `combo_ref_from_layer` function
  256. will give a reference layer of _QWERTY for the _DVORAK layer and
  257. will give the _NAV layer as a reference to it's self. All other layers
  258. will have the default for their combo reference layer. If the default
  259. is not set, all other layers will reference themselves.
  260. ```c
  261. #define COMBO_REF_DEFAULT _MY_COMBO_LAYER
  262. uint8_t combo_ref_from_layer(uint8_t layer){
  263. switch (get_highest_layer(layer_state)){
  264. case _DVORAK: return _QWERTY;
  265. case _NAV: return _NAV;
  266. default: return _MY_COMBO_LAYER;
  267. }
  268. return layer; // important if default is not in case.
  269. }
  270. ```
  271. The equivalent definition using the combo macros is this:
  272. ```c
  273. COMBO_REF_LAYER(_DVORAK, _QWERTY)
  274. COMBO_REF_LAYER(_NAV, _NAV)
  275. DEFAULT_REF_LAYER(_MY_COMBO_LAYER).
  276. ```
  277. ## User callbacks
  278. In addition to the keycodes, there are a few functions that you can use to set the status, or check it:
  279. |Function |Description |
  280. |-----------|--------------------------------------------------------------------|
  281. | `combo_enable()` | Enables the combo feature |
  282. | `combo_disable()` | Disables the combo feature, and clears the combo buffer |
  283. | `combo_toggle()` | Toggles the state of the combo feature |
  284. | `is_combo_enabled()` | Returns the status of the combo feature state (true or false) |
  285. ## Dictionary Management
  286. Having 3 places to update when adding new combos or altering old ones does become cumbersome when you have a lot of combos. We can alleviate this with some magic! ... If you consider C macros magic.
  287. First, you need to add `VPATH += keyboards/gboards` to your `rules.mk`. Next, include the file `g/keymap_combo.h` in your `keymap.c`.
  288. !> This functionality uses the same `process_combo_event` function as `COMBO_ACTION` macros do, so you cannot use the function yourself in your keymap. Instead, you have to define the `case`s of the `switch` statement by themselves within `inject.h`, which `g/keymap_combo.h` will then include into the function.
  289. Then, write your combos in `combos.def` file in the following manner:
  290. ```c
  291. // Alternate reference layers by layer
  292. // Layer Reference layer
  293. COMBO_REF_LAYER(_DVORAK, _QWERTY) // reference the qwerty layer for dvorak.
  294. COMBO_REF_LAYER(_NAV, _NAV) // explicit reference to self instead of the default.
  295. // name result chord keys
  296. COMB(AB_ESC, KC_ESC, KC_A, KC_B)
  297. COMB(JK_TAB, KC_TAB, KC_J, KC_K)
  298. COMB(JKL_SPC, KC_SPC, KC_J, KC_K, KC_L)
  299. COMB(BSSL_CLR, KC_NO, KC_BSPC, KC_LSFT) // using KC_NO as the resulting keycode is the same as COMBO_ACTION before.
  300. COMB(QW_UNDO, C(KC_Z), KC_Q, KC_W)
  301. SUBS(TH_THE, "the", KC_T, KC_H) // SUBS uses SEND_STRING to output the given string.
  302. ...
  303. ```
  304. For small to huge ready made dictionaries of combos, you can check out http://combos.gboards.ca/.