How To Handle The OnKeyPress Event In React

Handle the onKeyPress event in React

Users can set onKeyPress active with any key or set onKeyPress active with a specific key to handle the onKeyPress event in React. We’ll give you step-by-step instructions to make it happen in this article.

What is the onkeypress event in JavaScript?

The onkeypress event in JavaScript occurs after the user press a key that is not a modifier key on the keyboard. This event occurs as soon as a key is pressed and is not related to whether the key is held down or released later. And this event fires on Element Objects in JavaScript.

The modifier keys here are keys like Shift or Ctrl. The onkeypress event does not support getting events generated on these modifier keys. To get events generated when pressing them, use the event with almost the same effect as keyup and keydown events for replacement. Here is an example:

<input id="name_input" onkeypress="onKeyPress()" />
function onKeyPress() {
    console.log('This is the name input');


This is the name input

After you press any key on your keyboard, the function return “This is name input” because we can catch the onkeypress event of the input now.

The difference between onkeypress and onkeyup/onkeydown

When a key is disabled (As in keyboard shortcuts; for instance, in Ctrl+A, Ctrl is held “down”), onkeydown is triggered.

When the key is released, onkeyup is triggered (Including modifier keys).

Depending on the keyboard repeat, onkeypress is activated as a combination of onkeydown and onkeyup.

Handle the onKeyPress event in React 

Set onKeyPress active with any key

You can set the keyPress function active with any key on your keyboard. Look at the example below. 


export default function App() {
    function keyPress() {
      	console.log("You pressed the key");
    return <textarea onKeyPress={keyPress} defaultValue="Your name"></textarea>;


You pressed the key

So with this example, if you press any key, the result is the same because we didn’t set the key for the function. We also can’t get which key we have pressed.

Set onKeyPress active with a specific key

In real projects, you have to set a onkeypress event for each specific case when the user type key. For each specific case, we have different ways of handling it. Here is a specific example of a user typing specific keys.


export default function App() {
    function handleKeyPress(e) {
        if (e.key === "Enter") {
            console.log("You pressed " + e.key);
            console.log("You entered your name");

    return (
          onKeyPress={(e) => handleKeyPress(e)} 
          defaultValue="Your name"


You pressed Enter
You entered your name

In the above example, to catch the press Enter key event, we’ll press Enter and console.log run the value of the key and “You entered your name”. If we press other buttons, nothing is returned.


This article shows us how to handle the onKeyPress event in React. We hope these above methods are effective for you. If you have any questions or problems, please comment below. We will respond as possible. Thank you for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *