]> git.donarmstrong.com Git - qmk_firmware.git/blob - Macros.md
Created Unit testing (markdown)
[qmk_firmware.git] / Macros.md
1 # Macro shortcuts: Send a whole string when pressing just one key
2
3 Instead of using the `ACTION_MACRO` function, you can simply use `M(n)` to access macro *n* - *n* will get passed into the `action_get_macro` as the `id`, and you can use a switch statement to trigger it. This gets called on the keydown and keyup, so you'll need to use an if statement testing `record->event.pressed` (see keymap_default.c).
4
5 ```c
6 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt) // this is the function signature -- just copy/paste it into your keymap file as it is.
7 {
8   switch(id) {
9     case 0: // this would trigger when you hit a key mapped as M(0)
10       if (record->event.pressed) {
11         return MACRO( I(255), T(H), T(E), T(L), T(L), W(255), T(O), END  ); // this sends the string 'hello' when the macro executes
12       }
13       break;
14   }
15   return MACRO_NONE;
16 };
17 ```
18 A macro can include the following commands:
19
20 * I() change interval of stroke in milliseconds.
21 * D() press key.
22 * U() release key.
23 * T() type key(press and release).
24 * W() wait (milliseconds).
25 * END end mark.
26
27 So above you can see the stroke interval changed to 255ms between each keystroke, then a bunch of keys being typed, waits a while, then the macro ends.
28
29 Note: Using macros to have your keyboard send passwords for you is possible, but a bad idea.
30
31 ## Advanced macro functions
32
33 To get more control over the keys/actions your keyboard takes, the following functions are available to you in the `action_get_macro` function block:
34
35 * `record->event.pressed`
36
37 This is a boolean value that can be tested to see if the switch is being pressed or released. An example of this is
38
39 ```c
40 if (record->event.pressed) {
41   // on keydown
42 } else {
43   // on keyup
44 }
45 ```
46
47 * `register_code(<kc>);`
48
49 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`.
50
51 * `unregister_code(<kc>);`
52
53 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.
54
55 * `layer_on(<n>);`
56
57 This will turn on the layer `<n>` - the higher layer number will always take priority. Make sure you have `KC_TRNS` for the key you're pressing on the layer you're switching to, or you'll get stick there unless you have another plan.
58
59 * `layer_off(<n>);`
60
61 This will turn off the layer `<n>`.
62
63 * `clear_keyboard();`
64
65 This will clear all mods and keys currently pressed.
66
67 * `clear_mods();`
68
69 This will clear all mods currently pressed.
70
71 * `clear_keyboard_but_mods();`
72
73 This will clear all keys besides the mods currently pressed.
74
75 * `update_tri_layer(layer_1, layer_2, layer_3);`
76
77 If the user attempts to activate layer 1 AND layer 2 at the same time (for example, by hitting their respective layer keys), layer 3 will be activated. Layers 1 and 2 will _also_ be activated, for the purposes of fallbacks (so a given key will fall back from 3 to 2, to 1 -- and only then to 0).
78
79 ### Naming your macros
80
81 If you have a bunch of macros you want to refer to from your keymap, while keeping the keymap easily readable, you can just name them like so:
82
83 ```
84 #define AUD_OFF M(6)
85 #define AUD_ON M(7)
86 #define MUS_OFF M(8)
87 #define MUS_ON M(9)
88 #define VC_IN M(10)
89 #define VC_DE M(11)
90 #define PLOVER M(12)
91 #define EXT_PLV M(13)
92 ```
93
94 As was done on the [Planck default keymap](/keyboards/planck/keymaps/default/keymap.c#L33-L40)
95
96 #### Timer functionality
97
98 It's possible to start timers and read values for time-specific events - here's an example:
99
100 ```c
101 static uint16_t key_timer;
102 key_timer = timer_read();
103 if (timer_elapsed(key_timer) < 100) {
104   // do something if less than 100ms have passed
105 } else {
106   // do something if 100ms or more have passed
107 }
108 ```
109
110 It's best to declare the `static uint16_t key_timer;` outside of the macro block (top of file, etc).
111
112 ### Example: Single-key copy/paste (hold to copy, tap to paste)
113
114 With QMK, it's easy to make one key do two things, as long as one of those things is being a modifier. :) So if you want a key to act as Ctrl when held and send the letter R when tapped, that's easy: `CTL_T(KC_R)`. But what do you do when you want that key to send Ctrl-V (paste) when tapped, and Ctrl-C (copy) when held?
115
116 Here's what you do:
117
118
119 ```
120 static uint16_t key_timer;
121
122 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
123 {
124       switch(id) {
125         case 0: {
126             if (record->event.pressed) {
127                 key_timer = timer_read(); // if the key is being pressed, we start the timer.
128             }
129             else { // this means the key was just released, so we can figure out how long it was pressed for (tap or "held down").
130                 if (timer_elapsed(key_timer) > 150) { // 150 being 150ms, the threshhold we pick for counting something as a tap.
131                     return MACRO( D(LCTL), T(C), U(LCTL), END  );
132                 }
133                 else {
134                     return MACRO( D(LCTL), T(V), U(LCTL), END  );
135                 }
136             }
137             break;
138         }
139       }
140     return MACRO_NONE;
141 };
142 ```
143
144 And then, to assign this macro to a key on your keyboard layout, you just use `M(0)` on the key you want to press for copy/paste.
145
146 # Dynamic macros: record and replay macros in runtime
147
148 In addition to the static macros described above, you may enable the dynamic macros which you may record while writing. They are forgotten as soon as the keyboard is unplugged. Only two such macros may be stored at the same time, with the total length of 64 keypresses (by default).
149
150 To enable them, first add a new element to the `planck_keycodes` enum -- `DYNAMIC_MACRO_RANGE`:
151
152     enum planck_keycodes {
153       QWERTY = SAFE_RANGE,
154       COLEMAK,
155       DVORAK,
156       PLOVER,
157       LOWER,
158       RAISE,
159       BACKLIT,
160       EXT_PLV,
161       DYNAMIC_MACRO_RANGE,
162     };
163
164 Afterwards create a new layer called `_DYN`:
165
166     #define _DYN 6    /* almost any other free number should be ok */
167
168 Below these two modifications include the `dynamic_macro.h` header:
169
170     #include "dynamic_macro.h"`
171
172 Then define the `_DYN` layer with the following keys: `DYN_REC_START1`, `DYN_MACRO_PLAY1`,`DYN_REC_START2` and `DYN_MACRO_PLAY2`. It may also contain other keys, it doesn't matter apart from the fact that you won't be able to record these keys in the dynamic macros.
173
174     [_DYN]= {
175         {_______,  DYN_REC_START1, DYN_MACRO_PLAY1, _______, _______, _______, _______, _______, _______, _______, _______, _______},
176         {_______,  DYN_REC_START2, DYN_MACRO_PLAY2, _______, _______, _______, _______, _______, _______, _______, _______, _______},
177         {_______,  _______,        _______,         _______, _______, _______, _______, _______, _______, _______, _______, _______},
178         {_______,  _______,        _______,         _______, _______, _______, _______, _______, _______, _______, _______, _______}
179     },
180
181 Add the following code to the very beginning of your `process_record_user()` function:
182
183     if (!process_record_dynamic_macro(keycode, record)) {
184         return false;
185     }
186
187 To start recording the macro, press either `DYN_REC_START1` or `DYN_REC_START2`. To finish the recording, press the `_DYN` layer button. The handler awaits specifically for the `MO(_DYN)` keycode as the "stop signal" so please don't use any fancy ways to access this layer, use the regular `MO()` modifier. To replay the macro, press either `DYN_MACRO_PLAY1` or `DYN_MACRO_PLAY2`.
188
189 If the LED-s start blinking during the recording with each keypress, it means there is no more space for the macro in the macro buffer. To fit the macro in, either make the other macro shorter (they share the same buffer) or increase the buffer size by setting the `DYNAMIC_MACRO_SIZE` preprocessor macro (default value: 128; please read the comments for it in the header).
190
191 For the details about the internals of the dynamic macros, please read the comments in the `dynamic_macro.h` header.