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