What is the difference between these three events? Upon googling I found that:
- The
onKeyDownevent is triggered when the user presses a key.- The
onKeyUpevent is triggered when the user releases a key.- The
onKeyPressevent is triggered when the user presses & releases a key (onKeyDownfollowed byonKeyUp).
I understand the first two, but isn't onKeyPress the same as onKeyUp? Is it possible to release a key (onKeyUp) without pressing it (onKeyDown)?
This is a bit confusing, can someone clear this up for me?
The keydown event is triggered first when user presses a key. The keyup event is triggered last when user releases a key. In between, the keypress event is triggered.
The keydown event is fired when a key is pressed. Unlike the keypress event, the keydown event is fired for all keys, regardless of whether they produce a character value. The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered.
The onkeydown attribute fires when the user is pressing a key (on the keyboard).
The onkeyup attribute fires when the user releases a key (on the keyboard).
NOTE KeyPress is now deprecated. Use KeyDown instead.
KeyPress, KeyUp and KeyDown are analogous to, respectively: Click, MouseUp, and MouseDown.
Down happens firstPress happens second (when text is entered)Up happens last (when text input is complete).keydown keypress textInput keyup Below is a snippet you can use to see for yourself when the events get fired:
window.addEventListener("keyup", log); window.addEventListener("keypress", log); window.addEventListener("keydown", log); function log(event){ console.log( event.type ); } Check here for the archived link originally used in this answer.
From that link:
In theory, the
onKeyDownandonKeyUpevents represent keys being pressed or released, while theonKeyPressevent represents a character being typed. The implementation of the theory is not same in all browsers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With