aboutsummaryrefslogtreecommitdiff
path: root/docs/Macros.md
diff options
context:
space:
mode:
Diffstat (limited to 'docs/Macros.md')
-rw-r--r--docs/Macros.md197
1 files changed, 197 insertions, 0 deletions
diff --git a/docs/Macros.md b/docs/Macros.md
new file mode 100644
index 000000000..78290bbf6
--- /dev/null
+++ b/docs/Macros.md
@@ -0,0 +1,197 @@
1# Macro shortcuts: Send a whole string when pressing just one key
2
3Instead 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
6const 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```
18A 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
27So 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
29Note: Using macros to have your keyboard send passwords for you is possible, but a bad idea.
30
31## Advanced macro functions
32
33To 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
37This 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
40if (record->event.pressed) {
41 // on keydown
42} else {
43 // on keyup
44}
45```
46
47* `register_code(<kc>);`
48
49This 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
53Parallel 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
57This 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
61This will turn off the layer `<n>`.
62
63* `clear_keyboard();`
64
65This will clear all mods and keys currently pressed.
66
67* `clear_mods();`
68
69This will clear all mods currently pressed.
70
71* `clear_keyboard_but_mods();`
72
73This will clear all keys besides the mods currently pressed.
74
75* `update_tri_layer(layer_1, layer_2, layer_3);`
76
77If 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
81If 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
94As was done on the [Planck default keymap](https://github.com/qmk/qmk_firmware/blob/master/keyboards/planck/keymaps/default/keymap.c#L33-L40)
95
96#### Timer functionality
97
98It's possible to start timers and read values for time-specific events - here's an example:
99
100```c
101static uint16_t key_timer;
102key_timer = timer_read();
103if (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
110It'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
114With 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
116Here's what you do:
117
118
119```
120static uint16_t key_timer;
121
122const 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
144And 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
148In 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
150To 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
164It must be the last element because `dynamic_macros.h` will add some more keycodes after it.
165
166Below it include the `dynamic_macro.h` header:
167
168 #include "dynamic_macro.h"`
169
170Add the following keys to your keymap:
171
172- `DYN_REC_START1` — start recording the macro 1,
173- `DYN_REC_START2` — start recording the macro 2,
174- `DYN_MACRO_PLAY1` — replay the macro 1,
175- `DYN_MACRO_PLAY2` — replay the macro 2,
176- `DYN_REC_STOP` — finish the macro that is currently being recorded.
177
178Add the following code to the very beginning of your `process_record_user()` function:
179
180 if (!process_record_dynamic_macro(keycode, record)) {
181 return false;
182 }
183
184That should be everything necessary. To start recording the macro, press either `DYN_REC_START1` or `DYN_REC_START2`. To finish the recording, press the `DYN_REC_STOP` layer button. To replay the macro, press either `DYN_MACRO_PLAY1` or `DYN_MACRO_PLAY2`.
185
186Note that it's possible to replay a macro as part of a macro. It's ok to replay macro 2 while recording macro 1 and vice versa but never create recursive macros i.e. macro 1 that replays macro 1. If you do so and the keyboard will get unresponsive, unplug the keyboard and plug it again.
187
188For users of the earlier versions of dynamic macros: It is still possible to finish the macro recording using just the layer modifier used to access the dynamic macro keys, without a dedicated `DYN_REC_STOP` key. If you want this behavior back, use the following snippet instead of the one above:
189
190 uint16_t macro_kc = (keycode == MO(_DYN) ? DYN_REC_STOP : keycode);
191 if (!process_record_dynamic_macro(macro_kc, record)) {
192 return false;
193 }
194
195If 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).
196
197For the details about the internals of the dynamic macros, please read the comments in the `dynamic_macro.h` header.