Languages

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

0 points
Asked by:
Rubi-Reyna
337

What is the difference between onKeyPress,Ā onKeyDown and onKeyUp events in React?

1 answer
0 points
Answered by:
Rubi-Reyna
337

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
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