const__pageData=JSON.parse('{"title":"How Keys Are Registered, and Interpreted by Computers","description":"","frontmatter":{},"headers":[],"relativePath":"how_keyboards_work.md","filePath":"how_keyboards_work.md"}');
const_sfc_main={name:"how_keyboards_work.md"};
const_hoisted_1=/* @__PURE__ */createStaticVNode('<h1id="how-keys-are-registered-and-interpreted-by-computers"tabindex="-1">HowKeysAreRegistered,andInterpretedbyComputers<aclass="header-anchor"href="#how-keys-are-registered-and-interpreted-by-computers"aria-label="Permalink to "How Keys Are Registered, and Interpreted by Computers""></a></h1><p>Inthisfile,youcanwilllearntheconceptsofhowkeyboardsworkoverUSB,andyou'llbeabletobetterunderstandwhatyoucanexpectfromchangingyourfirmwaredirectly.</p><h2id="schematic-view"tabindex="-1">SchematicView<aclass="header-anchor"href="#schematic-view"aria-label="Permalinkto"SchematicView""></a></h2><p>Wheneveryoutypeon1particularkey,hereisthechainofactionstakingplace:</p><divclass="language-vp-adaptive-theme"><buttontitle="CopyCode"class="copy"></button><spanclass="lang"></span><preclass="shikishiki-themesgithub-lightgithub-darkvp-code"><code><spanclass="line"><span>+------++-----++----------++----------++----+</span></span>\n<spanclass="line"><span>|User|-------->|Key|------>|Firmware|----->|USBwire|---->|OS|</span></span>\n<spanclass="line"><span>+------++-----++----------++----------++----+</span></span></code></pre></div><p>Thisschemeisaverysimpleviewofwhat'sgoingon,andmoredetailsfollowinthenextsections.</p><h2id="_1-you-press-a-key"tabindex="-1">1.YouPressaKey<aclass="header-anchor"href="#_1-you-press-a-key"aria-label="Permalinkto"1.YouPressaKey""></a></h2><p>Wheneveryoupressakey,thefirmwareofyourkeyboardcanregisterthisevent.Itcanregisterwhenthekeyispressed,heldandreleased.</p><p>Thisusuallyhappenswithaperiodicscanofkeypresses.Thisspeedoftenislimitedbythemechanicalkeyresponsetime,theprotocoltotransferthosekeypresses(hereUSBHID),andbythesoftwareitisusedin.</p><h2id="_2-what-the-firmware-sends"tabindex="-1">2.WhattheFirmwareSends<aclass="header-anchor"href="#_2-what-the-firmware-sends"aria-label="Permalinkto"2.WhattheFirmwareSends""></a></h2><p>The<ahref="https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf"target="_blank"rel="noreferrer">HIDspecification</a>tellswhatakeyboardcanactuallysendthroughUSBtohaveachancetobeproperlyrecognised.Thisincludesapre-definedlistofscancodeswhicharesimplenumbersfrom<code>0x00</code>to<code>0xE7</code>.Thefirmwareassignsascancodetoeachkeyofthekeyboard.</p><p>Thefirmwaredoesnotsendactuallettersorcharacters,butonlyscancodes.Thus,bymodifyingthefirmware,youcanonlymodifywhatscancodeissentoverUSBforagivenkey.</p><h2id="_3-what-the-event-input-kernel-does"tabindex="-1">3.WhattheEventInput/KernelDoes<aclass="header-anchor"href="#_3-what-the-event-input-kernel-does"aria-label="Permalinkto"3.WhattheEventInput/KernelDoes""></a></h2><p>The<em>scancode</em>ismappedtoa<em>keycode</em>dependentonthekeyboard<ahref="https://github.com/systemd/systemd/blob/main/hwdb.d/60-keyboard.hwdb"target="_blank"rel="noreferrer">60-keyboard.hwdbatMain</a>.Withoutthismapping,theoperatingsystemwillnotreceiveavalidkeycodeandwillbeunabletodoanythingusefulwiththatkeypress.</p><h2id="_4-what-the-operating-system-does"tabindex="-1">4.WhattheOperatingSystemDoes<aclass="header-anchor"href="#_4-what-the-operating-system-does"aria-label="Permalinkto"4.WhattheOperatingSystemDoes""></a></h2><p>Oncethekeycodereachestheoperatingsystem,apieceofsoftwarehastohaveitmatchanactualcharacterthankstoakeyboardlayout.Forexample,ifyourlayoutissettoQWERTY,asampleofthematchingtableisasfollows:</p><table><thead><tr><th>keycode</th><th>character</th></tr></thead><tbody><tr><td>0x04</td><td>a/A</td></tr><tr><td>0x05</td><td>b/B</td></tr><tr><td>0x06</td><td>c