Skip to
Keyboard shortcuts

Use the up and down arrow keys to move between items. Press Alt+Shift+K to open this list again.

Alpha version out now. The API may still change.

Kbd

A key name in running text, drawn as a key. Shows a key the reader presses, such as Tab or Esc, inside a sentence. It is the browser’s own kbd element, so a screen reader (software that reads the screen aloud) reads the key as part of the sentence.

Package
@kvirn-ui/react
Testing
Automated. Core n/a, Release n/a

When to use it#

  • Use it in help text, instructions and shortcut lists to show a key the user presses: Tab, Esc, Ctrl and C.
  • One key per Kbd. Show a combination by nesting them.
  • Say what the key does in words: the key is never the only instruction.
  • Not for something users can press: a key drawn as text isn’t a control. Use a Button.

Example#

You can move between the fields with Tab.

Use cases#

Two keys pressed together

An outer Kbd groups one Kbd per key, with the separator as plain text. The default theme draws the keys and leaves the outer one plain, and the group never breaks across lines.

Copy the text with Ctrl+C.

Information: Did you know?

Don’t translate key names: write them as printed on the keyboard. In Nordic text, Ctrl, Shift, Tab and Esc need nothing more.

A list of shortcuts

Each item names the key or keys, then says what they do in words. Give the list a name, so a screen reader user hears what the list is for.

Shortcuts in the editor

  • Ctrl+S: Save the draft
  • Ctrl+Z: Undo the last change
  • Esc: Close the dialog

Information: Did you know?

A plain <kbd> inside a Prose gets the same look as a Kbd, so text you don’t write in React, such as Markdown, needs no component.

A shortcut that differs on a Mac

A key can have a different name on another system: Ctrl on Windows is Cmd on a Mac. Write both, and say in words which system each is for.

Print your application with Ctrl+P. On a Mac, use Cmd+P.

Information: Did you know?

A Swedish Mac prints Retur on the key a Windows keyboard calls Enter. Write the name your users have on their keyboards.

A key in a text in another script

Key names stay as printed on the keyboard, in Latin letters. In a text in Arabic, wrap the key in an element that says it is English, so a screen reader says it in the right language.

اضغط على Tab للانتقال بين الحقول.

Warning: Don't forget

Mark the key with lang="en" on your own element around the Kbd. Without it, a screen reader may say the key in the wrong language (3.1.2 Language of Parts).

Accessibility#

What it does for you#

  • APG pattern: none. Kbd is inline text, not a widget.
  • Deviations: none
  • Native elements used: <kbd>. as accepts only kbd (allowed elements below).

Kbd is the native <kbd> element with a class, so the theme can draw it as a key. It adds no role, ARIA, text or behaviour, so what a user perceives is the text inside it, in the flow of the sentence.

PartElement / roleARIANotes
Kbd<kbd> → no rolenoneclass="kv-kbd". One key per Kbd. Attributes (id, aria-*) and the ref reach the element unchanged. The key name is the content: Tab, Esc, Ctrl
Kbdnested <kbd>noneA combination is an outer Kbd around one Kbd per key, with the separator as text: <Kbd><Kbd>Ctrl</Kbd>+<Kbd>C</Kbd></Kbd>. The theme draws the innermost keys and leaves the outer one plain
Kbdasnonekbd is the only value. Any other value, samp included, is a type error and, in JavaScript, warns once in development and renders the default element. className joins, the ref gets the element
Kbdneverno role, tabindexNo click handler, no live region. A key name is text, so screen readers read it as part of the sentence, and the theme draws no content (no generated text, no icons) that would be skipped or read twice

It announces nothing.

What you need to do#

  • Name the key as printed on the keyboard, and say what it does in words. Tab is the same in every locale, so it is not translated. The sentence around it says what the key is for, and the key is never the only instruction (1.3.3 Sensory Characteristics (A): "press the key in the box" is not enough).
  • Language. Key names that are the surrounding language's vernacular (Ctrl, Shift, Tab, Esc in Nordic text) need nothing. A key in another script (Arabic text, say) goes in your own <span lang="en"><Kbd>Tab</Kbd></span> (3.1.2 Language of Parts (AA)).
  • Name the keys your users have. A key name differs between keyboards: Ctrl is Cmd on macOS, and Enter is Retur on a Swedish Mac. Name what your users have.
  • A character shortcut needs a way to turn it off or remap it (2.1.4 Character Key Shortcuts (A)). Kbd only documents a shortcut: it doesn't make one.

Warning: Don't forget

If your service has a shortcut of a single letter or number, let people turn it off or change it (2.1.4 Character Key Shortcuts). Kbd only shows a shortcut. It doesn’t make one work.

Visual and modes#

Headless: Kbd ships no CSS. With the default theme, kv-kbd (and a bare kbd inside Prose, which gets the same rule) draws a flat key in the same family as inline code: the body font at the code size, a light background, a 1px edge on all four sides, a small corner radius, and no line break inside the key. There is no depth, because depth means "press me" and is for buttons only.

  • Grouping. An outer kbd that holds other keys is plain: it inherits the font, stays on one line and puts a small space between its keys.
  • Contrast. Text on the key's background is checked automatically at 4.5:1 or more (1.4.3 Contrast (Minimum), AA).
  • The edge is not required by 1.4.11 Non-text Contrast (AA), since the key name is text. It keeps the contrast of a control's border, so a low-vision user at 200% sees a key and not a word.
  • Text size. The size is relative to the surrounding text, so it follows the browser's text size (1.4.4 Resize Text, AA).
  • Forced colours. The edge uses the system's text colour. The shape and edge are not colour cues.
  • Target size, focus indicator and motion: not applicable, because Kbd is not a control.

Warning: Don't forget

If you restyle the key, keep its text at 4.5:1 contrast against its background (1.4.3 Contrast (Minimum)), and size it relative to the surrounding text so it follows the browser’s text size (1.4.4 Resize Text).

Focus management#

  • Initial focus: not moved. Kbd never moves focus.
  • Trap: no.
  • Restore to: not applicable.
  • A key drawn in text is not a control. Don't make it look pressable: if it must act, use a Button.

Allowed elements#

A tag outside the list changes the page's outline or semantics (1.3.1 Info and Relationships (A), 4.1.2 Name, Role, Value (A)). as is a string, so it works from a Server Component.

Part`as`Why
Kbdkbd (default)samp is text a program prints, not a key, so it is not allowed. Nothing else names keyboard input

WCAG success criteria#

  • 1.3.1 Info and Relationships (A): keyboard input is marked up as <kbd> and not as styled text.
  • 1.4.3 Contrast (Minimum) (AA): the key's text is dark enough on its background, checked automatically in the default theme.
  • 1.4.4 Resize Text (AA): the size follows the browser's text size.

Keyboard#

Kbd has no keys of its own. What you put in it keeps its own keys.

API reference#

import { Kbd, useKbd } from '@kvirn-ui/react'

Kbd

Renders <kbd class="kv-kbd"> with no role. It adds no ARIA, text or behaviour, takes every attribute of an HTML element and passes ref to it. The default theme is described on Theming.

PropTypeDefaultDescription
as'kbd''kbd'Only kbd is allowed. Program output (samp) is not a key, and nothing else names keyboard input.
Class or attributeValuesMeaning
kv-kbdalwaysThe part class. A className you pass is joined with it, never replaces it.

useKbd

Takes no options.

Result

PropTypeDefaultDescription
element'kbd'–The element: always kbd.
rootPropsKbdPartProps–Spread on the element: only the class kv-kbd. No role, ARIA or state.

Testing#

The automated tests prove the keyboard behaviour, the names and states, that axe finds no violations in every story and theme, and, for the components they cover, that the stories hold up in forced colours, reduced motion, text spacing and a 320 pixel width. An Automated row passes on these tests alone. A Core or Release row passes only when an automated agent check and a person on the real assistive technology have both checked it. "agent-verified" means automated tests in the repository (with a virtual screen reader for the screen reader rows), not the real assistive technology.

AT + browser + OSAgent checkHuman checkResultNotes
Automated (re-runnable tests)
axe, WCAG 2.2 A and AA, every story, 4 themespassn/apasskbd.stories.tsx, every story, 4 themes
Markup and ARIA (roles, names, ids, states)passn/apasskbd.test.tsx › renders a kbd, no role or ARIA, and passes attributes and the ref through; expectNoA11yViolations
Keyboard contract rows (Vitest browser)passn/apasskbd.test.tsx › is not a Tab stop
Focus order and managementn/an/an/anothing focusable: the component never takes or moves focus
Display modes (forced colours, 320px reflow, text spacing, reduced motion)passn/apassthe four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI
Core (required for beta)
NVDA + Chrome + Windowsn/an/an/anative <kbd> with no role, read as text in the sentence; no state, focus or key handling; nothing to operate
VoiceOver + Safari + iOSn/an/an/anative <kbd> with no role, read as text in the sentence; no state, focus or key handling; nothing to operate
Release (before 1.0 and each minor)
JAWS + Chrome + Windowsn/an/an/anative <kbd> with no role, read as text in the sentence; no state, focus or key handling; nothing to operate
TalkBack + Chrome + Androidn/an/an/anative <kbd> with no role, read as text in the sentence; no state, focus or key handling; nothing to operate
VoiceOver + Safari + macOSn/an/an/anative <kbd> with no role, read as text in the sentence; no state, focus or key handling; nothing to operate