Fix up tap_code functionality (#4609)
[jackhill/qmk/firmware.git] / docs / feature_dynamic_macros.md
CommitLineData
7b0356d1 1# Dynamic Macros: Record and Replay Macros in Runtime
a0ac0d3c 2
3276c4c5 3QMK supports temporary macros created on the fly. We call these Dynamic Macros. They are defined by the user from the keyboard and are lost when the keyboard is unplugged or otherwise rebooted.
a0ac0d3c 4
5You can store one or two macros and they may have a combined total of 128 keypresses. You can increase this size at the cost of RAM.
6
6caea0ac 7To enable them, first add a new element to the end of your `keycodes` enum — `DYNAMIC_MACRO_RANGE`:
a0ac0d3c 8
9```c
6caea0ac 10enum keycodes {
a0ac0d3c 11 QWERTY = SAFE_RANGE,
12 COLEMAK,
13 DVORAK,
14 PLOVER,
15 LOWER,
16 RAISE,
17 BACKLIT,
18 EXT_PLV,
19 DYNAMIC_MACRO_RANGE,
20};
21```
22
6caea0ac 23Your `keycodes` enum may have a slightly different name. You must add `DYNAMIC_MACRO_RANGE` as the last element because `dynamic_macros.h` will add some more keycodes after it.
a0ac0d3c 24
3276c4c5 25Below it, include the `dynamic_macro.h` header:
a0ac0d3c 26
27```c
28 #include "dynamic_macro.h"`
29```
30
31Add the following keys to your keymap:
32
33* `DYN_REC_START1` — start recording the macro 1,
34* `DYN_REC_START2` — start recording the macro 2,
35* `DYN_MACRO_PLAY1` — replay the macro 1,
36* `DYN_MACRO_PLAY2` — replay the macro 2,
37* `DYN_REC_STOP` — finish the macro that is currently being recorded.
38
39Add the following code to the very beginning of your `process_record_user()` function:
40
41```c
42 if (!process_record_dynamic_macro(keycode, record)) {
43 return false;
44 }
45```
46
47That 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`.
48
49Note 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.
50
51For 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:
52
53```c
54 uint16_t macro_kc = (keycode == MO(_DYN) ? DYN_REC_STOP : keycode);
bb53635f 55
a0ac0d3c 56 if (!process_record_dynamic_macro(macro_kc, record)) {
57 return false;
58 }
59```
60
3276c4c5 61If the LEDs 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).
a0ac0d3c 62
63For the details about the internals of the dynamic macros, please read the comments in the `dynamic_macro.h` header.