Keycaps drawn from kbd
A list of shortcuts and a small keyboard, written with <kbd> elements. The canvas draws each <kbd> as a keycap. Hold a key on a hardware keyboard and its cap goes down
until the key comes up. On a phone, press a cap with a finger, or type in the field under the canvas with the on-screen keyboard. A row of the list lights while all of its keys are down.
Show the code each legend is a piece of the section's picture, moved by key events
- The shortcuts. Plain
<kbd>elements in onedrawablesection. - The styles. A
<kbd>keeps its box and its legend and has no background. - The input.
keydown,keyup, pointer events on the caps andbeforeinputfrom the text field. - One cap. The well, the side, the face, then the legend at the same offset.
- On
paint. The section once, then every key. - Legends from the keyboard layout, where
navigator.keyboard.getLayoutMap()exists.
How it works
The section is one drawable child of the canvas, and its picture holds every label, counter and legend. On paint the canvas draws the section once. Then, for each <kbd>, it draws
a dark well, the side of the cap and the face over the legend at rest, and copies the box of the <kbd> from the section's picture onto the face with the nine-argument drawElementImage().
A pressed key gets its face and its legend 5 px lower. offsetLeft and offsetTop give the box of each key.
The first version of this page gave every <kbd> the drawable attribute and drew each one with its own call. That works in a small canvas, and a live check below still does it.
On this page the keys that were below the bottom edge of the window had empty pictures. The Findings section has the details.
A key goes down on keydown and comes up on keyup. The keys of the keyboard block are matched by event.code, which names the physical key. The keys in the list are matched by
event.key. A spring moves each cap, and each press starts a ring that travels outward and dips the keys it passes. The page asks for frames only while a cap or a ring is moving.
The on-screen keyboard of Chrome for Android reports most keys as key: "Unidentified" in keydown. The text field under the canvas reads the typed character from
beforeinput and presses the matching cap for 140 ms. The line under the field shows the last event as the browser reported it.
Live checks
These run on their own fixtures in the corner of the window, after a tap on the button. They send key events to the fixture, not to the page.
Findings
- A piece of the section's picture, drawn with the nine-argument
drawElementImage(), moves a legend without making the<kbd>a drawable of its own. Pointer events still reach the<kbd>: hit testing inside the drawn section finds it at its layout position. - A
<kbd drawable>inside adrawablesection is left out of the section's picture and draws with its own call. In Chromium 157 the picture of such a nested drawable holds only the part of it that is inside the window. A direct child of the canvas is recorded whole, wherever it is. The last live check measures both. - Changing the text of a
<kbd>(the layout map does this) firespaintwith no call from the page. navigator.keyboard.getLayoutMap()returns the character on each physical key in a top-level page on desktop Chromium. The page uses it to relabel the keyboard block. Chrome for Android doesn't have it, and the block keeps its QWERTY labels.- A
keyupcan be lost: when the window loses focus while a key is down, and on macOS for keys released while the Command key is held. The page releases every key onblurand when the Meta key comes up. - With one drawable per key, the keys below the bottom edge of the window at load were blank on this page, and scrolling them into the window didn't paint them.
drawElementImage()didn't throw. A paint-affecting style change on a key while it was inside the window (text-shadow: 0 0 0 transparent) made the browser record it. The page no longer depends on this. - Not verified: Chrome 159 for Android. What the on-screen keyboard reports in
keydownandbeforeinput, whether a finger press holds a cap down, and whether a hardware keyboard paired with the phone givesevent.code. Type in the field and read the line under it.