]> git.donarmstrong.com Git - qmk_firmware.git/blob - Keymap.md
Removing eltang as he doesn't show up in the current git blame.
[qmk_firmware.git] / Keymap.md
1 # Keymap Overview
2
3 QMK keymaps are defined inside a C source file. The data structure is an array of arrays. The outer array is a list of layer arrays while the inner layer array is a list of keys. Most keyboards define a `KEYMAP()` macro to help you create this array of arrays.
4
5 ## Anatomy Of A `keymap.c`
6
7 For this example we will walk through the [default Clueboard keymap](https://github.com/qmk/qmk_firmware/blob/master/keyboards/clueboard/keymaps/default/keymap.c). You'll find it helpful to open that file in another browser window so you can look at everything in context.
8
9 There are 3 main sections of a `keymap.c` file you'll want to concern yourself with:
10
11 * [The Definitions](#definitions)
12 * [The Layer/Keymap Datastructure](#layers-and-keymaps)
13 * [Custom Functions](#custom-functions), if any
14
15 ### Definitions
16
17 At the top of the file you'll find this:
18
19     #include "clueboard.h"
20
21     // Helpful defines
22     #define GRAVE_MODS  (MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT)|MOD_BIT(KC_LGUI)|MOD_BIT(KC_RGUI)|MOD_BIT(KC_LALT)|MOD_BIT(KC_RALT))
23     #define _______ KC_TRNS
24
25     // Each layer gets a name for readability.
26     // The underscores don't mean anything - you can
27     // have a layer called STUFF or any other name.
28     // Layer names don't all need to be of the same 
29     // length, and you can also skip them entirely
30     // and just use numbers.
31     #define _BL 0
32     #define _FL 1
33     #define _CL 2
34
35 These are some handy definitions we can use when building our keymap and our custom function. The `GRAVE_MODS` definition will be used later in our custom function. The `_______` define makes it easier to see what keys a layer is overriding, while the `_BL`, `_FL`, and `_CL` defines make it easier to refer to each of our layers.
36
37 ### Layers and Keymaps
38
39 The main part of this file is the `keymaps[]` definition. This is where you list your layers and the contents of those layers. This part of the file begins with this definition:
40
41     const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
42
43 After this you'll find a list of KEYMAP() macros. A KEYMAP() is simply a list of keys to define a single layer. Typically you'll have one or more "base layers" (such as QWERTY, Dvorak, or Colemak) and then you'll layer on top of that one or more "function" layers. Due to the way layers are processed you can't overlay a "lower" layer on top of a "higher" layer. 
44
45 #### Base Layer
46
47 Here is an example of the Clueboard's base layer:
48
49       /* Keymap _BL: Base Layer (Default Layer)
50        */
51     [_BL] = KEYMAP(
52       F(0),    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_GRV,  KC_BSPC,          KC_PGUP, \
53       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_BSLS,                   KC_PGDN, \
54       KC_CAPS, KC_A,    KC_S,   KC_D,   KC_F,   KC_G,   KC_H,   KC_J,   KC_K,   KC_L,    KC_SCLN,  KC_QUOT,  KC_NUHS,  KC_ENT,                             \
55       KC_LSFT, KC_NUBS, KC_Z,   KC_X,   KC_C,   KC_V,   KC_B,   KC_N,   KC_M,   KC_COMM, KC_DOT,   KC_SLSH,  KC_RO,    KC_RSFT,          KC_UP,            \
56       KC_LCTL, KC_LGUI, KC_LALT, KC_MHEN,          KC_SPC,KC_SPC,                        KC_HENK,  KC_RALT,  KC_RCTL,  MO(_FL), KC_LEFT, KC_DOWN, KC_RGHT),
57
58 Some interesting things to note about this:
59
60 * From a C source point of view it's only a single array, but we have embedded whitespace to more easily visualize where each key is on the physical device.
61 * Plain keyboard scancodes are prefixed with KC_, while "special" keys are not.
62 * The upper left key activates custom function 0 (`F(0)`)
63 * The "Fn" key is defined with `MO(_FL)`, which moves to the `_FL` layer while that key is being held down.
64
65 #### Function Overlay Layer
66
67 Our function layer is, from a code point of view, no different from the base layer. Conceptually, however, you will build that layer as an overlay, not a replacement. For many people this distinction does not matter, but as you build more complicated layering setups it matters more and more.
68
69     [_FL] = KEYMAP(
70       KC_GRV,  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_DEL,           BL_STEP, \
71       _______, _______, _______,_______,_______,_______,_______,_______,KC_PSCR,KC_SLCK, KC_PAUS,  _______,  _______,  _______,                   _______, \
72       _______, _______, MO(_CL),_______,_______,_______,_______,_______,_______,_______, _______,  _______,  _______,  _______,                           \
73       _______, _______, _______,_______,_______,_______,_______,_______,_______,_______, _______,  _______,  _______,  _______,          KC_PGUP,         \
74       _______, _______, _______, _______,        _______,_______,                        _______,  _______,  _______,  MO(_FL), KC_HOME, KC_PGDN, KC_END),
75
76 Some interesting things to note:
77
78 * We have used our `_______` definition to turn `KC_TRNS` into `_______`. This makes it easier to spot the keys that have changed on this layer.
79 * While in this layer if you press one of the `_______` keys it will activate the key in the next lowest active layer. 
80
81 ### Custom Functions
82
83 At the bottom of the file we've defined a single custom function. This function defines a key that sends `KC_ESC` when pressed without modifiers and `KC_GRAVE` when modifiers are held. There are a couple pieces that need to be in place for this to work, and we will go over both of them. 
84
85 #### `fn_actions[]`
86
87 We define the `fn_actions[]` array to point to custom functions. `F(N)` in a keymap will call element N of that array. For the Clueboard's that looks like this:
88
89     const uint16_t PROGMEM fn_actions[] = {
90       [0] = ACTION_FUNCTION(0),  // Calls action_function()
91     };
92
93 In this case we've instructed QMK to call the `ACTION_FUNCTION` callback, which we will define in the next section.
94
95 #### `action_function()`
96
97 To actually handle the keypress event we define an `action_function()`. This function will be called when the key is pressed, and then again when the key is released. We have to handle both situations within our code, as well as determining whether to send/release `KC_ESC` or `KC_GRAVE`.
98
99     void action_function(keyrecord_t *record, uint8_t id, uint8_t opt) {
100       static uint8_t mods_pressed;
101
102       switch (id) {
103         case 0:
104           /* Handle the combined Grave/Esc key
105            */
106           mods_pressed = get_mods()&GRAVE_MODS; // Check to see what mods are pressed
107
108           if (record->event.pressed) {
109             /* The key is being pressed.
110              */
111             if (mods_pressed) {
112               add_key(KC_GRV);
113               send_keyboard_report();
114             } else {
115               add_key(KC_ESC);
116               send_keyboard_report();
117             }
118           } else {
119             /* The key is being released.
120              */
121             if (mods_pressed) {
122               del_key(KC_GRV);
123               send_keyboard_report();
124             } else {
125               del_key(KC_ESC);
126               send_keyboard_report();
127             }
128           }
129           break;
130       }
131     }
132
133 # Nitty Gritty Details
134
135 This should have given you a basic overview for creating your own keymap. For more details see the following resources:
136
137 * https://github.com/qmk/qmk_firmware/blob/master/doc/keymap.md
138 * https://github.com/qmk/qmk_firmware/wiki/Keycodes
139 * https://github.com/qmk/qmk_firmware/wiki/FAQ-Keymap
140 * https://github.com/qmk/qmk_firmware/wiki/Keymap-examples
141
142 We are actively working to improve these docs. If you have suggestions for how they could be made better please [file an issue](https://github.com/qmk/qmk_firmware/issues/new)!