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.
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.
9 ## The new way: `SEND_STRING()` & `process_record_user`
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`).
16 enum custom_keycodes {
17 PRINT_TRUTH = SAFE_RANGE
20 bool process_record_user(uint16_t keycode, keyrecord_t *record) {
21 if (record->event.pressed) {
24 SEND_STRING("QMK is the best thing ever!");
34 You can send arbitary keycodes by wrapping them in:
42 SEND_STRING(SS_TAP(X_HOME));
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:
46 SEND_STRING("VE"SS_TAP(X_HOME)"LO");
48 Which would send "VE" followed by a `KC_HOME` tap, and "LO" (spelling "LOVE" if on a newline).
50 There's also a couple of mod shortcuts you can use:
56 That can be used like this:
58 SEND_STRING(SS_LCTRL("a"));
60 Which would send LCTRL+a (LTRL down, a, LTRL up) - notice that they take strings (eg `"k"`), and not the `X_K` keycodes.
62 ### Alternative keymaps
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:
66 #include <sendstring_colemak.h>
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:
73 char my_str[4] = "ok.";
77 The shortcuts defined above won't work with `send_string()`, but you can separate things out to different lines if needed:
80 char my_str[4] = "ok.";
81 SEND_STRING("I said: ");
83 SEND_STRING(".."SS_TAP(X_END));
86 ## The old way: `MACRO()` & `action_get_macro`
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.
92 By default QMK assumes you don't have any macros. To define your macros you create an `action_get_macro()` function. For example:
95 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt) {
96 if (record->event.pressed) {
99 return MACRO(D(LSFT), T(H), U(LSFT), T(I), D(LSFT), T(1), U(LSFT), END);
101 return MACRO(D(LSFT), T(B), U(LSFT), T(Y), T(E), D(LSFT), T(1), U(LSFT), END);
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:
110 if (!record->event.pressed) {
114 A macro can include the following commands:
116 * I() change interval of stroke in milliseconds.
119 * T() type key(press and release).
120 * W() wait (milliseconds).
123 ### Mapping a Macro to a key
125 Use the `M()` function within your `KEYMAP()` to call a macro. For example, here is the keymap for a 2-key keyboard:
128 const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
134 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt) {
135 if (record->event.pressed) {
138 return MACRO(D(LSFT), T(H), U(LSFT), T(I), D(LSFT), T(1), U(LSFT), END);
140 return MACRO(D(LSFT), T(B), U(LSFT), T(Y), T(E), D(LSFT), T(1), U(LSFT), END);
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!".
149 ### Naming your macros
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.
157 const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
164 ## Advanced macro functions
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.
168 ### `record->event.pressed`
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
173 if (record->event.pressed) {
180 ### `register_code(<kc>);`
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`.
184 ### `unregister_code(<kc>);`
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.
188 ### `clear_keyboard();`
190 This will clear all mods and keys currently pressed.
194 This will clear all mods currently pressed.
196 ### `clear_keyboard_but_mods();`
198 This will clear all keys besides the mods currently pressed.
200 ## Advanced Example: Single-key copy/paste
202 This example defines a macro which sends `Ctrl-C` when pressed down, and `Ctrl-V` when released.
205 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt) {
208 if (record->event.pressed) {
209 return MACRO( D(LCTL), T(C), U(LCTL), END );
211 return MACRO( D(LCTL), T(V), U(LCTL), END );