Updated OLED Docs with notes about screen timeout. (#6276)
[jackhill/qmk/firmware.git] / docs / how_keyboards_work.md
CommitLineData
7b0356d1 1# How Keys Are Registered, and Interpreted by Computers
d9bef165
JM
2
3In this file, you can will learn the concepts of how keyboards work over USB,
4and you'll be able to better understand what you can expect from changing your
5firmware directly.
6
7b0356d1 7## Schematic View
d9bef165
JM
8
9Whenever you type on 1 particular key, here is the chain of actions taking
10place:
11
12``` text
13+------+ +-----+ +----------+ +----------+ +----+
14| User |-------->| Key |------>| Firmware |----->| USB wire |---->| OS |
c02c7b75 15+------+ +-----+ +----------+ +----------+ +----+
d9bef165
JM
16```
17
18This scheme is a very simple view of what's going on, and more details follow
19in the next sections.
20
21## 1. You Press a Key
22
23Whenever you press a key, the firmware of your keyboard can register this event.
24It can register when the key is pressed, held and released.
25
dff5708b 26This 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.
d9bef165
JM
27
28## 2. What the Firmware Sends
29
d08f9cb6 30The [HID specification](https://www.usb.org/sites/default/files/documents/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.
d9bef165 31
d08f9cb6
CH
32The firmware does not send actual letters or characters, but only scancodes.
33Thus, by modifying the firmware, you can only modify what scancode is sent over
d9bef165
JM
34USB for a given key.
35
36## 3. What the Operating System Does
37
38Once the keycode reaches the operating system, a piece of software has to have
39it match an actual character thanks to a keyboard layout. For example, if your
d08f9cb6 40layout is set to QWERTY, a sample of the matching table is as follows:
d9bef165 41
d9bef165 42| keycode | character |
dff5708b 43|---------|-----------|
44| 0x04 | a/A |
45| 0x05 | b/B |
46| 0x06 | c/C |
47| ... | ... |
48| 0x1C | y/Y |
49| 0x1D | z/Z |
50| ... | ... |
d9bef165 51
7b0356d1 52## Back to the Firmware
d9bef165 53
9fed4f79 54As 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).
d9bef165
JM
55
56## List of Characters You Can Send
57
d08f9cb6 58Putting 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 are only the ones present in the layout**.
d9bef165 59
d08f9cb6 60For example, this means that if you have a QWERTY US layout, and you want to assign one 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.
d9bef165 61
d08f9cb6 62You may wonder why a keyboard layout containing all of Unicode is not devised then? The limited number of keycodes available through USB simply disallows such a thing.
d9bef165
JM
63
64## How to (Maybe) Enter Unicode Characters
65
e0a0d80b 66You can have the firmware send *sequences of keys* to use the [software Unicode Input Method](https://en.wikipedia.org/wiki/Unicode_input#Hexadecimal_input) of the target operating system, thus effectively entering characters independently of the layout defined in the OS.
d9bef165
JM
67
68Yet, it does come with multiple disadvantages:
69
e0a0d80b 70 - Tied to a specific OS at a time (need recompilation when changing OS);
d9bef165
JM
71 - Within a given OS, does not work in all software;
72 - Limited to a subset of Unicode on some systems.