What is the TypeScript definition for the onKeyDown event in React?

The right interface for onKeyDown is KeyboardEvent

Please continue reading below to see how to use it or read my guide on using React events with TypeScript.

You can also go to the search page 🔍 to find another event.

Interface

interface KeyboardEvent<T = Element> extends SyntheticEvent<T, NativeKeyboardEvent> {
    altKey: boolean;
    /** @deprecated */
    charCode: number;
    ctrlKey: boolean;
    getModifierState(key: string): boolean;
    key: string;
    /** @deprecated */
    keyCode: number;
    locale: string;
    location: number;
    metaKey: boolean;
    repeat: boolean;
    shiftKey: boolean;
    /** @deprecated */
    which: number;
}

Full example

import React, { KeyboardEvent } from 'react';

const App = () => {
  const handleKeyboardEvent = (e: KeyboardEvent<HTMLImageElement>) => {
    // Do something
  };

  return <div onKeyDown={handleKeyboardEvent}>{/** Some code */}</div>;
};

export default App;

Attributes that use KeyboardEvent:

Get notified about new tutorials

Join over 1,000 developers who receive React and JavaScript tutorials via email.

No spam. Unsubscribe at any time.