aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/keymap.md232
1 files changed, 143 insertions, 89 deletions
diff --git a/doc/keymap.md b/doc/keymap.md
index 6f2a663fc..c127db2b2 100644
--- a/doc/keymap.md
+++ b/doc/keymap.md
@@ -1,15 +1,30 @@
1Keymap framework - how to define your keymap 1Keymap framework - how to define your keymap
2============================================ 2============================================
3***NOTE: This is not final version, may be inconsistent with source code and changed occasionally for a while.*** 3***NOTE: This is updated for QMK but this is still work in progress. This may still be inconsistent with the source code.***
4
5QMK is based on TMK. Understanding the essential changes made should help you understand variable names etc.
6
7## TMK vs. QMK
8
9| Firmware |TMK |QMK |
10|-------------------------------------------------|-----------------------|-------------------------|
11| Maintainer |hasu |Jack Humbert et al. |
12| Build path customization | `TMK_DIR = ...` | `include .../Makefile` |
13| `keymaps` data | 3D array of `uint8_t` holding **keycode** | 3D array of `uint16_t` holding **action code** |
14| `fn_actions` data | 1D array of `uint16_t` holding **action code** | 3D array of `uint16_t` holding **action code** |
15
16Since QMK is based on TMK and uses major portion of TMK code as is, understanding the essential changes made should help you understand the code.
4 17
5## 0. Keymap and layers 18## 0. Keymap and layers
6**Keymap** is comprised of multiple layers of key layout, you can define **32 layers** at most. 19In QMK, **`const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS]`** holds multiple **layers** of keymap information in **16 bit** data holding the **action code**. You can define **32 layers** at most.
7**Layer** is an array of **keycodes** to define **actions** for each physical keys.
8respective layers can be validated simultaneously. Layers are indexed with 0 to 31 and higher layer has precedence.
9 20
10 Keymap: 32 Layers Layer: Keycode matrix 21For trivial key definitions, the higher 8 bits of the **action code** are all 0 and the lower 8 bits holds the USB HID usage code generated by the key as **keycode**.
22
23Respective layers can be validated simultaneously. Layers are indexed with 0 to 31 and higher layer has precedence.
24
25 Keymap: 32 Layers Layer: action code matrix
11 ----------------- --------------------- 26 ----------------- ---------------------
12 stack of layers array_of_keycode[row][column] 27 stack of layers array_of_action_code[row][column]
13 ____________ precedence _______________________ 28 ____________ precedence _______________________
14 / / | high / ESC / F1 / F2 / F3 .... 29 / / | high / ESC / F1 / F2 / F3 ....
15 31 /___________// | /-----/-----/-----/----- 30 31 /___________// | /-----/-----/-----/-----
@@ -22,11 +37,15 @@ respective layers can be validated simultaneously. Layers are indexed with 0 to
22 0 /___________/ V low 0 `-------------------------- 37 0 /___________/ V low 0 `--------------------------
23 38
24 39
40Sometimes, the action code stored in keymap may be referred as keycode in some documents due to the TMK history.
41
42### 0.1 Keymap layer status
43Keymap layer has its state in two 32 bit parameters:
25 44
26### 0.1 Keymap status 45* **`default_layer_state`** indicates a base keymap layer(0-31) which is always valid and to be referred.
27Keymap has its state in two parameters: 46* **`layer_state`** () has current on/off status of the layer on its each bit.
28**`default_layer`** indicates a base keymap layer(0-31) which is always valid and to be referred, **`keymap_stat`** is 16bit variable which has current on/off status of layers on its each bit.
29 47
48Keymap has its state in two parameter**`default_layer`** indicates a base keymap layer(0-31) which is always valid and to be referred, **`keymap_stat`** is 16bit variable which has current on/off status of layers on its each bit.
30Keymap layer '0' is usually `default_layer` and which is the only valid layer and other layers is initially off after boot up firmware, though, you can configured them in `config.h`. 49Keymap layer '0' is usually `default_layer` and which is the only valid layer and other layers is initially off after boot up firmware, though, you can configured them in `config.h`.
31To change `default_layer` will be useful when you switch key layout completely, say you want Colmak instead of Qwerty. 50To change `default_layer` will be useful when you switch key layout completely, say you want Colmak instead of Qwerty.
32 51
@@ -67,97 +86,132 @@ On the other hand, you shall change `layer_state` to overlay base layer with som
67### 0.2 Layer Precedence and Transparency 86### 0.2 Layer Precedence and Transparency
68Note that ***higher layer has higher priority on stack of layers***, namely firmware falls down from top layer to bottom to look up keycode. Once it spots keycode other than **`KC_TRNS`**(transparent) on a layer it stops searching and lower layers aren't referred. 87Note that ***higher layer has higher priority on stack of layers***, namely firmware falls down from top layer to bottom to look up keycode. Once it spots keycode other than **`KC_TRNS`**(transparent) on a layer it stops searching and lower layers aren't referred.
69 88
70You can place `KC_TRNS` on overlay layer changes just part of layout to fall back on lower or base layer. 89You can place `KC_TRANS` on overlay layer changes just part of layout to fall back on lower or base layer.
71Key with `KC_TRANS` doesn't has its own keycode and refers to lower valid layers for keycode, instead. 90Key with `KC_TRANS` (`KC_TRNS` and `_______` are the alias) doesn't has its own keycode and refers to lower valid layers for keycode, instead.
72See example below. 91See example below.
73 92
74 93
75### 0.3 Keymap Example 94### 0.3 Keymap Example
76Keymap is **`keymaps[]`** C array in fact and you can define layers in it with **`KEYMAP()`** C macro and keycodes. To use complex actions you need to define `Fn` keycode in **`fn_actions[]`** array. 95Keymap in this QMK is **`static const uint16_t PROGMEM keymaps[]`** C array in fact and you can define layers in it with **`KEYMAP()`** C macro and keycodes. To use complex actions you need to define `Fn` keycode in **`fn_actions[]`** array. It holds the 16 bit quantum keycode (action code).
96
97> Please note that keymap in the TMK, which QMK was forked from, is **`static const uint8_t PROGMEM keymaps[]`** C array which holds the 8 bit keycode (~USB HID usage code).
77 98
78This is a keymap example for [HHKB](http://en.wikipedia.org/wiki/Happy_Hacking_Keyboard) keyboard. 99This is a keymap example for [HHKB](http://en.wikipedia.org/wiki/Happy_Hacking_Keyboard) keyboard.
79This example has three layers, 'Qwerty' as base layer, 'Cursor' and 'Mousekey'. 100This example has three layers, 'QWERTY' as base layer, 'FN' and 'MOUSE'.
101
80In this example, 102In this example,
81 103
82 `Fn0` is a **momentary layer switching** key, you can use keys on Cursor layer while holding the key. 104 `MO(layer)` is a **momentary layer switching** key.
83 105
84 `Fn1` is a momentary layer switching key with tapping feature, you can get semicolon **';'** with taping the key and switch layers while holding the key. The word **'tap'** or **'tapping'** mean to press and release a key quickly. 106You can find other keymap definitions in file `keymap.c` located on project directories.
107```
108/*
109 * dbroqua HHKB Layout
110 */
111#include "hhkb.h"
112
113#define BASE 0
114#define FN 1
115#define MOUSE 2
116
117const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
118 /* BASE Level: Default Layer
119 * ,-----------------------------------------------------------------------------------------.
120 * | Esc | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 0 | - | = | \ | ` |
121 * |-----------------------------------------------------------------------------------------+
122 * | Tab | Q | W | E | R | T | Y | U | I | O | P | [ | ] | Bksp |
123 * |-----------------------------------------------------------------------------------------+
124 * | Ctrl | A | S | D | F | G | H | J | K | L | ; | ' | Enter |
125 * |-----------------------------------------------------------------------------------------+
126 * | Shift | Z | X | C | V | B | N | M | , | . | / | Shift | fn |
127 * +-----------------------------------------------------------------------------------------+
128 * | Gui | Alt | Space | AltGr |Mouse|
129 * `----------------------------------------------------------------´
130 */
131 [BASE] = KEYMAP(
132 KC_ESC, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSLS, KC_GRV, \
133 KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSPC, \
134 KC_LCTL, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, \
135 KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, MO(FN), \
136 KC_LGUI, KC_LALT, /* */ KC_SPC, KC_RALT, MO(MOUSE)
137 ),
138
139 /* FN Layer
140 * ,-----------------------------------------------------------------------------------------.
141 * | Pwr | F1 | F2 | F3 | F4 | F5 | F6 | F7 | F8 | F9 | F0 | F11 | F12 | Ins | Del|
142 * |-----------------------------------------------------------------------------------------+
143 * | Caps | | | | | | | |PrtSc| Slck| Paus| Up | | |
144 * |-----------------------------------------------------------------------------------------+
145 * | | Vol-| Vol+| Mute| | | * | / | Home| PgUp| Left |Right| |
146 * |-----------------------------------------------------------------------------------------+
147 * | | Prev| Play| Next| | | + | - | End |PgDwn| Down| | |
148 * +-----------------------------------------------------------------------------------------+
149 * | | | | Stop | |
150 * `----------------------------------------------------------------´
151 */
152 [FN] = KEYMAP(
153 KC_PWR, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_INS, KC_DEL, \
154 KC_CAPS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_PSCR, KC_SLCK, KC_PAUS, KC_UP, KC_TRNS, KC_TRNS, \
155 KC_TRNS, KC_VOLD, KC_VOLU, KC_MUTE, KC_TRNS, KC_TRNS, KC_PAST, KC_PSLS, KC_HOME, KC_PGUP, KC_LEFT, KC_RGHT, KC_TRNS, \
156 KC_TRNS, KC_MPRV, KC_MPLY, KC_MNXT, KC_TRNS, KC_TRNS, KC_PPLS, KC_PMNS, KC_END, KC_PGDN, KC_DOWN, KC_TRNS, KC_TRNS, \
157 KC_TRNS, KC_TRNS, KC_TRNS, KC_MSTP, KC_TRNS
158 ),
159
160 /* MOUSE Layer
161 * ,-----------------------------------------------------------------------------------------.
162 * | | | | | | | | | | | | | | | |
163 * |-----------------------------------------------------------------------------------------+
164 * | | | WUp | | | | | | | | Btn1| Up | Btn2| |
165 * |-----------------------------------------------------------------------------------------+
166 * | | WLt | WDn | WRt | | | | | | | Left |Right| |
167 * |-----------------------------------------------------------------------------------------+
168 * | | | | | | | | | | Btn3| Down| | |
169 * +-----------------------------------------------------------------------------------------+
170 * | | | | | |
171 * `----------------------------------------------------------------´
172 */
173 [MOUSE] = KEYMAP(
174 KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, \
175 KC_TRNS, KC_TRNS, KC_WH_U, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_BTN1, KC_MS_U, KC_BTN2, KC_TRNS, \
176 KC_TRNS, KC_WH_L, KC_WH_D, KC_WH_R, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_MS_L, KC_MS_R, KC_TRNS, \
177 KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_BTN3, KC_MS_D, KC_TRNS, KC_TRNS, \
178 KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS
179 )
180};
85 181
86 `Fn2` is a **toggle layer switch** key, you can stay switched layer after releasing the key unlike momentary switching. 182const uint16_t PROGMEM fn_actions[] = {
87 183
88You can find other keymap definitions in file `keymap.c` located on project directories. 184};
89 185
90 static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { 186const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
91 /* 0: Qwerty 187{
92 * ,-----------------------------------------------------------. 188 // MACRODOWN only works in this function
93 * |Esc| 1| 2| 3| 4| 5| 6| 7| 8| 9| 0| -| =| \| `| 189 switch(id) {
94 * |-----------------------------------------------------------| 190 case 0:
95 * |Tab | Q| W| E| R| T| Y| U| I| O| P| [| ]|Backs| 191 if (record->event.pressed) {
96 * |-----------------------------------------------------------| 192 register_code(KC_RSFT);
97 * |Contro| A| S| D| F| G| H| J| K| L|Fn1| '|Enter | 193 } else {
98 * |-----------------------------------------------------------| 194 unregister_code(KC_RSFT);
99 * |Shift | Z| X| C| V| B| N| M| ,| .| /|Shift |Fn0| 195 }
100 * `-----------------------------------------------------------' 196 break;
101 * |Gui|Alt |Space |Alt |Fn2| 197 }
102 * `-------------------------------------------' 198 return MACRO_NONE;
103 */ 199};
104 KEYMAP(ESC, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, MINS,EQL, BSLS,GRV, \ 200```
105 TAB, Q, W, E, R, T, Y, U, I, O, P, LBRC,RBRC,BSPC, \
106 LCTL,A, S, D, F, G, H, J, K, L, FN1, QUOT,ENT, \
107 LSFT,Z, X, C, V, B, N, M, COMM,DOT, SLSH,RSFT,FN0, \
108 LGUI,LALT, SPC, RALT,FN2),
109 /* 1: Cursor(HHKB mode)
110 * ,-----------------------------------------------------------.
111 * |Pwr| F1| F2| F3| F4| F5| F6| F7| F8| F9|F10|F11|F12|Ins|Del|
112 * |-----------------------------------------------------------|
113 * |Caps | | | | | | | |Psc|Slk|Pus|Up | |Backs|
114 * |-----------------------------------------------------------|
115 * |Contro|VoD|VoU|Mut| | | *| /|Hom|PgU|Lef|Rig|Enter |
116 * |-----------------------------------------------------------|
117 * |Shift | | | | | | +| -|End|PgD|Dow|Shift | |
118 * `-----------------------------------------------------------'
119 * |Gui |Alt |Space |Alt |Gui|
120 * `--------------------------------------------'
121 */
122 KEYMAP(PWR, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, INS, DEL, \
123 CAPS,TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,PSCR,SLCK,PAUS,UP, TRNS,BSPC, \
124 LCTL,VOLD,VOLU,MUTE,TRNS,TRNS,PAST,PSLS,HOME,PGUP,LEFT,RGHT,ENT, \
125 LSFT,TRNS,TRNS,TRNS,TRNS,TRNS,PPLS,PMNS,END, PGDN,DOWN,RSFT,TRNS, \
126 LGUI,LALT, SPC, RALT,RGUI),
127 /* 2: Mousekey
128 * ,-----------------------------------------------------------.
129 * |Esc| F1| F2| F3| F4| F5| F6| F7| F8| F9|F10|F11|F12|Ins|Del|
130 * |-----------------------------------------------------------|
131 * |Tab | | | | | |MwL|MwD|MwU|MwR| | | |Backs|
132 * |-----------------------------------------------------------|
133 * |Contro| | | | | |McL|McD|McU|McR| | |Return |
134 * |-----------------------------------------------------------|
135 * |Shift | | | | |Mb3|Mb2|Mb1|Mb4|Mb5| |Shift | |
136 * `-----------------------------------------------------------'
137 * |Gui |Alt |Mb1 |Alt | |
138 * `--------------------------------------------'
139 * Mc: Mouse Cursor / Mb: Mouse Button / Mw: Mouse Wheel
140 */
141 KEYMAP(ESC, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, INS, DEL, \
142 TAB, TRNS,TRNS,TRNS,TRNS,TRNS,WH_L,WH_D,WH_U,WH_R,TRNS,TRNS,TRNS,BSPC, \
143 LCTL,TRNS,ACL0,ACL1,ACL2,TRNS,MS_L,MS_D,MS_U,MS_R,TRNS,QUOT,ENT, \
144 LSFT,TRNS,TRNS,TRNS,TRNS,BTN3,BTN2,BTN1,BTN4,BTN5,SLSH,RSFT,TRNS, \
145 LGUI,LALT, BTN1, RALT,TRNS),
146 };
147 201
148 static const uint16_t PROGMEM fn_actions[] = {
149 ACTION_LAYER_MOMENTARY(1), // FN0
150 ACTION_LAYER_TAP_KEY(2, KC_SCLN), // FN1
151 ACTION_LAYER_TOGGLE(2), // FN2
152 };
153 202
203## 1. Keycode
204See [`tmk_core/common/keycode.h`](../tmk_core/common/keycode.h) or keycode table below for the detail. Keycode is internal **8bit code** to indicate action performed on key in keymap. Keycodes are based on [HID Usage Keyboard/Keypad Page(0x07)](http://www.usb.org/developers/hidpage/Hut1_12v2.pdf) plus special codes in the `0xA5-DF` range.
205
206Keycode has `KC_` prefixed symbol respectively. Most of keycodes like `KC_A` have simple action registers key to host on press and unregister on release, while some of other keycodes has some special actions like `Fn` keys, Media control keys, System control keys and Mousekeys.
154 207
208keymaps[]
155 209
210In `KEYMAP()` macro, TMK recommends you to keep prefix part `KC_` of keycode to keep keymap compact. For example, just use `A` instead you place `KC_A` in `KEYMAP()`. But this doesn't apply for QMK.
156 211
157## 1. Keycode 212The `KEYMAP()` macro defines correspondence between the physical key location to the electrical key connection.
158See [`common/keycode.h`](../common/keycode.h) or keycode table below for the detail. Keycode is internal **8bit code** to indicate action performed on key in keymap. Keycode has `KC_` prefixed symbol respectively. Most of keycodes like `KC_A` have simple action registers key to host on press and unregister on release, while some of other keycodes has some special actions like `Fn` keys, Media control keys, System control keys and Mousekeys.
159 213
160 ***In `KEYMAP()` macro you should omit prefix part `KC_` of keycode to keep keymap compact.*** For example, just use `A` instead you place `KC_A` in `KEYMAP()`. Some keycodes has 4-letter **short name** in addition to descriptive name, you'll prefer short one in `KEYMAP()`. 214Some keycodes has 7-letter **short name** such as `KC_COMM` in addition to descriptive name `KC_COMMA`, you'll prefer short one in `KEYMAP()`.
161 215
162### 1.0 Other key 216### 1.0 Other key
163- `KC_NO` for no action 217- `KC_NO` for no action
@@ -192,7 +246,10 @@ There are 8 modifiers which has discrimination between left and right.
192- `KC_WSCH`, `KC_WHOM`, `KC_WBAK`, `KC_WFWD`, `KC_WSTP`, `KC_WREF`, `KC_WFAV` for web browser operation 246- `KC_WSCH`, `KC_WHOM`, `KC_WBAK`, `KC_WFWD`, `KC_WSTP`, `KC_WREF`, `KC_WFAV` for web browser operation
193 247
194### 1.5 Fn key 248### 1.5 Fn key
195`KC_FNnn` are keycodes for `Fn` key which not given any actions at the beginning unlike most of keycodes has its own inborn action. To use these keycodes in `KEYMAP()` you need to assign action you want at first. Action of `Fn` key is defined in `fn_actions[]` and its index of the array is identical with number part of `KC_FNnn`. Thus `KC_FN0` keycode indicates the action defined in first element of the array. ***32 `Fn` keys can be defined at most.*** 249You don't need to use this functionality under QMK since this is a backward compatibility functionality. Unlike TMK, you can write action code itself directly in **`static const uint16_t PROGMEM keymaps[]`** C array using `MO(layer)`, etc.
250
251`KC_FNnn` are keycodes for `Fn` key which not given any actions at the beginning unlike most of keycodes has its own inborn action. To use these keycodes in `KEYMAP()` you need to assign action you want at first. Action of `Fn` key is defined in `fn_actions[]` and its index of the array is identical with number part of `KC_FNnn`. Thus `KC_FN0` keycode indicates the action defined in first element of the array. ***Only 32 `Fn` keys can be defined at most.***
252
196 253
197### 1.6 Keycode Table 254### 1.6 Keycode Table
198 See keycode table in [`doc/keycode.txt`](./keycode.txt) for description of keycodes. 255 See keycode table in [`doc/keycode.txt`](./keycode.txt) for description of keycodes.
@@ -581,15 +638,11 @@ Similar to layer tap toggle, this works as a momentary modifier when holding, bu
581 638
582 639
583## 5. Legacy Keymap 640## 5. Legacy Keymap
584This was used in prior version and still works due to legacy support code in `common/keymap.c`. Legacy keymap doesn't support many of features that new keymap offers. ***It is not recommended to use Legacy Keymap for new project.*** 641In QMK, `tmk_core/common/keymap.c` is missing and its replacement `quantum/keymap_common.c` lacks Legacy Keymap support.
585
586To enable Legacy Keymap support define this macro in `config.h`.
587
588 #define USE_LEGACY_KEYMAP
589 642
590Legacy Keymap uses two arrays `fn_layer[]` and `fn_keycode[]` to define Fn key. The index of arrays corresponds with postfix number of `Fn` key. Array `fn_layer[]` indicates destination layer to switch and `fn_keycode[]` has keycodes to send when tapping `Fn` key. 643Legacy Keymap uses two arrays `fn_layer[]` and `fn_keycode[]` to define Fn key. The index of arrays corresponds with postfix number of `Fn` key. Array `fn_layer[]` indicates destination layer to switch and `fn_keycode[]` has keycodes to send when tapping `Fn` key.
591 644
592In following setting example, `Fn0`, `Fn1` and `Fn2` switch layer to 1, 2 and 2 respectively. `Fn2` registers `Space` key when tapping while `Fn0` and `Fn1` doesn't send any key. 645In the following legacy keymap setting example, `Fn0`, `Fn1` and `Fn2` switch layer to 1, 2 and 2 respectively. `Fn2` registers `Space` key when tapping while `Fn0` and `Fn1` doesn't send any key.
593 646
594 static const uint8_t PROGMEM fn_layer[] = { 647 static const uint8_t PROGMEM fn_layer[] = {
595 1, // Fn0 648 1, // Fn0
@@ -603,6 +656,7 @@ In following setting example, `Fn0`, `Fn1` and `Fn2` switch layer to 1, 2 and 2
603 KC_SPC, // Fn2 656 KC_SPC, // Fn2
604 }; 657 };
605 658
659Under QMK, these can be realized using action code ACTION_LAYER_TAP_KEY(1, KC_NO), ACTION_LAYER_TAP_KEY(2, KC_NO), and ACTION_LAYER_TAP_KEY(2, KC_SPC) in the `keymaps` directly.
606 660
607## 6. Terminology 661## 6. Terminology
608***TBD*** 662***TBD***