]> git.donarmstrong.com Git - qmk_firmware.git/blob - docs/feature_macros.md
convert to unix line-endings [skip ci]
[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 for you. All ascii that is easily translated to a keycode is supported (eg `\n\t`).
12
13 For example:
14
15 ```c
16 enum custom_keycodes {
17         PRINT_TRUTH = 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 PRINT_TRUTH:
24                                 SEND_STRING("QMK is the best thing ever!");
25                                 return false; break;
26                 }
27         }
28         return true;
29 };
30 ```
31
32 ### Tap/down/up
33
34 You can send arbitary keycodes by wrapping them in:
35
36 * `SS_TAP()`
37 * `SS_DOWN()`
38 * `SS_UP()`
39
40 For example:
41
42     SEND_STRING(SS_TAP(X_HOME));
43
44 Would 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
48 Which would send "VE" followed by a `KC_HOME` tap, and "LO" (spelling "LOVE" if on a newline).
49
50 There's also a couple of mod shortcuts you can use:
51
52 * `SS_LCTRL(string)`
53 * `SS_LGUI(string)`
54 * `SS_LALT(string)`
55
56 That can be used like this:
57
58     SEND_STRING(SS_LCTRL("a"));
59
60 Which 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
64 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:
65
66     #include <sendstring_colemak.h>
67
68 ### Strings in memory
69
70 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:
71
72 ```c
73 char my_str[4] = "ok.";
74 send_string(my_str);
75 ```
76
77 The shortcuts defined above won't work with `send_string()`, but you can separate things out to different lines if needed:
78
79 ```c
80 char my_str[4] = "ok.";
81 SEND_STRING("I said: ");
82 send_string(my_str);
83 SEND_STRING(".."SS_TAP(X_END));
84 ```
85
86 ## The old way: `MACRO()` & `action_get_macro`
87
88 {% hint style='info' %}
89 This is inherited from TMK, and hasn't been updated - it's recommend that you use `SEND_STRING` and `process_record_user` instead.
90 {% endhint %}
91
92 By default QMK assumes you don't have any macros. To define your macros you create an `action_get_macro()` function. For example:
93
94 ```c
95 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt) {
96         if (record->event.pressed) {
97                 switch(id) {
98                         case 0:
99                                 return MACRO(D(LSFT), T(H), U(LSFT), T(I), D(LSFT), T(1), U(LSFT), END);
100                         case 1:
101                                 return MACRO(D(LSFT), T(B), U(LSFT), T(Y), T(E), D(LSFT), T(1), U(LSFT), END);
102                 }
103         }
104         return MACRO_NONE;
105 };
106 ```
107
108 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:
109
110         if (!record->event.pressed) {
111
112 ### Macro Commands
113
114 A 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.
122
123 ### Mapping a Macro to a key
124
125 Use the `M()` function within your `KEYMAP()` to call a macro. For example, here is the keymap for a 2-key keyboard:
126
127 ```c
128 const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
129         [0] = KEYMAP(
130                 M(0), M(1)
131         ),
132 };
133
134 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt) {
135         if (record->event.pressed) {
136                 switch(id) {
137                         case 0:
138                                 return MACRO(D(LSFT), T(H), U(LSFT), T(I), D(LSFT), T(1), U(LSFT), END);
139                         case 1:
140                                 return MACRO(D(LSFT), T(B), U(LSFT), T(Y), T(E), D(LSFT), T(1), U(LSFT), END);
141                 }
142         }
143         return MACRO_NONE;
144 };
145 ```
146
147 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!".
148
149 ### Naming your macros
150
151 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.
152
153 ```c
154 #define M_HI M(0)
155 #define M_BYE M(1)
156
157 const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
158         [0] = KEYMAP(
159                 M_HI, M_BYE
160         ),
161 };
162 ```
163
164 ## Advanced macro functions
165
166 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.
167
168 ### `record->event.pressed`
169
170 This is a boolean value that can be tested to see if the switch is being pressed or released. An example of this is
171
172 ```c
173         if (record->event.pressed) {
174                 // on keydown
175         } else {
176                 // on keyup
177         }
178 ```
179
180 ### `register_code(<kc>);`
181
182 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`.
183
184 ### `unregister_code(<kc>);`
185
186 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.
187
188 ### `clear_keyboard();`
189
190 This will clear all mods and keys currently pressed.
191
192 ### `clear_mods();`
193
194 This will clear all mods currently pressed.
195
196 ### `clear_keyboard_but_mods();`
197
198 This will clear all keys besides the mods currently pressed.
199
200 ## Advanced Example: Single-key copy/paste
201
202 This example defines a macro which sends `Ctrl-C` when pressed down, and `Ctrl-V` when released. 
203
204 ```c
205 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt) {
206         switch(id) {
207                 case 0: {
208                         if (record->event.pressed) {
209                                 return MACRO( D(LCTL), T(C), U(LCTL), END  );
210                         } else {
211                                 return MACRO( D(LCTL), T(V), U(LCTL), END  );
212                         }
213                         break;
214                 }
215         }
216         return MACRO_NONE;
217 };
218 ```
219
220