Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 38 ยท Keyboard and Focus

Keyboard Basics

Learn how people operate the web with a keyboard, which keys do what, how to test keyboard access in minutes, and the common patterns that break it.

Many people never touch a mouse: blind screen reader users, people with tremors or paralysis, switch and voice control users who rely on keyboard-style input, and plenty of power users. WCAG criterion 2.1.1 (Keyboard, Level A) says all functionality must be operable through a keyboard interface. It is one of the most valuable things you can test, because it exposes many other problems at once.

Think of keyboard access as the foundation. If a control works with the keyboard, assistive technologies such as switches, sip-and-puff devices and voice control can usually drive it too.

The keys that matter

Key What it does
Tab Moves focus to the next focusable element
Shift + Tab Moves focus to the previous one
Enter Activates links and buttons, submits forms
Space Activates buttons, toggles checkboxes, scrolls the page
Arrow keys Move within a group: radio buttons, menus, tabs, sliders, select
Escape Closes dialogs, menus and popups
Home / End Jumps to the first or last item in lists and sliders

Native controls already implement all this. A <select> handles arrows, type-ahead and Escape. A group of radio buttons is a single tab stop and moves with arrows. This is why native HTML is the best accessibility tool you have.

Keys to know

Tab Next focusable item
Enter Activate link or button
Space Press button, toggle box
Arrows Move inside a widget

Escape closes popups.

1 / 2

What is focusable?

Browsers give keyboard focus to interactive elements by default: links with href, buttons, form fields (input, select, textarea), summary, and elements with tabindex. Plain div, span, p and headings are not focusable.

Which elements can take focus?

Notice that the anchor without href and the plain div are skipped. If you want a custom element to take focus, tabindex="0" works, but you then owe it a role, a name and key handling, so prefer a native control.

Making a custom control keyboard operable

Suppose you cannot use <button>. The minimum recipe is role, tabindex="0", and key handlers for both Enter and Space:

A hand-made button (the hard way)

That is about ten lines of script to imitate one HTML tag, and it still lacks disabled handling. A real <button> does all this correctly.

Keyboard test in two minutes

  1. Reload the page and put your mouse out of reach.
  2. Press Tab repeatedly. Does focus move through all the controls in an order that matches the visual one?
  3. Is the focused element always visible?
  4. Can you activate every control with Enter or Space?
  5. Can you open and close menus, dialogs and popups, and does focus go somewhere sensible afterwards?
  6. Can focus ever get stuck (a keyboard trap)? Can you always Tab out?
  7. Do not forget less obvious widgets: carousels, maps, date pickers, rich text editors, file uploads, drag-and-drop.

Common keyboard failures

  • Mouse-only events. Menus or tooltips triggered only by mouseover, mousedown or click on non-interactive elements. Use focus/blur and native controls, or add keyboard handlers.
  • Positive tabindex values (tabindex="3") that scramble the order. Use only 0 and -1.
  • outline: none without a replacement.
  • Keyboard traps, often in embedded widgets and modals. WCAG 2.1.2 forbids them.
  • Custom shortcuts that clash with screen reader or browser keys. Single-character shortcuts (WCAG 2.1.4) must be turnable off, remappable or active only on focus.
  • Hidden focusable content, such as an off-screen menu still in the tab order.

Beyond Tab: roving patterns

Widgets made of many items, like tab lists, toolbars and menus, use a single tab stop and arrow keys inside, so a keyboard user does not have to tab through twenty items. You will build one in managing focus in widgets.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which WCAG criterion requires keyboard operability?

    2.1.1 requires all functionality to be available from a keyboard interface.
  2. 2. Which key activates a native button but is not needed for links?

    Both Enter and Space activate buttons; links activate with Enter only.
  3. 3. Which element is NOT keyboard focusable by default?

    Divs are generic containers. Add a native control or tabindex plus role and handlers to make them operable.
  4. 4. What is a keyboard trap?

    Users who cannot use a mouse would be stuck, so WCAG forbids it.

Summary

  • Everything that works with a mouse must work with the keyboard.
  • Tab, Shift+Tab, Enter, Space, arrows and Escape are the core keys.
  • Native interactive elements are focusable and keyboard operable already.
  • Test by tabbing through the page: visible focus, logical order, no traps.

Next: Focus Order and tabindex