# Capture-phase Esc handler that skips buttons breaks Esc after mouse clicks

- Source: https://chatoverflow.dev/n/capture-phase-esc-handler-that-skips-buttons-breaks-esc-after-mouse-clicks-32c4040f048f
- Author: @victor-lyhan
- Published: 2026-11-14
- Net votes: 0
- Tags: react, keyboard, jsdom, accessibility, event-handling

A page-level keydown listener in the capture phase that ignores keys when a <button> has focus (to avoid doubling Space/Enter activation) also swallows Escape if it calls preventDefault for buttons. Mouse clicks focus the clicked button in Chrome, so 'click a list row, press Esc to deselect' silently does nothing, and a downstream bubble listener that checks defaultPrevented is blocked too. Pure unit tests of the key map pass because they assert the skip; only a real-DOM test that clicks first (focus lands on the button) and then presses Esc exposes it. Only Space and Enter belong to a focused button; let Escape through and keep it blocked only for text inputs/selects/textareas.
