aboutsummaryrefslogtreecommitdiff
path: root/docs/macros.md
diff options
context:
space:
mode:
Diffstat (limited to 'docs/macros.md')
-rw-r--r--docs/macros.md136
1 files changed, 95 insertions, 41 deletions
diff --git a/docs/macros.md b/docs/macros.md
index c7a9b2e7a..66d2bc090 100644
--- a/docs/macros.md
+++ b/docs/macros.md
@@ -1,72 +1,126 @@
1# Macros 1# Macros
2 2
3Macros 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. 3Macros 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 4
5{% hint style='danger' %} 5{% hint style='danger' %}
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 ahold of your keyboard will be able to access that information by opening a text editor. 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 ahold of your keyboard will be able to access that information by opening a text editor.
7{% endhint %} 7{% endhint %}
8 8
9# Macro Definitions 9## The new way: `SEND_STRING()` & `process_record_user`
10 10
11By default QMK assumes you don't have any macros. To define your macros you create an `action_get_macro()` function. For example: 11Sometimes you just want a key to type out words or phrases. For the most common situations we've provided `SEND_STRING()`, which will type out your string for you. All ascii that is easily translated to a keycode is supported (eg `\n\t`).
12
13For example:
12 14
13```c 15```c
14const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt) { 16enum custom_keycodes {
17 PRINT_TRUTH = SAFE_RANGE
18};
19
20bool process_record_user(uint16_t keycode, keyrecord_t *record) {
15 if (record->event.pressed) { 21 if (record->event.pressed) {
16 switch(id) { 22 switch(keycode) {
17 case 0: 23 case PRINT_TRUTH:
18 return MACRO(D(LSFT), T(H), U(LSFT), T(I), D(LSFT), T(1), U(LSFT), END); 24 SEND_STRING("QMK is the best thing ever!");
19 case 1: 25 return false; break;
20 return MACRO(D(LSFT), T(B), U(LSFT), T(Y), T(E), D(LSFT), T(1), U(LSFT), END);
21 } 26 }
22 } 27 }
23 return MACRO_NONE; 28 return true;
24}; 29};
25``` 30```
26 31
27This defines two macros which will be run when the key they are assigned to is pressed. If instead you'd like them to run when the key is released you can change the if statement: 32### Tap/down/up
33
34You can send arbitary keycodes by wrapping them in:
35
36* `SS_TAP()`
37* `SS_DOWN()`
38* `SS_UP()`
39
40For example:
41
42 SEND_STRING(SS_TAP(X_HOME));
43
44Would tap `KC_HOME` - note how the prefix is now `X_`, and not `KC_`. You can also combine this with other strings, like this:
45
46 SEND_STRING("VE"SS_TAP(X_HOME)"LO");
47
48Which would send "VE" followed by a `KC_HOME` tap, and "LO" (spelling "LOVE" if on a newline).
49
50There's also a couple of mod shortcuts you can use:
51
52* `SS_LCTRL(string)`
53* `SS_LGUI(string)`
54* `SS_LALT(string)`
55
56That can be used like this:
57
58 SEND_STRING(SS_LCTRL("a"));
59
60Which would send LCTRL+a (LTRL down, a, LTRL up) - notice that they take strings (eg `"k"`), and not the `X_K` keycodes.
61
62### Alternative keymaps
63
64By 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:
65
66 #include <sendstring_colemak.h>
67
68### Strings in memory
69
70If 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:
28 71
29```c 72```c
30 if (!record->event.pressed) { 73char my_str[4] = "ok.";
74send_string(my_str);
31``` 75```
32 76
33## Macro Commands 77The shortcuts defined above won't work with `send_string()`, but you can separate things out to different lines if needed:
34
35A macro can include the following commands:
36 78
37* I() change interval of stroke in milliseconds. 79```c
38* D() press key. 80char my_str[4] = "ok.";
39* U() release key. 81SEND_STRING("I said: ");
40* T() type key(press and release). 82send_string(my_str);
41* W() wait (milliseconds). 83SEND_STRING(".."SS_TAP(X_END));
42* END end mark. 84```
43 85
44## Sending strings 86## The old way: `MACRO()` & `action_get_macro`
45 87
46Sometimes you just want a key to type out words or phrases. For the most common situations we've provided `SEND_STRING()`, which will type out your string for you instead of having to build a `MACRO()`. 88{% hint style='info' %}
89This is inherited from TMK, and hasn't been updated - it's recommend that you use `SEND_STRING` and `process_record_user` instead.
90{% endhint %}
47 91
48For example: 92By default QMK assumes you don't have any macros. To define your macros you create an `action_get_macro()` function. For example:
49 93
50```c 94```c
51const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt) { 95const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt) {
52 if (record->event.pressed) { 96 if (record->event.pressed) {
53 switch(id) { 97 switch(id) {
54 case 0: 98 case 0:
55 SEND_STRING("QMK is the best thing ever!"); 99 return MACRO(D(LSFT), T(H), U(LSFT), T(I), D(LSFT), T(1), U(LSFT), END);
56 return false; 100 case 1:
101 return MACRO(D(LSFT), T(B), U(LSFT), T(Y), T(E), D(LSFT), T(1), U(LSFT), END);
57 } 102 }
58 } 103 }
59 return MACRO_NONE; 104 return MACRO_NONE;
60}; 105};
61``` 106```
62 107
63By 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: 108This defines two macros which will be run when the key they are assigned to is pressed. If instead you'd like them to run when the key is released you can change the if statement:
64 109
65``` 110 if (!record->event.pressed) {
66#include <sendstring_colemak.h> 111
67``` 112### Macro Commands
113
114A macro can include the following commands:
115
116* I() change interval of stroke in milliseconds.
117* D() press key.
118* U() release key.
119* T() type key(press and release).
120* W() wait (milliseconds).
121* END end mark.
68 122
69## Mapping a Macro to a key 123### Mapping a Macro to a key
70 124
71Use the `M()` function within your `KEYMAP()` to call a macro. For example, here is the keymap for a 2-key keyboard: 125Use the `M()` function within your `KEYMAP()` to call a macro. For example, here is the keymap for a 2-key keyboard:
72 126
@@ -92,7 +146,7 @@ const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt) {
92 146
93When you press the key on the left it will type "Hi!" and when you press the key on the right it will type "Bye!". 147When you press the key on the left it will type "Hi!" and when you press the key on the right it will type "Bye!".
94 148
95## Naming your macros 149### Naming your macros
96 150
97If you have a bunch of macros you want to refer to from your keymap while keeping the keymap easily readable you can name them using `#define` at the top of your file. 151If you have a bunch of macros you want to refer to from your keymap while keeping the keymap easily readable you can name them using `#define` at the top of your file.
98 152
@@ -107,11 +161,11 @@ const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
107}; 161};
108``` 162```
109 163
110# Advanced macro functions 164## Advanced macro functions
111 165
112While working within the `action_get_macro()` function block there are some functions you may find useful. 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. 166There 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.
113 167
114#### `record->event.pressed` 168### `record->event.pressed`
115 169
116This is a boolean value that can be tested to see if the switch is being pressed or released. An example of this is 170This is a boolean value that can be tested to see if the switch is being pressed or released. An example of this is
117 171
@@ -123,27 +177,27 @@ This is a boolean value that can be tested to see if the switch is being pressed
123 } 177 }
124``` 178```
125 179
126#### `register_code(<kc>);` 180### `register_code(<kc>);`
127 181
128This 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`. 182This 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`.
129 183
130#### `unregister_code(<kc>);` 184### `unregister_code(<kc>);`
131 185
132Parallel 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. 186Parallel 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.
133 187
134#### `clear_keyboard();` 188### `clear_keyboard();`
135 189
136This will clear all mods and keys currently pressed. 190This will clear all mods and keys currently pressed.
137 191
138#### `clear_mods();` 192### `clear_mods();`
139 193
140This will clear all mods currently pressed. 194This will clear all mods currently pressed.
141 195
142#### `clear_keyboard_but_mods();` 196### `clear_keyboard_but_mods();`
143 197
144This will clear all keys besides the mods currently pressed. 198This will clear all keys besides the mods currently pressed.
145 199
146# Advanced Example: Single-key copy/paste 200## Advanced Example: Single-key copy/paste
147 201
148This example defines a macro which sends `Ctrl-C` when pressed down, and `Ctrl-V` when released. 202This example defines a macro which sends `Ctrl-C` when pressed down, and `Ctrl-V` when released.
149 203