DOM events

onKeyDown

Responds to a key press, including repeated presses when held. Prefer a native button for activation. Use key for semantic keyboard commands; avoid intercepting text entry during IME composition.

Formal Syntax & Structure

react syntaxreact
onKeyDown={handler}

Working Example

Example Snippetreact
import {useState} from 'react';export default function App() { const [text,setText]=useState(''); return <label>Filter (Escape clears)<input value={text} onChange={e=>setText(e.currentTarget.value)} onKeyDown={e=>{if(e.key==='Escape'&&!e.nativeEvent.isComposing)setText('');}}/></label>; }
React Reference