Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

A key press does nothing on a div with onKeyDown in React

ErroronKeyDown on a div never runs, no console output

Short answer

Keyboard events go to the element that has focus. A plain div cannot take focus, so its onKeyDown handler never runs. Add tabIndex={0} (or -1 when you focus it from code), make sure the div really has focus, and use onKeyDown, not the deprecated onKeyPress.

  • Applies to React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Press a key, then ask the browser which element currently has focus. If it prints BODY, your div never got focus.

    // paste in the browser console after clicking your div
    document.activeElement
  2. 2Match what you see, then jump to its fix

    1. 1document.activeElement is BODY after you click the divThe div is not focusableFix
    2. 2The div has tabIndex, but the handler runs only after a manual click or TabFocus is on another element, or never moved to the divFix
    3. 3The prop is onKeyPress, or Enter and arrow keys are ignoredThe handler is on onKeyPressFix
    4. 4You added the div only to react to Enter or Space like a buttonA div is the wrong element for a clickable controlFix

What does the error look like?

  • onKeyDown on a div never runs, no console output
  • The handler fires only after clicking an input inside the div
  • onKeyPress is deprecated

Cause 1 of 4

The div is not focusable

1Check

A div without a tabindex attribute is not in the tab order and does not take focus, so the key press is aimed at the body, not at your handler.

// does the rendered div carry a tabindex?
document.querySelector('[data-key-target]').getAttribute('tabindex')

2Fix

Give the div a tabIndex. 0 puts it in the Tab order. -1 lets you focus it from code or by click only.

<div
  data-key-target
  tabIndex={0}
  onKeyDown={(e) => console.log(e.key)}
>
  Click here, then press a key
</div>

3Verify

Click the div, check document.activeElement again and press a key. The key name shows in the console.

document.activeElement === document.querySelector('[data-key-target]')

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 4

Focus is on another element, or never moved to the div

1Check

A handler on an element that does not hold focus does not receive key presses. If the div should work as soon as it appears (a game board, a modal), focus has to be set from code.

document.activeElement

2Fix

Use tabIndex={-1} and call focus() on a ref in an effect when the component shows.

import { useEffect, useRef } from 'react';

function Board() {
  const ref = useRef(null);
  useEffect(() => { ref.current?.focus(); }, []);
  return (
    <div ref={ref} tabIndex={-1} onKeyDown={(e) => console.log(e.key)}>
      Board
    </div>
  );
}

3Verify

Reload the page and press a key without clicking. The key name should print.

document.activeElement.tagName

Confirmed by developer.mozilla.org

Did this fix it?

Cause 3 of 4

The handler is on onKeyPress

1Check

React marks onKeyPress as deprecated and tells you to use onKeyDown or onBeforeInput instead.

grep -rn "onKeyPress" src/

2Fix

Rename the prop to onKeyDown and read e.key in the handler.

<div tabIndex={0} onKeyDown={(e) => {
  if (e.key === 'Enter') save();
}} />

3Verify

Press Enter while the div has focus and confirm save() runs.

grep -rn "onKeyPress" src/ || echo "none left"

Confirmed by react.dev

Did this fix it?

Cause 4 of 4

A div is the wrong element for a clickable control

1Check

Native controls such as button, input and select are focusable by default. A div acting as a control needs focus handling that you must add yourself.

grep -rn "<div.*onClick" src/

2Fix

Use a button element for actions. Keep the div with tabIndex only for areas that need to capture keys, such as a canvas or a game board.

<button type="button" onClick={save} onKeyDown={onKey}>Save</button>

3Verify

Tab to the control and press Enter or Space. It works without extra code.

document.activeElement.tagName

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Use native button, a and input elements for anything a user can activate.
  • When a div must take keys, set tabIndex and decide who gives it focus.
  • Use onKeyDown for new code; onKeyPress is deprecated.
  • Log document.activeElement first when keyboard handlers seem dead.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.