1 # Macro shortcuts: Send a whole string when pressing just one key
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).
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.
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
18 A macro can include the following commands:
20 * I() change interval of stroke in milliseconds.
23 * T() type key(press and release).
24 * W() wait (milliseconds).
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.
29 Note: Using macros to have your keyboard send passwords for you is possible, but a bad idea.
31 ## Advanced macro functions
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:
35 * `record->event.pressed`
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
40 if (record->event.pressed) {
47 * `register_code(<kc>);`
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`.
51 * `unregister_code(<kc>);`
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.
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.
61 This will turn off the layer `<n>`.
65 This will clear all mods and keys currently pressed.
69 This will clear all mods currently pressed.
71 * `clear_keyboard_but_mods();`
73 This will clear all keys besides the mods currently pressed.
75 * `update_tri_layer(layer_1, layer_2, layer_3);`
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).
79 ### Naming your macros
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:
94 As was done on the [Planck default keymap](https://github.com/qmk/qmk_firmware/blob/master/keyboards/planck/keymaps/default/keymap.c#L33-L40)
96 #### Timer functionality
98 It's possible to start timers and read values for time-specific events - here's an example:
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
106 // do something if 100ms or more have passed
110 It's best to declare the `static uint16_t key_timer;` outside of the macro block (top of file, etc).
112 ### Example: Single-key copy/paste (hold to copy, tap to paste)
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?
120 static uint16_t key_timer;
122 const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
126 if (record->event.pressed) {
127 key_timer = timer_read(); // if the key is being pressed, we start the timer.
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 );
134 return MACRO( D(LCTL), T(V), U(LCTL), END );
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.
146 # Dynamic macros: record and replay macros in runtime
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).
150 To enable them, first add a new element to the `planck_keycodes` enum -- `DYNAMIC_MACRO_RANGE`:
152 enum planck_keycodes {
164 Afterwards create a new layer called `_DYN`:
166 #define _DYN 6 /* almost any other free number should be ok */
168 Below these two modifications include the `dynamic_macro.h` header:
170 #include "dynamic_macro.h"`
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.
175 {_______, DYN_REC_START1, DYN_MACRO_PLAY1, _______, _______, _______, _______, _______, _______, _______, _______, _______},
176 {_______, DYN_REC_START2, DYN_MACRO_PLAY2, _______, _______, _______, _______, _______, _______, _______, _______, _______},
177 {_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______},
178 {_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______}
181 Add the following code to the very beginning of your `process_record_user()` function:
183 if (!process_record_dynamic_macro(keycode, record)) {
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`.
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).
191 For the details about the internals of the dynamic macros, please read the comments in the `dynamic_macro.h` header.