aboutsummaryrefslogtreecommitdiff
path: root/docs/feature_macros.md
diff options
context:
space:
mode:
authorskullydazed <skullydazed@users.noreply.github.com>2017-10-24 20:34:28 -0700
committerGitHub <noreply@github.com>2017-10-24 20:34:28 -0700
commit67cc5cebc0430d15169e2c649ea25112a31bfa31 (patch)
treef90f432a40b1d03ec7b9e68666c2444f2b2d0e79 /docs/feature_macros.md
parent8892c50336fe49fbd3524ed44f2139074dca5ba6 (diff)
downloadqmk_firmware-67cc5cebc0430d15169e2c649ea25112a31bfa31.tar.gz
qmk_firmware-67cc5cebc0430d15169e2c649ea25112a31bfa31.zip
Restructure the hardware and feature docs to make things easier to find (#1888)
* fix #1313 by documenting more config.h options * Clean up and organize documentation
Diffstat (limited to 'docs/feature_macros.md')
-rw-r--r--docs/feature_macros.md220
1 files changed, 220 insertions, 0 deletions
diff --git a/docs/feature_macros.md b/docs/feature_macros.md
new file mode 100644
index 000000000..66d2bc090
--- /dev/null
+++ b/docs/feature_macros.md
@@ -0,0 +1,220 @@
1# Macros
2
3Macros 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.
4
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.
7{% endhint %}
8
9## The new way: `SEND_STRING()` & `process_record_user`
10
11Sometimes 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`).
12
13For example:
14
15```c
16enum custom_keycodes {
17 PRINT_TRUTH = SAFE_RANGE
18};
19
20bool process_record_user(uint16_t keycode, keyrecord_t *record) {
21 if (record->event.pressed) {
22 switch(keycode) {
23 case PRINT_TRUTH:
24 SEND_STRING("QMK is the best thing ever!");
25 return false; break;
26 }
27 }
28 return true;
29};
30```
31
32### Tap/down/up
33
34You can send arbitary keycodes by wrapping them in:
35
36* `SS_TAP()`
37* `SS_DOWN()`
38* `SS_UP()`
39
40For example:
41
42 SEND_STRING(SS_TAP(X_HOME));
43
44Would tap `KC_HOME` - note how the prefix is now `X_`, and not `KC_`. You can also combine this with other strings, like this:
45
46 SEND_STRING("VE"SS_TAP(X_HOME)"LO");
47
48Which would send "VE" followed by a `KC_HOME` tap, and "LO" (spelling "LOVE" if on a newline).
49
50There's also a couple of mod shortcuts you can use:
51
52* `SS_LCTRL(string)`
53* `SS_LGUI(string)`
54* `SS_LALT(string)`
55
56That can be used like this:
57
58 SEND_STRING(SS_LCTRL("a"));
59
60Which would send LCTRL+a (LTRL down, a, LTRL up) - notice that they take strings (eg `"k"`), and not the `X_K` keycodes.
61
62### Alternative keymaps
63
64By 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:
65
66 #include <sendstring_colemak.h>
67
68### Strings in memory
69
70If 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:
71
72```c
73char my_str[4] = "ok.";
74send_string(my_str);
75```
76
77The shortcuts defined above won't work with `send_string()`, but you can separate things out to different lines if needed:
78
79```c
80char my_str[4] = "ok.";
81SEND_STRING("I said: ");
82send_string(my_str);
83SEND_STRING(".."SS_TAP(X_END));
84```
85
86## The old way: `MACRO()` & `action_get_macro`
87
88{% hint style='info' %}
89This is inherited from TMK, and hasn't been updated - it's recommend that you use `SEND_STRING` and `process_record_user` instead.
90{% endhint %}
91
92By default QMK assumes you don't have any macros. To define your macros you create an `action_get_macro()` function. For example:
93
94```c
95const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt) {
96 if (record->event.pressed) {
97 switch(id) {
98 case 0:
99 return MACRO(D(LSFT), T(H), U(LSFT), T(I), D(LSFT), T(1), U(LSFT), END);
100 case 1:
101 return MACRO(D(LSFT), T(B), U(LSFT), T(Y), T(E), D(LSFT), T(1), U(LSFT), END);
102 }
103 }
104 return MACRO_NONE;
105};
106```
107
108This 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:
109
110 if (!record->event.pressed) {
111
112### Macro Commands
113
114A macro can include the following commands:
115
116* I() change interval of stroke in milliseconds.
117* D() press key.
118* U() release key.
119* T() type key(press and release).
120* W() wait (milliseconds).
121* END end mark.
122
123### Mapping a Macro to a key
124
125Use the `M()` function within your `KEYMAP()` to call a macro. For example, here is the keymap for a 2-key keyboard:
126
127```c
128const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
129 [0] = KEYMAP(
130 M(0), M(1)
131 ),
132};
133
134const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt) {
135 if (record->event.pressed) {
136 switch(id) {
137 case 0:
138 return MACRO(D(LSFT), T(H), U(LSFT), T(I), D(LSFT), T(1), U(LSFT), END);
139 case 1:
140 return MACRO(D(LSFT), T(B), U(LSFT), T(Y), T(E), D(LSFT), T(1), U(LSFT), END);
141 }
142 }
143 return MACRO_NONE;
144};
145```
146
147When you press the key on the left it will type "Hi!" and when you press the key on the right it will type "Bye!".
148
149### Naming your macros
150
151If 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.
152
153```c
154#define M_HI M(0)
155#define M_BYE M(1)
156
157const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
158 [0] = KEYMAP(
159 M_HI, M_BYE
160 ),
161};
162```
163
164## Advanced macro functions
165
166There 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.
167
168### `record->event.pressed`
169
170This is a boolean value that can be tested to see if the switch is being pressed or released. An example of this is
171
172```c
173 if (record->event.pressed) {
174 // on keydown
175 } else {
176 // on keyup
177 }
178```
179
180### `register_code(<kc>);`
181
182This 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`.
183
184### `unregister_code(<kc>);`
185
186Parallel 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.
187
188### `clear_keyboard();`
189
190This will clear all mods and keys currently pressed.
191
192### `clear_mods();`
193
194This will clear all mods currently pressed.
195
196### `clear_keyboard_but_mods();`
197
198This will clear all keys besides the mods currently pressed.
199
200## Advanced Example: Single-key copy/paste
201
202This example defines a macro which sends `Ctrl-C` when pressed down, and `Ctrl-V` when released.
203
204```c
205const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt) {
206 switch(id) {
207 case 0: {
208 if (record->event.pressed) {
209 return MACRO( D(LCTL), T(C), U(LCTL), END );
210 } else {
211 return MACRO( D(LCTL), T(V), U(LCTL), END );
212 }
213 break;
214 }
215 }
216 return MACRO_NONE;
217};
218```
219
220