]> git.donarmstrong.com Git - qmk_firmware.git/blob - docs/how_keyboards_work.md
convert to unix line-endings [skip ci]
[qmk_firmware.git] / docs / how_keyboards_work.md
1 # How keys are registered, and interpreted by computers
2
3 In this file, you can will learn the concepts of how keyboards work over USB,
4 and you'll be able to better understand what you can expect from changing your
5 firmware directly.
6
7 ## Schematic view
8
9 Whenever you type on 1 particular key, here is the chain of actions taking
10 place:
11
12 ``` text
13 +------+         +-----+       +----------+      +----------+     +----+
14 | User |-------->| Key |------>| Firmware |----->| USB wire |---->| OS |
15 +------+         +-----+       +----------+      +----------+     |----+
16 ```
17
18 This scheme is a very simple view of what's going on, and more details follow
19 in the next sections.
20
21 ## 1. You Press a Key
22
23 Whenever you press a key, the firmware of your keyboard can register this event.
24 It can register when the key is pressed, held and released.
25
26 This usually happens with a periodic scan of key presses. This speed often is limited by the mechanical key response time, the protocol to transfer those key presses (here USB HID), and by the software it is used in.
27
28 ## 2. What the Firmware Sends
29
30 The [HID specification](http://www.usb.org/developers/hidpage/Hut1_12v2.pdf) tells what a keyboard can actually send through USB to have a chance to be properly recognised. This includes a pre-defined list of scancodes which are simple numbers from `0x00` to `0xE7`. The firmware assigns a scancode to each key of the keyboard.
31
32 The firmware does not send actually letters or characters, but only scancodes.
33 Thus, by modifying the firmware, you only can modify what scancode is sent over
34 USB for a given key.
35
36 ## 3. What the Operating System Does
37
38 Once the keycode reaches the operating system, a piece of software has to have
39 it match an actual character thanks to a keyboard layout. For example, if your
40 layout is set to QWERTY, a sample of the matching table is as follow:
41
42 | keycode | character |
43 |---------|-----------|
44 | 0x04 | a/A |
45 | 0x05 | b/B |
46 | 0x06 | c/C |
47 | ... | ... |
48 | 0x1C | y/Y |
49 | 0x1D | z/Z |
50 | ... | ... |
51
52 ## Back to the firmware
53
54 As the layout is generally fixed (unless you create your own), the firmware can actually call a keycode by its layout name directly to ease things for you. This is exactly what is done here with `KC_A` actually representing `0x04` in QWERTY. The full list can be found in [keycodes](keycodes.md).
55
56 ## List of Characters You Can Send
57
58 Putting aside shortcuts, having a limited set of keycodes mapped to a limited layout means that **the list of characters you can assign to a given key only is the ones present in the layout**.
59
60 For example, this means that if you have a QWERTY US layout, and you want to assign 1 key to produce `€` (euro currency symbol), you are unable to do so, because the QWERTY US layout does not have such mapping. You could fix that by using a QWERTY UK layout, or a QWERTY US International.
61
62 You may wonder why a keyboard layout containing all of Unicode is not devised then? The limited number of keycode available through USB simply disallow such a thing.
63
64 ## How to (Maybe) Enter Unicode Characters
65
66 You can have the firmware send *sequences of keys* to use the [software Unicode Input Method](https://en.wikipedia.org/wiki/Unicode_input#Hexadecimal_code_input) of the target operating system, thus effectively entering characters independently of the layout defined in the OS.
67
68 Yet, it does come with multiple disadvantages:
69
70  - Tied to a specific OS a a time (need recompilation when changing OS);
71  - Within a given OS, does not work in all software;
72  - Limited to a subset of Unicode on some systems.