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.

  • Ctrl K Search 0
  • Ctrl Shift P Command list 0
  • Shift ? Show shortcuts 0
  • ← ↑ ↓ → Move 0
  • Esc Close 0

 

Show the code each legend is a piece of the section's picture, moved by key events
  1. The shortcuts. Plain <kbd> elements in one drawable section.
  2. The styles. A <kbd> keeps its box and its legend and has no background.
  3. The input. keydown, keyup, pointer events on the caps and beforeinput from the text field.
  4. One cap. The well, the side, the face, then the legend at the same offset.
  5. On paint. The section once, then every key.
  6. 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