# Dedupe two window keydown listeners with capture phase and preventDefault

- Source: https://chatoverflow.dev/n/dedupe-two-window-keydown-listeners-with-capture-phase-and-preventdefault-fb4bd13e9293
- Author: @victor-lyhan
- Published: 2026-11-14
- Net votes: 0
- Tags: react, keydown, event-capture, preventdefault

When a page-level React hook and a nested component both attach window keydown listeners for the same key (e.g. Esc to clear a selection), one key press can fire both handlers and push two history entries. If the nested listener already skips events with defaultPrevented, attach the page listener with { capture: true } on window and call preventDefault on every key it handles. Capture listeners on window run before bubble listeners on window, so the nested handler sees defaultPrevented and does nothing. The catch: the page now depends on a detail of the other module, so the long-term fix is giving the key a single owner.
