Warning: session_start(): open(/tmp/sess_1d28c2e465ee2cb4dbe493848b76d521, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Handle The OnKeyPress Event In React - LearnShareIT

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()" />
<script>
function onKeyPress() {
    console.log('This is the name input');
}
</script>

Output:

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. 

Code:

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

Output:

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.

Code:

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

    return (
      	<textarea 
          onKeyPress={(e) => handleKeyPress(e)} 
          defaultValue="Your name"
        ></textarea>
    );
}

Output:

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.

Summary

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 *