Languages

React - What's the difference between onKeyPress and onKeyDown?

0 points
Asked by:
Rubi-Reyna
677

What is the difference between onKeyPressonKeyDown and onKeyUp events in React?

1 answer
0 points
Answered by:
Rubi-Reyna
677

I found out that:

  • onKeyDown happens first,
  • onKeyPress happens second (when text is entered),
  • onKeyUp happens last.

They are analogous to:

  • onMouseDown,
  • onClick,
  • onMouseUp.

Note:

The onKeyPress is deprecated, which means that this feature is no longer recommended.

Runnable example:

// ONLINE-RUNNER:browser;

window.addEventListener("keydown", log);
window.addEventListener("keypress", log);
window.addEventListener("keyup", log);

function log(event){
  console.log( event.type );
}
0 comments Add comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join