Type onKeyDown, onKeyUp, onKeyPress events in React (TS)

avatar
Borislav Hadzhiev

Last updated: Feb 29, 2024
6 min

banner

# Table of Contents

  1. Type the onKeyDown event in React (TypeScript)
  2. Type the onKeyUp event in React (TypeScript)
  3. Type the onKeyPress event in React (TypeScript)

Make sure to click on the relevant to you subheading depending on the event you need to type.

# Type the onKeyDown event in React (TypeScript)

Use the React.KeyboardEvent<HTMLElement> type to type the onKeyDown event in React. The KeyboardEvent interface is used for onKeyDown events.

You can access the value of the key pressed by the user as event.key.

App.tsx
import React from 'react'; const App = () => { // ๐Ÿ‘‡๏ธ type event as React.KeyboardEvent<HTMLElement> const handleKeyDown = ( event: React.KeyboardEvent<HTMLElement>, ) => { console.log(event.key); }; return ( <div> <input type="text" id="message" name="message" defaultValue="" onKeyDown={handleKeyDown} /> </div> ); }; export default App;
The code for this article is available on GitHub

We typed the event as React.KeyboardEvent<HTMLElement> because the KeyboardEvent type is used for onKeyDown events in React.

onkeydown event

However, we could have been more specific when typing the event.

The easiest way for you to find out what the type of an event is, is to write the event handler inline and hover over the event parameter in the function.
App.tsx
const App = () => { // ๐Ÿ‘‡๏ธ onKeyDown event is written inline // hover over the `event` parameter with your mouse return ( <div> <input type="text" id="message" name="message" defaultValue="" onKeyDown={event => console.log(event)} /> </div> ); }; export default App;

get type of onkeydown event inline

The code for this article is available on GitHub
When the event is written inline, I can hover over the event parameter and it shows me what the type of the event is.

TypeScript is able to infer the type of the event when it's written inline.

This is very useful because it works with all events. Simply write a "mock" implementation of your event handler inline and hover over the event parameter to get its type.

Once you know the type of the event, you are able to extract your handler function and type it correctly.

Now that we know that the correct type for the onKeyDown event in the example is React.KeyboardEvent<HTMLInputElement>, we can extract our handler function.

App.tsx
import React from 'react'; const App = () => { // ๐Ÿ‘‡๏ธ type event correctly const handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => { console.log(event.key); }; return ( <div> <input type="text" id="message" name="message" defaultValue="" onKeyDown={handleKeyDown} /> </div> ); }; export default App;

The key property on the KeyboardEvent object returns the value of the key that was pressed by the user.

The type we passed to the KeyboardEvent generic is HTMLInputElement because we attached the onKeyDown event to an input element, however, you could be attaching the event to a different element.

The types are consistently named HTML***Element. Once you start typing HTML.., your IDE should be able to help you with autocomplete.

Some commonly used types are: HTMLInputElement, HTMLButtonElement, HTMLAnchorElement, HTMLImageElement, HTMLTextAreaElement, HTMLSelectElement, etc.

Note that you can use this approach to get the type of all events, not just onKeyDown events.

As long as you write the event handler function inline and hover over the event parameter, TypeScript will be able to infer the event's type.

If you need to handle the onKeyDown event on a Div element, click on the link and follow the instructions.

# Type the onKeyUp event in React (TypeScript)

Use the React.KeyboardEvent<HTMLElement> type to type the onKeyUp event in React. The KeyboardEvent interface is used for onKeyUp events.

App.tsx
import React from 'react'; const App = () => { // โœ… type event as React.KeyboardEvent<HTMLElement> const handleKeyUp = (event: React.KeyboardEvent<HTMLElement>) => { console.log(event.key); // console.log(event.code); }; return ( <div> <input type="text" id="message" name="message" defaultValue="" onKeyUp={handleKeyUp} /> </div> ); }; export default App;
The code for this article is available on GitHub

We typed the event as React.KeyboardEvent<HTMLElement> because the KeyboardEvent type is used for onKeyUp events in React.

onkeyup event

However, we could have been more specific when typing the event.

The easiest way for you to find out what the type of an event is, is to write the event handler inline and hover over the event parameter in the function.
App.tsx
const App = () => { // ๐Ÿ‘‡๏ธ onKeyUp event is written inline // hover over the `event` parameter with your mouse return ( <div> <input type="text" id="message" name="message" defaultValue="" onKeyUp={event => console.log(event)} /> </div> ); }; export default App;

get type of onkeyup event inline

When the event is written inline, I can hover over the event parameter and it shows me what the type of the event is.

TypeScript is able to infer the type of the event when it's written inline.

This is very useful because it works with all events. Simply write a "mock" implementation of your event handler inline and hover over the event parameter to get its type.

Once you know the type of the event, you are able to extract your handler function and type it correctly.

Now that we know that the correct type for the onKeyUp event in the example is React.KeyboardEvent<HTMLInputElement>, we can extract our handler function.

App.tsx
import React from 'react'; const App = () => { const handleKeyUp = (event: React.KeyboardEvent<HTMLInputElement>) => { console.log(event.key); // console.log(event.code); }; return ( <div> <input type="text" id="message" name="message" defaultValue="" onKeyUp={handleKeyUp} /> </div> ); }; export default App;

The key property on the KeyboardEvent object returns the value of the key that was pressed by the user.

The type we passed to the KeyboardEvent generic is HTMLInputElement because we attached the onKeyUp event to an input element, however, you could be attaching the event to a different element.

The types are consistently named HTML***Element. Once you start typing HTML.., your IDE should be able to help you with autocomplete.

Some commonly used types are: HTMLInputElement, HTMLButtonElement, HTMLAnchorElement, HTMLImageElement, HTMLTextAreaElement, HTMLSelectElement, etc.

Note that you can use this approach to get the type of all events, not just onKeyUp events.

As long as you write the event handler function inline and hover over the event parameter, TypeScript will be able to infer the event's type.

# Type the onKeyPress event in React (TypeScript)

Use the React.KeyboardEvent<HTMLElement> type to type the onKeyPress event in React. The KeyboardEvent interface is used for onKeyPress events.

App.tsx
import React from 'react'; const App = () => { const handleKeyPress = (event: React.KeyboardEvent<HTMLElement>) => { console.log(event.key); }; return ( <div> <input type="text" id="message" name="message" defaultValue="" onKeyPress={handleKeyPress} /> </div> ); }; export default App;
The code for this article is available on GitHub

We typed the event as React.KeyboardEvent<HTMLElement> because the KeyboardEvent type is used for onKeyPress events in React.

However, we could have been more specific when typing the event.

The easiest way for you to find out what the type of an event is, is to write the event handler inline and hover over the event parameter in the function.
App.tsx
const App = () => { // ๐Ÿ‘‡๏ธ onKeyPress event is written inline // hover over the `event` parameter with your mouse return ( <div> <input type="text" id="message" name="message" defaultValue="" onKeyPress={event => console.log(event)} /> </div> ); }; export default App;

get type of onkeypress event inline

When the event is written inline, I can hover over the event parameter and it shows me what the type of the event is.

TypeScript is able to infer the type of the event when it's written inline.

This is very useful because it works with all events. Simply write a "mock" implementation of your event handler inline and hover over the event parameter to get its type.

Once you know the type of the event, you are able to extract your handler function and type it correctly.

Now that we know that the correct type for the onKeyPress event in the example is React.KeyboardEvent<HTMLInputElement>, we can extract our handler function.

App.tsx
import React from 'react'; const App = () => { // โœ… type event correctly const handleKeyPress = (event: React.KeyboardEvent<HTMLInputElement>) => { console.log(event.key); }; return ( <div> <input type="text" id="message" name="message" defaultValue="" onKeyPress={handleKeyPress} /> </div> ); }; export default App;

The type we passed to the KeyboardEvent generic is HTMLInputElement because we attached the onKeyPress event to an input element, however, you could be attaching the event to a different element.

The types are consistently named HTML***Element. Once you start typing HTML.., your IDE should be able to help you with autocomplete.

Some commonly used types are: HTMLInputElement, HTMLButtonElement, HTMLAnchorElement, HTMLImageElement, HTMLTextAreaElement, HTMLSelectElement, etc.

Note that you can use this approach to get the type of all events, not just onKeyPress events.

As long as you write the event handler function inline and hover over the event parameter, TypeScript will be able to infer the event's type.

Want to learn more about typing events in React? Check out these resources: Type the onChange event of an element in React (TypeScript),Type the onSubmit event in React (TypeScript).

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev