keyboard stuff
at master 213 lines 9.5 kB view raw view rendered
1# Supporting Your Keyboard in QMK Configurator 2 3This page covers how to properly support keyboards in the [QMK Configurator](https://config.qmk.fm/). 4 5 6## How the Configurator Understands Keyboards 7 8To understand how the Configurator understands keyboards, first one must understand layout macros. For this exercise, we're going to imagine a 17-key numpad PCB, which we're going to call `numpad`. 9 10``` 11|---------------| 12|NLk| / | * | - | 13|---+---+---+---| 14|7 |8 |9 | + | 15|---+---+---| | 16|4 |5 |6 | | 17|---+---+---+---| 18|1 |2 |3 |Ent| 19|-------+---| | 20|0 | . | | 21|---------------| 22``` 23 24::: tip 25For more on layout macros, see [Understanding QMK: Matrix Scanning](understanding_qmk#matrix-scanning) and [Understanding QMK: Matrix to Physical Layout Map](understanding_qmk#matrix-to-physical-layout-map). 26::: 27 28The Configurator's API reads the keyboard's `.h` file from `qmk_firmware/keyboards/<keyboard>/<keyboard>.h`. For our numpad, this file would be `qmk_firmware/keyboards/numpad/numpad.h`: 29 30```c 31#pragma once 32 33#define LAYOUT( \ 34 k00, k01, k02, k03, \ 35 k10, k11, k12, k13, \ 36 k20, k21, k22, \ 37 k30, k31, k32, k33, \ 38 k40, k42 \ 39 ) { \ 40 { k00, k01, k02, k03 }, \ 41 { k10, k11, k12, k13 }, \ 42 { k20, k21, k22, KC_NO }, \ 43 { k30, k31, k32, k33 }, \ 44 { k40, KC_NO, k42, KC_NO } \ 45} 46``` 47 48QMK uses `KC_NO` to designate places in the switch matrix where there is no switch. Sometimes, `XXX`, `___` or `____` are used as shorthand to make this section easier to read if it needs to be debugged. This is usually defined near the beginning of the `.h` file: 49 50```c 51#pragma once 52 53#define XXX KC_NO 54 55#define LAYOUT( \ 56 k00, k01, k02, k03, \ 57 k10, k11, k12, k13, \ 58 k20, k21, k22, \ 59 k30, k31, k32, k33, \ 60 k40, k42 \ 61 ) { \ 62 { k00, k01, k02, k03 }, \ 63 { k10, k11, k12, k13 }, \ 64 { k20, k21, k22, XXX }, \ 65 { k30, k31, k32, k33 }, \ 66 { k40, XXX, k42, XXX } \ 67} 68``` 69 70::: warning 71This usage differs from that of keymap macros, which almost always use `XXXXXXX` (seven capital X's) for `KC_NO` and `_______` (seven underscores) for `KC_TRNS`. 72::: 73 74::: warning 75To prevent user confusion, using `KC_NO` is preferred. 76::: 77 78The layout macro tells the Configurator that our keyboard has 17 keys, arranged in five rows of four columns each. Our switch positions are named `k<row><column>`, counting from 0. The names themselves actually don't matter, as long as they match between the top section, which receives the keycodes from the keymap, and the bottom half which designates where each key is in the matrix. 79 80To display our keyboard in a way that resembles the physical keyboard, we need to build a JSON file that tells the Configurator how to tie the physical locations and sizes of our keys to our switch matrix. 81 82## Building the JSON file 83 84To build the JSON file, the easiest way is to build the layout in [Keyboard Layout Editor](https://www.keyboard-layout-editor.com/) ("KLE"), from which we'll feed the Raw Data into a QMK tool that converts this data into a JSON the Configurator will read and use. Since KLE opens by default with a numpad layout, we're just going to remove the Getting Started instructions, and use what's left. 85 86Once the layout is as desired, move to the Raw Data tab in KLE, and copy the contents: 87 88``` 89["Num Lock","/","*","-"], 90["7\nHome","8\n↑","9\nPgUp",{h:2},"+"], 91["4\n←","5","6\n→"], 92["1\nEnd","2\n↓","3\nPgDn",{h:2},"Enter"], 93[{w:2},"0\nIns",".\nDel"] 94``` 95 96To convert this data into our JSON, go to the [QMK KLE-JSON Converter](https://qmk.fm/converter/), paste the Raw Data into the Input field, and click the Convert button. After a moment, our JSON data will appear in the Output field. Copy the contents to a new text document, and name the document `info.json`, saving it in the same folder that contains `numpad.h`. 97 98Use the `keyboard_name` object to set the name of the keyboard. For instruction purposes, we will put each key's object on its own line. This is only to make the file more human-readable, and does not affect the Configurator's functionality. 99 100```json 101{ 102 "keyboard_name": "Numpad", 103 "url": "", 104 "maintainer": "qmk", 105 "tags": { 106 "form_factor": "numpad" 107 }, 108 "layouts": { 109 "LAYOUT": { 110 "layout": [ 111 {"label":"Num Lock", "x":0, "y":0}, 112 {"label":"/", "x":1, "y":0}, 113 {"label":"*", "x":2, "y":0}, 114 {"label":"-", "x":3, "y":0}, 115 {"label":"7", "x":0, "y":1}, 116 {"label":"8", "x":1, "y":1}, 117 {"label":"9", "x":2, "y":1}, 118 {"label":"+", "x":3, "y":1, "h":2}, 119 {"label":"4", "x":0, "y":2}, 120 {"label":"5", "x":1, "y":2}, 121 {"label":"6", "x":2, "y":2}, 122 {"label":"1", "x":0, "y":3}, 123 {"label":"2", "x":1, "y":3}, 124 {"label":"3", "x":2, "y":3}, 125 {"label":"Enter", "x":3, "y":3, "h":2}, 126 {"label":"0", "x":0, "y":4, "w":2}, 127 {"label":".", "x":2, "y":4} 128 ] 129 } 130 } 131} 132``` 133 134The `layouts` object contains the data that represents the physical layout of the keyboard. It has an object `LAYOUT`, which needs to match the name of our layout macro from `numpad.h`. The `LAYOUT` object itself has an object named `layout`, which contains one JSON object for each physical key on our keyboard, formatted as follows: 135 136``` 137 The name of the key. Not displayed in the Configurator. 138 | 139 | The key's X-axis location, in key units from the 140 | | keyboard's left edge. 141 | | 142 | | The key's Y-axis location, in key units from 143 | | | the keyboard's top (rear-facing) edge. 144 ↓ ↓ ↓ 145{"label":"Num Lock", "x":0, "y":0}, 146``` 147 148Some objects will also have `"w"` and `"h"` keys, which represent a key's width and height, respectively. 149 150::: tip 151For more on the `info.json` files, see [`info.json` Format](reference_info_json). 152::: 153 154 155## How the Configurator Programs Keys 156 157The Configurator's API uses the layout macro and the JSON file we've given it to create a visual representation of the keyboard that has each visual object tied to a specific key, in sequence: 158 159| Key in layout macro | JSON object used | 160| ------------------- | ---------------------------------------- | 161| k00 | `{"label":"Num Lock", "x":0, "y":0}` | 162| k01 | `{"label":"/", "x":1, "y":0}` | 163| k02 | `{"label":"*", "x":2, "y":0}` | 164| k03 | `{"label":"-", "x":3, "y":0}` | 165| k10 | `{"label":"7", "x":0, "y":1}` | 166| k11 | `{"label":"8", "x":1, "y":1}` | 167| k12 | `{"label":"9", "x":2, "y":1}` | 168| k13 | `{"label":"+", "x":3, "y":1, "h":2}` | 169| k20 | `{"label":"4", "x":0, "y":2}` | 170| k21 | `{"label":"5", "x":1, "y":2}` | 171| k22 | `{"label":"6", "x":2, "y":2}` | 172| k30 | `{"label":"1", "x":0, "y":3}` | 173| k31 | `{"label":"2", "x":1, "y":3}` | 174| k32 | `{"label":"3", "x":2, "y":3}` | 175| k33 | `{"label":"Enter", "x":3, "y":3, "h":2}` | 176| k40 | `{"label":"0", "x":0, "y":4, "w":2}` | 177| k42 | `{"label":".", "x":2, "y":4}` | 178 179When a user selects the top-left key in the Configurator, and assigns Num Lock to it, the Configurator builds a keymap file with `KC_NUM` as the first key, and so on as the keymap is built. The `label` keys are not used; they are only for the user's reference in identifying specific keys when debugging the `info.json` file. 180 181 182## Issues and Hazards 183 184Currently, the Configurator does not support key rotation or non-rectangular key shapes like ISO Enter. Additionally, keys that are vertically-offset from their "row" &mdash; the arrow keys on 1800-layouts like the [TKC1800](https://github.com/qmk/qmk_firmware/tree/4ac48a61a66206beaf2fdd5f2939d8bbedd0004c/keyboards/tkc1800/) being a prominent example &mdash; confuse the KLE-to-JSON Converter, if not adjusted for by the contributor of the `info.json` file. 185 186### Workarounds 187 188#### Non-rectangular keys 189 190For ISO Enter keys, QMK custom is to display it as a rectangular key, 1.25u wide and 2u high, aligned so its right edge is aligned with the right edge of the alphanumeric key block. 191 192![](/JKngtTw.png) 193*A 60% keyboard in standard ISO layout, as rendered by QMK Configurator.* 194 195#### Vertically-offset keys 196 197For vertically-offset keys, place them in KLE as if they were not offset, then edit the Y-values as needed in the converted JSON file 198 199![](/fmDvDzR.png) 200*An 1800-layout keyboard as rendered in Keyboard Layout Editor, without the vertical offset applied to the arrow keys.* 201 202```diff 203-{"label": "\u2191", "x", 14.25, "y": 5}, 204+{"label": "\u2191", "x", 14.25, "y": 5.25}, 205... 206-{"label": "\u2190", "x", 13.25, "y": 6}, 207-{"label": "\u2193", "x", 14.25, "y": 6}, 208-{"label": "\u2192", "x", 15.25, "y": 6}, 209+{"label": "\u2190", "x", 13.25, "y": 6.25}, 210+{"label": "\u2193", "x", 14.25, "y": 6.25}, 211+{"label": "\u2192", "x", 15.25, "y": 6.25}, 212``` 213*A diff showing the changes needed to vertically-offset the arrow keys in our keyboard's JSON file.*