feature_macros.md (14166B)
1 # Macros 2 3 Macros allow you to send multiple keystrokes when pressing just one key. QMK has a number of ways to define and use macros. These can do anything you want: type common phrases for you, copypasta, repetitive game movements, or even help you code. 4 5 ::: warning 6 **Security Note**: While it is possible to use macros to send passwords, credit card numbers, and other sensitive information it is a supremely bad idea to do so. Anyone who gets a hold of your keyboard will be able to access that information by opening a text editor. 7 ::: 8 9 ## Using Macros In JSON Keymaps 10 11 You can define up to 32 macros in a `keymap.json` file, as used by [Configurator](newbs_building_firmware_configurator), and `qmk compile`. You can define these macros in a list under the `macros` keyword, like this: 12 13 ```json 14 { 15 "keyboard": "handwired/my_macropad", 16 "keymap": "my_keymap", 17 "macros": [ 18 [ 19 {"action":"down", "keycodes": ["LSFT"]}, 20 "hello world1", 21 {"action": "up","keycodes": ["LSFT"]} 22 ], 23 [ 24 {"action":"tap", "keycodes": ["LCTL", "LALT", "DEL"]} 25 ], 26 [ 27 "ding!", 28 {"action":"beep"} 29 ], 30 [ 31 {"action":"tap", "keycodes": ["F1"]}, 32 {"action":"delay", "duration": 1000}, 33 {"action":"tap", "keycodes": ["PGDN"]} 34 ] 35 ], 36 "layout": "LAYOUT_all", 37 "layers": [ 38 ["QK_MACRO_0", "QK_MACRO_1", "QK_MACRO_2", "QK_MACRO_3"] 39 ] 40 } 41 ``` 42 43 ### Selecting Your Host Keyboard Layout 44 45 If you type in a language other than English, or use a non-QWERTY layout like Colemak, Dvorak, or Workman, you may have set your computer's input language to match this layout. This presents a challenge when creating macros — you may need to type different keys to get the same letters! To address this you can use [language-specific keycodes](reference_keymap_extras). 46 47 ### Macro Basics 48 49 Each macro is an array consisting of strings and objects (dictionaries). Strings are typed to your computer while objects allow you to control how your macro is typed out. 50 51 #### Object Format 52 53 All objects have one required key: `action`. This tells QMK what the object does. There are currently 5 actions: beep, delay, down, tap, up 54 55 Only basic keycodes (prefixed by `KC_`) are supported. Do not include the `KC_` prefix when listing keycodes. 56 57 * `beep` 58 * Play a bell if the keyboard has [audio enabled](features/audio). 59 * Example: `{"action": "beep"}` 60 * `delay` 61 * Pause macro playback. Duration is specified in milliseconds (ms). 62 * Example: `{"action": "delay", "duration": 500}` 63 * `down` 64 * Send a key down event for one or more keycodes. 65 * Example, single key: `{"action":"down", "keycodes": ["LSFT"]}` 66 * Example, multiple keys: `{"action":"down", "keycodes": ["CTRL", "LSFT"]}` 67 * `tap` 68 * Type a chord, which sends a down event for each key followed by an up event for each key. 69 * Example, single key: `{"action":"tap", "keycodes": ["F13"]}` 70 * Example, multiple keys: `{"action":"tap", "keycodes": ["CTRL", "LALT", "DEL"]}` 71 * `up` 72 * Send a key up event for one or more keycodes. 73 * Example, single key: `{"action":"up", "keycodes": ["LSFT"]}` 74 * Example, multiple keys: `{"action":"up", "keycodes": ["CTRL", "LSFT"]}` 75 76 ## Using Macros in C Keymaps 77 78 ### `SEND_STRING()` & `process_record_user` 79 80 See also: [Send String](features/send_string) 81 82 Sometimes you want a key to type out words or phrases. For the most common situations, we've provided `SEND_STRING()`, which will type out a string (i.e. a sequence of characters) for you. All ASCII characters that are easily translatable to a keycode are supported (e.g. `qmk 123\n\t`). 83 84 Here is an example `keymap.c` for a two-key keyboard: 85 86 ```c 87 enum custom_keycodes { 88 QMKBEST = SAFE_RANGE, 89 }; 90 91 bool process_record_user(uint16_t keycode, keyrecord_t *record) { 92 switch (keycode) { 93 case QMKBEST: 94 if (record->event.pressed) { 95 // when keycode QMKBEST is pressed 96 SEND_STRING("QMK is the best thing ever!"); 97 } else { 98 // when keycode QMKBEST is released 99 } 100 break; 101 } 102 return true; 103 }; 104 105 const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { 106 [0] = { 107 {QMKBEST, KC_ESC}, 108 // ... 109 }, 110 }; 111 ``` 112 113 What happens here is this: 114 We first define a new custom keycode in the range not occupied by any other keycodes. 115 Then we use the `process_record_user` function, which is called whenever a key is pressed or released, to check if our custom keycode has been activated. 116 If yes, we send the string `"QMK is the best thing ever!"` to the computer via the `SEND_STRING` macro (this is a C preprocessor macro, not to be confused with QMK macros). 117 We return `true` to indicate to the caller that the key press we just processed should continue to be processed as normal (as we didn't replace or alter the functionality). 118 Finally, we define the keymap so that the first button activates our macro and the second button is just an escape button. 119 120 ::: tip 121 It is recommended to use the SAFE_RANGE macro as per [Customizing Functionality](custom_quantum_functions). 122 ::: 123 124 You might want to add more than one macro. 125 You can do that by adding another keycode and adding another case to the switch statement, like so: 126 127 ```c 128 enum custom_keycodes { 129 QMKBEST = SAFE_RANGE, 130 QMKURL, 131 MY_OTHER_MACRO, 132 }; 133 134 bool process_record_user(uint16_t keycode, keyrecord_t *record) { 135 switch (keycode) { 136 case QMKBEST: 137 if (record->event.pressed) { 138 // when keycode QMKBEST is pressed 139 SEND_STRING("QMK is the best thing ever!"); 140 } else { 141 // when keycode QMKBEST is released 142 } 143 break; 144 145 case QMKURL: 146 if (record->event.pressed) { 147 // when keycode QMKURL is pressed 148 SEND_STRING("https://qmk.fm/\n"); 149 } else { 150 // when keycode QMKURL is released 151 } 152 break; 153 154 case MY_OTHER_MACRO: 155 if (record->event.pressed) { 156 SEND_STRING(SS_LCTL("ac")); // selects all and copies 157 } 158 break; 159 } 160 return true; 161 }; 162 163 const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { 164 [0] = { 165 {MY_CUSTOM_MACRO, MY_OTHER_MACRO}, 166 // ... 167 }, 168 }; 169 ``` 170 171 ::: tip 172 An enumerated list of custom keycodes (`enum custom_keycodes`) must be declared before `keymaps[]` array, `process_record_user()` and any other function that use the list for the compiler to recognise it. 173 ::: 174 175 #### Advanced Macros 176 177 In addition to the `process_record_user()` function, is the `post_process_record_user()` function. This runs after `process_record` and can be used to do things after a keystroke has been sent. This is useful if you want to have a key pressed before and released after a normal key, for instance. 178 179 In this example, we modify most normal keypresses so that `F22` is pressed before the keystroke is normally sent, and release it __only after__ it's been released. 180 181 ```c 182 static uint8_t f22_tracker; 183 184 bool process_record_user(uint16_t keycode, keyrecord_t *record) { 185 switch (keycode) { 186 case KC_A ... KC_F21: //notice how it skips over F22 187 case KC_F23 ... KC_EXSEL: //exsel is the last one before the modifier keys 188 if (record->event.pressed) { 189 register_code(KC_F22); //this means to send F22 down 190 f22_tracker++; 191 register_code(keycode); 192 return false; 193 } 194 break; 195 } 196 return true; 197 } 198 199 void post_process_record_user(uint16_t keycode, keyrecord_t *record) { 200 switch (keycode) { 201 case KC_A ... KC_F21: //notice how it skips over F22 202 case KC_F23 ... KC_EXSL: //exsel is the last one before the modifier keys 203 if (!record->event.pressed) { 204 f22_tracker--; 205 if (!f22_tracker) { 206 unregister_code(KC_F22); //this means to send F22 up 207 } 208 } 209 break; 210 } 211 } 212 ``` 213 214 215 #### TAP, DOWN and UP 216 217 You may want to use keys in your macros that you can't write down, such as `Ctrl` or `Home`. 218 You can send arbitrary keycodes by wrapping them in: 219 220 * `SS_TAP()` presses and releases a key. 221 * `SS_DOWN()` presses (but does not release) a key. 222 * `SS_UP()` releases a key. 223 224 For example: 225 226 ```c 227 SEND_STRING(SS_TAP(X_HOME)); 228 ``` 229 230 Would tap `KC_HOME` - note how the prefix is now `X_`, and not `KC_`. You can also combine this with other strings, like this: 231 232 ```c 233 SEND_STRING("VE"SS_TAP(X_HOME)"LO"); 234 ``` 235 236 Which would send "VE" followed by a `KC_HOME` tap, and "LO" (spelling "LOVE" if on a newline). 237 238 Delays can be also added to the string: 239 240 * `SS_DELAY(msecs)` will delay for the specified number of milliseconds. 241 242 For example: 243 244 ```c 245 SEND_STRING("VE" SS_DELAY(1000) SS_TAP(X_HOME) "LO"); 246 ``` 247 248 Which would send "VE" followed by a 1-second delay, then a `KC_HOME` tap, and "LO" (spelling "LOVE" if on a newline, but delayed in the middle). 249 250 There's also a couple of mod shortcuts you can use: 251 252 * `SS_LCTL(string)` 253 * `SS_LSFT(string)` 254 * `SS_LALT(string)` or `SS_LOPT(string)` 255 * `SS_LGUI(string)`, `SS_LCMD(string)` or `SS_LWIN(string)` 256 * `SS_RCTL(string)` 257 * `SS_RSFT(string)` 258 * `SS_RALT(string)`, `SS_ROPT(string)` or `SS_ALGR(string)` 259 * `SS_RGUI(string)`, `SS_RCMD(string)` or `SS_RWIN(string)` 260 261 These press the respective modifier, send the supplied string and then release the modifier. 262 They can be used like this: 263 264 ```c 265 SEND_STRING(SS_LCTL("a")); 266 ``` 267 268 Which would send Left Control+`a` (Left Control down, `a`, Left Control up) - notice that they take strings (eg `"k"`), and not the `X_K` keycodes. 269 270 #### Alternative Keymaps 271 272 By default, it assumes a US keymap with a QWERTY layout; if you want to change that (e.g. if your OS uses software Colemak), include this somewhere in your keymap: 273 274 ```c 275 #include "sendstring_colemak.h" 276 ``` 277 278 #### Strings in Memory 279 280 If for some reason you're manipulating strings and need to print out something you just generated (instead of being a literal, constant string), you can use `send_string()`, like this: 281 282 ```c 283 char my_str[4] = "ok."; 284 send_string(my_str); 285 ``` 286 287 The shortcuts defined above won't work with `send_string()`, but you can separate things out to different lines if needed: 288 289 ```c 290 char my_str[4] = "ok."; 291 SEND_STRING("I said: "); 292 send_string(my_str); 293 SEND_STRING(".."SS_TAP(X_END)); 294 ``` 295 296 297 ### Advanced Macro Functions 298 299 There are some functions you may find useful in macro-writing. Keep in mind that while you can write some fairly advanced code within a macro, if your functionality gets too complex you may want to define a custom keycode instead. Macros are meant to be simple. 300 301 ::: tip 302 You can also use the functions described in [Useful function](ref_functions) and [Checking modifier state](feature_advanced_keycodes#checking-modifier-state) for additional functionality. For example, `reset_keyboard()` allows you to reset the keyboard as part of a macro and `get_mods() & MOD_MASK_SHIFT` lets you check for the existence of active shift modifiers. 303 ::: 304 305 #### `record->event.pressed` 306 307 This is a boolean value that can be tested to see if the switch is being pressed or released. An example of this is 308 309 ```c 310 if (record->event.pressed) { 311 // on keydown 312 } else { 313 // on keyup 314 } 315 ``` 316 317 #### `register_code(<kc>);` 318 319 This sends the `<kc>` keydown event to the computer. Some examples would be `KC_ESC`, `KC_C`, `KC_4`, and even modifiers such as `KC_LSFT` and `KC_LGUI`. 320 321 #### `unregister_code(<kc>);` 322 323 Parallel to `register_code` function, this sends the `<kc>` keyup event to the computer. If you don't use this, the key will be held down until it's sent. 324 325 #### `tap_code(<kc>);` 326 327 Sends `register_code(<kc>)` and then `unregister_code(<kc>)`. This is useful if you want to send both the press and release events ("tap" the key, rather than hold it). 328 329 If `TAP_CODE_DELAY` is defined (default 0), this function waits that many milliseconds before calling `unregister_code(<kc>)`. This can be useful when you are having issues with taps (un)registering. 330 331 If the keycode is `KC_CAPS`, it waits `TAP_HOLD_CAPS_DELAY` milliseconds instead (default 80), as macOS prevents accidental Caps Lock activation by waiting for the key to be held for a certain amount of time. 332 333 #### `tap_code_delay(<kc>, <delay>);` 334 335 Like `tap_code(<kc>)`, but with a `delay` parameter for specifying arbitrary intervals before sending the unregister event. 336 337 #### `register_code16(<kc>);`, `unregister_code16(<kc>);`, `tap_code16(<kc>);` and `tap_code16_delay(<kc>, <delay>);` 338 339 These functions work similar to their regular counterparts, but allow you to use modded keycodes (with Shift, Alt, Control, and/or GUI applied to them). 340 341 Eg, you could use `register_code16(S(KC_5));` instead of registering the mod, then registering the keycode. 342 343 #### `clear_keyboard();` 344 345 This will clear all mods and keys currently pressed. 346 347 #### `clear_mods();` 348 349 This will clear all mods currently pressed. 350 351 #### `clear_keyboard_but_mods();` 352 353 This will clear all keys besides the mods currently pressed. 354 355 ### Advanced Example: 356 357 #### Super ALT↯TAB 358 359 This macro will register `KC_LALT` and tap `KC_TAB`, then wait for 1000ms. If the key is tapped again, it will send another `KC_TAB`; if there is no tap, `KC_LALT` will be unregistered, thus allowing you to cycle through windows. 360 361 ```c 362 bool is_alt_tab_active = false; // ADD this near the beginning of keymap.c 363 uint16_t alt_tab_timer = 0; // we will be using them soon. 364 365 enum custom_keycodes { // Make sure have the awesome keycode ready 366 ALT_TAB = SAFE_RANGE, 367 }; 368 369 bool process_record_user(uint16_t keycode, keyrecord_t *record) { 370 switch (keycode) { // This will do most of the grunt work with the keycodes. 371 case ALT_TAB: 372 if (record->event.pressed) { 373 if (!is_alt_tab_active) { 374 is_alt_tab_active = true; 375 register_code(KC_LALT); 376 } 377 alt_tab_timer = timer_read(); 378 register_code(KC_TAB); 379 } else { 380 unregister_code(KC_TAB); 381 } 382 break; 383 } 384 return true; 385 } 386 387 void matrix_scan_user(void) { // The very important timer. 388 if (is_alt_tab_active) { 389 if (timer_elapsed(alt_tab_timer) > 1000) { 390 unregister_code(KC_LALT); 391 is_alt_tab_active = false; 392 } 393 } 394 } 395 ```