Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 8.4 — Keyboard Controls
3D Web · Example 2 of 2

Tab through the parts, and a keyboard trap

Press Tab and then Enter: the buttons are real, so they work from the keyboard with no key code. The last two lines show a trap and its fix.

Every pointer action gets a keyboard twin: arrows turn, plus and minus zoom, Tab walks through the parts, Enter chooses. The canvas needs a way to take focus, a visible focus ring, and no keyboard trap.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 4 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write a keydown handler for a container that handles ArrowUp and ArrowDown by changing state.tilt by 5 degrees, limited to −30 … 30, and leaves every other key alone.

02

Write the CSS so that a button shows a clear ring only for keyboard focus, not for mouse clicks.

Explain it without notes

01

Name the keyboard twin of: dragging, the mouse wheel, hover and a click.

02

Why must the key handler ignore events with Ctrl held, and why must it not call preventDefault() on Tab?

Tab through the parts, and a keyboard trap
Press Run to see the page.
idle

Console

Nothing logged yet.

Expected console output

tab stops: bell -> saddle -> badge
selected: saddle
selected: nothing
selected: badge
handler that calls preventDefault on every key: TRAP: Tab is swallowed
handler that only takes arrow keys: Tab works