A key press does nothing on a div with onKeyDown in React
onKeyDown on a div never runs, no console outputShort 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.
Fix it fast
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.activeElement2Match what you see, then jump to its fix
- 1
document.activeElement is BODY after you click the divThe div is not focusableFix - 2
The 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
The prop is onKeyPress, or Enter and arrow keys are ignoredThe handler is on onKeyPressFix - 4
You added the div only to react to Enter or Space like a buttonA div is the wrong element for a clickable controlFix
- 1
What does the error look like?
onKeyDown on a div never runs, no console outputThe handler fires only after clicking an input inside the divonKeyPress 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
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.activeElement2Fix
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.tagNameConfirmed by developer.mozilla.org
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
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.tagNameConfirmed by developer.mozilla.org
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
Module not found for antd/dist/antd.less after upgrading to Ant Design 5
Module not found: Error: Can't resolve 'antd/dist/antd.less'
Ant Design 5 dropped Less and the bundled CSS file for CSS-in-JS, so antd/dist/antd.less no longer exists.
3 causes, each with a fixWeb apps
Astro Code component fails inside a React component
Astro components cannot be rendered directly via function call
Astro components such as Code only exist on the server and render static HTML.
2 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
How to catch and handle a 422 from Axios in a Redux thunk
action.error.message is 'Rejected' and action.payload is undefined
A 422 means the server read the request but cannot process its content, so repeating it unchanged fails again.
4 causes, each with a fixWeb apps
axios gets 429 Too Many Requests from a React app
429 (Too Many Requests)
A 429 comes from the server or an API gateway, not from axios: your client sent too many requests in a window.
4 causes, each with a fixWeb apps
Axios request is not cancelled when a React component unmounts
A cancelled request is logged as an error in the catch block
Axios cancels a request when you pass it an AbortSignal and later call abort() on its controller.
4 causes, each with a fix