Onkeyup onkeydown and onkeypress

  • 2020-03-26 21:34:29
  • OfStack

There are three differences on MSDN:

The name of the show


This event occurs when the user presses and releases any alphanumeric key. System buttons (for example, arrow keys and function keys) cannot be identified.


This event occurs when the user releases any previously pressed keyboard keys.


This event occurs when the user presses any keyboard key (including system buttons, such as arrow keys and function keys).

= = = = = = = = = = = = = = = = = = = = = =
function checkForm(){ 
if(event.keyCode ==13){ 
event.keyCode =9; 
<form name ="form1"> 
<input type="text" name = "text1" onkeydown = "checkForm()"> 
<input type="button" name = "button1" value=" button "> 

When press enter, the focus moves from the text box to the button. If you replace it with "onkeypress," the focus won't shift and won't be lost. But if you replace it with "onkeyup," you lose focus and the page reloads.

The test found that the onkeydown event was executed first, followed by onkeypress, and then onkeyup. Onkeydown and onkeypress affect the execution of onkeyup. If all three events are alert, only two alerts will pop up, and the alert of up event will not pop up.

There is a little difference in the response of the three events, that is, the characters entered in response to the events of onkeydown and onkeypress are not accepted by the system, while the input stream has been accepted by the system in response to the events of onkeyup. Since the onkeydown is executed before the onkeypress, it can be known from the above example that the input stream is about to enter the system when the onkeydown is triggered, that is to say, the input stream enters the system as soon as the onkeydown event is over and cannot be changed. So with the onkeydown event you can change which key the user is pressing; On the other hand, the onkeypress event is triggered after the input stream enters the system, but the input stream has not been processed by the system temporarily, so the input stream cannot be changed. Onkeyup occurs after the input stream is processed by the system.

Related articles: