diff options
Diffstat (limited to 'docs/feature_macros.md')
-rw-r--r-- | docs/feature_macros.md | 134 |
1 files changed, 117 insertions, 17 deletions
diff --git a/docs/feature_macros.md b/docs/feature_macros.md index 6807111ca..81ade5859 100644 --- a/docs/feature_macros.md +++ b/docs/feature_macros.md | |||
@@ -4,7 +4,107 @@ Macros allow you to send multiple keystrokes when pressing just one key. QMK has | |||
4 | 4 | ||
5 | !> **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. | 5 | !> **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. |
6 | 6 | ||
7 | ## `SEND_STRING()` & `process_record_user` | 7 | ## Using Macros In JSON Keymaps |
8 | |||
9 | You can define up to 32 macros in a `keymap.json` file, as used by [Configurator](newbs_building_firmware_configurator.md), and `qmk compile`. You can define these macros in a list under the `macros` keyword, like this: | ||
10 | |||
11 | ```json | ||
12 | { | ||
13 | "keyboard": "handwired/my_macropad", | ||
14 | "keymap": "my_keymap", | ||
15 | "macros": [ | ||
16 | [ | ||
17 | {"action":"down", "keycodes": ["LSFT"]}, | ||
18 | "hello world1", | ||
19 | {"action": "up","keycodes": ["LSFT"]} | ||
20 | ], | ||
21 | [ | ||
22 | {"action":"tap", "keycodes": ["LCTL", "LALT", "DEL"]} | ||
23 | ], | ||
24 | [ | ||
25 | "ding!", | ||
26 | {"action":"beep"} | ||
27 | ], | ||
28 | [ | ||
29 | {"action":"tap", "keycodes": ["F1"]}, | ||
30 | {"action":"delay", "duration": "1000"}, | ||
31 | {"action":"tap", "keycodes": ["PGDN"]} | ||
32 | ] | ||
33 | ], | ||
34 | "layout": "LAYOUT_all", | ||
35 | "layers": [ | ||
36 | ["MACRO_0", "MACRO_1", "MACRO_2", "MACRO_3"] | ||
37 | ] | ||
38 | } | ||
39 | ``` | ||
40 | |||
41 | ### Selecting Your Host Keyboard Layout | ||
42 | |||
43 | 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 add the `host_language` key to your keymap.json, like so: | ||
44 | |||
45 | ```json | ||
46 | { | ||
47 | "keyboard": "handwired/my_macropad", | ||
48 | "keymap": "my_keymap", | ||
49 | "host_layout": "dvorak", | ||
50 | "macros": [ | ||
51 | ["Hello, World!"] | ||
52 | ], | ||
53 | "layout": "LAYOUT_all", | ||
54 | "layers": [ | ||
55 | ["MACRO_0"] | ||
56 | ] | ||
57 | } | ||
58 | ``` | ||
59 | |||
60 | The current list of available languages is: | ||
61 | |||
62 | | belgian | bepo | br_abnt2 | canadian_multilingual | | ||
63 | |:-------:|:----:|:--------:|:---------------------:| | ||
64 | | **colemak** | **croatian** | **czech** | **danish** | | ||
65 | | **dvorak_fr** | **dvorak** | **dvp** | **estonian** | | ||
66 | | **finnish** | **fr_ch** | **french_afnor** | **french** | | ||
67 | | **french_osx** | **german_ch** | **german** | **german_osx** | | ||
68 | | **hungarian** | **icelandic** | **italian** | **italian_osx_ansi** | | ||
69 | | **italian_osx_iso** | **jis** | **latvian** | **lithuanian_azerty** | | ||
70 | | **lithuanian_qwerty** | **norman** | **norwegian** | **portuguese** | | ||
71 | | **portuguese_osx_iso** | **romanian** | **serbian_latin** | **slovak** | | ||
72 | | **slovenian** | **spanish_dvorak** | **spanish** | **swedish** | | ||
73 | | **turkish_f** | **turkish_q** | **uk** | **us_international** | | ||
74 | | **workman** | **workman_zxcvm** | | ||
75 | |||
76 | ### Macro Basics | ||
77 | |||
78 | 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. | ||
79 | |||
80 | #### Object Format | ||
81 | |||
82 | All objects have one required key: `action`. This tells QMK what the object does. There are currently 5 actions: beep, delay, down, tap, up | ||
83 | |||
84 | Only basic keycodes (prefixed by `KC_`) are supported. Do not include the `KC_` prefix when listing keycodes. | ||
85 | |||
86 | * `beep` | ||
87 | * Play a bell if the keyboard has [audio enabled](feature_audio.md). | ||
88 | * Example: `{"action": "beep"}` | ||
89 | * `delay` | ||
90 | * Pause macro playback. Duration is specified in milliseconds (ms). | ||
91 | * Example: `{"action": "delay", "duration": 500}` | ||
92 | * `down` | ||
93 | * Send a key down event for one or more keycodes. | ||
94 | * Example, single key: `{"action":"down", "keycodes": ["LSFT"]}` | ||
95 | * Example, multiple keys: `{"action":"down", "keycodes": ["CTRL", "LSFT"]}` | ||
96 | * `tap` | ||
97 | * Type a chord, which sends a down event for each key followed by an up event for each key. | ||
98 | * Example, single key: `{"action":"tap", "keycodes": ["F13"]}` | ||
99 | * Example, multiple keys: `{"action":"tap", "keycodes": ["CTRL", "LALT", "DEL"]}` | ||
100 | * `up` | ||
101 | * Send a key up event for one or more keycodes. | ||
102 | * Example, single key: `{"action":"up", "keycodes": ["LSFT"]}` | ||
103 | * Example, multiple keys: `{"action":"up", "keycodes": ["CTRL", "LSFT"]}` | ||
104 | |||
105 | ## Using Macros in C Keymaps | ||
106 | |||
107 | ### `SEND_STRING()` & `process_record_user` | ||
8 | 108 | ||
9 | 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`). | 109 | 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`). |
10 | 110 | ||
@@ -91,7 +191,7 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { | |||
91 | }; | 191 | }; |
92 | ``` | 192 | ``` |
93 | 193 | ||
94 | ### Advanced Macros | 194 | #### Advanced Macros |
95 | 195 | ||
96 | 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. | 196 | 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. |
97 | 197 | ||
@@ -131,7 +231,7 @@ void post_process_record_user(uint16_t keycode, keyrecord_t *record) { | |||
131 | ``` | 231 | ``` |
132 | 232 | ||
133 | 233 | ||
134 | ### TAP, DOWN and UP | 234 | #### TAP, DOWN and UP |
135 | 235 | ||
136 | You may want to use keys in your macros that you can't write down, such as `Ctrl` or `Home`. | 236 | You may want to use keys in your macros that you can't write down, such as `Ctrl` or `Home`. |
137 | You can send arbitrary keycodes by wrapping them in: | 237 | You can send arbitrary keycodes by wrapping them in: |
@@ -178,7 +278,7 @@ They can be used like this: | |||
178 | 278 | ||
179 | 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. | 279 | 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. |
180 | 280 | ||
181 | ### Alternative Keymaps | 281 | #### Alternative Keymaps |
182 | 282 | ||
183 | 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: | 283 | 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: |
184 | 284 | ||
@@ -186,7 +286,7 @@ By default, it assumes a US keymap with a QWERTY layout; if you want to change t | |||
186 | #include "sendstring_colemak.h" | 286 | #include "sendstring_colemak.h" |
187 | ``` | 287 | ``` |
188 | 288 | ||
189 | ### Strings in Memory | 289 | #### Strings in Memory |
190 | 290 | ||
191 | 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: | 291 | 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: |
192 | 292 | ||
@@ -205,13 +305,13 @@ SEND_STRING(".."SS_TAP(X_END)); | |||
205 | ``` | 305 | ``` |
206 | 306 | ||
207 | 307 | ||
208 | ## Advanced Macro Functions | 308 | ### Advanced Macro Functions |
209 | 309 | ||
210 | 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. | 310 | 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. |
211 | 311 | ||
212 | ?> You can also use the functions described in [Useful function](ref_functions.md) 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. | 312 | ?> You can also use the functions described in [Useful function](ref_functions.md) 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. |
213 | 313 | ||
214 | ### `record->event.pressed` | 314 | #### `record->event.pressed` |
215 | 315 | ||
216 | This is a boolean value that can be tested to see if the switch is being pressed or released. An example of this is | 316 | This is a boolean value that can be tested to see if the switch is being pressed or released. An example of this is |
217 | 317 | ||
@@ -223,15 +323,15 @@ This is a boolean value that can be tested to see if the switch is being pressed | |||
223 | } | 323 | } |
224 | ``` | 324 | ``` |
225 | 325 | ||
226 | ### `register_code(<kc>);` | 326 | #### `register_code(<kc>);` |
227 | 327 | ||
228 | 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`. | 328 | 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`. |
229 | 329 | ||
230 | ### `unregister_code(<kc>);` | 330 | #### `unregister_code(<kc>);` |
231 | 331 | ||
232 | 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. | 332 | 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. |
233 | 333 | ||
234 | ### `tap_code(<kc>);` | 334 | #### `tap_code(<kc>);` |
235 | 335 | ||
236 | 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). | 336 | 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). |
237 | 337 | ||
@@ -239,31 +339,31 @@ If `TAP_CODE_DELAY` is defined (default 0), this function waits that many millis | |||
239 | 339 | ||
240 | 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. | 340 | 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. |
241 | 341 | ||
242 | ### `tap_code_delay(<kc>, <delay>);` | 342 | #### `tap_code_delay(<kc>, <delay>);` |
243 | 343 | ||
244 | Like `tap_code(<kc>)`, but with a `delay` parameter for specifying arbitrary intervals before sending the unregister event. | 344 | Like `tap_code(<kc>)`, but with a `delay` parameter for specifying arbitrary intervals before sending the unregister event. |
245 | 345 | ||
246 | ### `register_code16(<kc>);`, `unregister_code16(<kc>);` and `tap_code16(<kc>);` | 346 | #### `register_code16(<kc>);`, `unregister_code16(<kc>);` and `tap_code16(<kc>);` |
247 | 347 | ||
248 | 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). | 348 | 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). |
249 | 349 | ||
250 | Eg, you could use `register_code16(S(KC_5));` instead of registering the mod, then registering the keycode. | 350 | Eg, you could use `register_code16(S(KC_5));` instead of registering the mod, then registering the keycode. |
251 | 351 | ||
252 | ### `clear_keyboard();` | 352 | #### `clear_keyboard();` |
253 | 353 | ||
254 | This will clear all mods and keys currently pressed. | 354 | This will clear all mods and keys currently pressed. |
255 | 355 | ||
256 | ### `clear_mods();` | 356 | #### `clear_mods();` |
257 | 357 | ||
258 | This will clear all mods currently pressed. | 358 | This will clear all mods currently pressed. |
259 | 359 | ||
260 | ### `clear_keyboard_but_mods();` | 360 | #### `clear_keyboard_but_mods();` |
261 | 361 | ||
262 | This will clear all keys besides the mods currently pressed. | 362 | This will clear all keys besides the mods currently pressed. |
263 | 363 | ||
264 | ## Advanced Example: | 364 | ### Advanced Example: |
265 | 365 | ||
266 | ### Super ALT↯TAB | 366 | #### Super ALT↯TAB |
267 | 367 | ||
268 | 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. | 368 | 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. |
269 | 369 | ||