While searching for the differences between these three events, you may encounter a common misconception that onKeyPress occurs simultaneously with onKeyUp. Let's clarify this confusion and shed light on the unique characteristics of each event.
In the sequence of key input events, we have the following breakdown:
To grasp the relationships between these key events, let's draw an analogy with their mouse counterparts:
In the world of web browsers, WebKit stands out by introducing an additional event: textInput. The event sequence for WebKit thus becomes:
keydown keypress textInput keyup
To witness these events in action, run the following code snippet:
window.addEventListener("keyup", log); window.addEventListener("keypress", log); window.addEventListener("keydown", log); function log(event){ console.log( event.type ); }
This script logs the event type to the console as you interact with the keyboard. You'll notice the distinct sequence of events as you press and release keys.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3