]> git.donarmstrong.com Git - qmk_firmware.git/blob - docs/feature_macros.md
Fix up info boxes.
[qmk_firmware.git] / docs / feature_macros.md
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 !> **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
7 ## The New Way: `SEND_STRING()` & `process_record_user`
8
9 Sometimes 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 (i.e. a sequence of characters) for you. All ASCII characters that are easily translated to a keycode are supported (e.g. `\n\t`).
10
11 Here is an example `keymap.c` for a two-key keyboard:
12
13 ```c
14 enum custom_keycodes {
15         MY_CUSTOM_MACRO = SAFE_RANGE
16 };
17
18 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
19         if (record->event.pressed) {
20                 switch(keycode) {
21                         case MY_CUSTOM_MACRO:
22                                 SEND_STRING("QMK is the best thing ever!"); // this is our macro!
23                                 return false;
24                 }
25         }
26         return true;
27 };
28
29 const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
30         [0] = {
31           {MY_CUSTOM_MACRO, KC_ESC}
32         }
33 };
34 ```
35
36 What happens here is this:
37 We first define a new custom keycode in the range not occupied by any other keycodes.
38 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.
39 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).
40 We return `false` to indicate to the caller that the key press we just processed need not be processed any further.
41 Finally, we define the keymap so that the first button activates our macro and the second button is just an escape button.
42
43 You might want to add more than one macro.
44 You can do that by adding another keycode and adding another case to the switch statement, like so:
45
46 ```c
47 enum custom_keycodes {
48         MY_CUSTOM_MACRO = SAFE_RANGE,
49         MY_OTHER_MACRO
50 };
51
52 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
53         if (record->event.pressed) {
54                 switch(keycode) {
55                         case MY_CUSTOM_MACRO:
56                                 SEND_STRING("QMK is the best thing ever!");
57                                 return false;
58                         case MY_OTHER_MACRO:
59                                 SEND_STRING(SS_LCTRL("ac")); // selects all and copies
60                                 return false;
61                 }
62         }
63         return true;
64 };
65
66 const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
67         [0] = {
68           {MY_CUSTOM_MACRO, MY_OTHER_MACRO}
69         }
70 };
71 ```
72
73 ### TAP, DOWN and UP
74
75 You may want to use keys in your macros that you can't write down, such as `Ctrl` or `Home`.
76 You can send arbitrary keycodes by wrapping them in:
77
78 * `SS_TAP()` presses and releases a key.
79 * `SS_DOWN()` presses (but does not release) a key.
80 * `SS_UP()` releases a key.
81
82 For example:
83
84     SEND_STRING(SS_TAP(X_HOME));
85
86 Would tap `KC_HOME` - note how the prefix is now `X_`, and not `KC_`. You can also combine this with other strings, like this:
87
88     SEND_STRING("VE"SS_TAP(X_HOME)"LO");
89
90 Which would send "VE" followed by a `KC_HOME` tap, and "LO" (spelling "LOVE" if on a newline).
91
92 There's also a couple of mod shortcuts you can use:
93
94 * `SS_LCTRL(string)`
95 * `SS_LGUI(string)`
96 * `SS_LALT(string)`
97 * `SS_LSFT(string)`
98 * `SS_RALT(string)`
99
100 These press the respective modifier, send the supplied string and then release the modifier.
101 They can be used like this:
102
103     SEND_STRING(SS_LCTRL("a"));
104
105 Which would send LCTRL+a (LCTRL down, a, LCTRL up) - notice that they take strings (eg `"k"`), and not the `X_K` keycodes.
106
107 ### Alternative Keymaps
108
109 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:
110
111     #include <sendstring_colemak.h>
112
113 ### Strings in Memory
114
115 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:
116
117 ```c
118 char my_str[4] = "ok.";
119 send_string(my_str);
120 ```
121
122 The shortcuts defined above won't work with `send_string()`, but you can separate things out to different lines if needed:
123
124 ```c
125 char my_str[4] = "ok.";
126 SEND_STRING("I said: ");
127 send_string(my_str);
128 SEND_STRING(".."SS_TAP(X_END));
129 ```
130
131 ## The Old Way: `MACRO()` & `action_get_macro`
132
133 ?> This is inherited from TMK, and hasn't been updated - it's recommend that you use `SEND_STRING` and `process_record_user` instead.
134
135 By default QMK assumes you don't have any macros. To define your macros you create an `action_get_macro()` function. For example:
136
137 ```c
138 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt) {
139         if (record->event.pressed) {
140                 switch(id) {
141                         case 0:
142                                 return MACRO(D(LSFT), T(H), U(LSFT), T(I), D(LSFT), T(1), U(LSFT), END);
143                         case 1:
144                                 return MACRO(D(LSFT), T(B), U(LSFT), T(Y), T(E), D(LSFT), T(1), U(LSFT), END);
145                 }
146         }
147         return MACRO_NONE;
148 };
149 ```
150
151 This 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:
152
153         if (!record->event.pressed) {
154
155 ### Macro Commands
156
157 A macro can include the following commands:
158
159 * I() change interval of stroke in milliseconds.
160 * D() press key.
161 * U() release key.
162 * T() type key(press and release).
163 * W() wait (milliseconds).
164 * END end mark.
165
166 ### Mapping a Macro to a Key
167
168 Use the `M()` function within your `KEYMAP()` to call a macro. For example, here is the keymap for a 2-key keyboard:
169
170 ```c
171 const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
172         [0] = KEYMAP(
173                 M(0), M(1)
174         ),
175 };
176
177 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt) {
178         if (record->event.pressed) {
179                 switch(id) {
180                         case 0:
181                                 return MACRO(D(LSFT), T(H), U(LSFT), T(I), D(LSFT), T(1), U(LSFT), END);
182                         case 1:
183                                 return MACRO(D(LSFT), T(B), U(LSFT), T(Y), T(E), D(LSFT), T(1), U(LSFT), END);
184                 }
185         }
186         return MACRO_NONE;
187 };
188 ```
189
190 When you press the key on the left it will type "Hi!" and when you press the key on the right it will type "Bye!".
191
192 ### Naming Your Macros
193
194 If 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.
195
196 ```c
197 #define M_HI M(0)
198 #define M_BYE M(1)
199
200 const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
201         [0] = KEYMAP(
202                 M_HI, M_BYE
203         ),
204 };
205 ```
206
207 ## Advanced Macro Functions
208
209 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.
210
211 ### `record->event.pressed`
212
213 This is a boolean value that can be tested to see if the switch is being pressed or released. An example of this is
214
215 ```c
216         if (record->event.pressed) {
217                 // on keydown
218         } else {
219                 // on keyup
220         }
221 ```
222
223 ### `register_code(<kc>);`
224
225 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`.
226
227 ### `unregister_code(<kc>);`
228
229 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.
230
231 ### `clear_keyboard();`
232
233 This will clear all mods and keys currently pressed.
234
235 ### `clear_mods();`
236
237 This will clear all mods currently pressed.
238
239 ### `clear_keyboard_but_mods();`
240
241 This will clear all keys besides the mods currently pressed.
242
243 ## Advanced Example: Single-Key Copy/Paste
244
245 This example defines a macro which sends `Ctrl-C` when pressed down, and `Ctrl-V` when released.
246
247 ```c
248 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt) {
249         switch(id) {
250                 case 0: {
251                         if (record->event.pressed) {
252                                 return MACRO( D(LCTL), T(C), U(LCTL), END  );
253                         } else {
254                                 return MACRO( D(LCTL), T(V), U(LCTL), END  );
255                         }
256                         break;
257                 }
258         }
259         return MACRO_NONE;
260 };
261 ```