Differences between onkeyup, onkeydown, and onkeypress

Source: Internet
Author: User

There are three differences on msdn:

Description

Onkeypress

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

Onkeyup

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

Onkeydown

This event occurs when you press any keyboard key (including system buttons, such as the Arrow keys and function keys.

======================================
Copy codeThe Code is as follows:
<Html>
<Script>
Function checkForm (){
If (event. keyCode = 13 ){
Event. keyCode = 9;
}
}
</Script>
<Body>
<Form name = "form1">
<Input type = "text" name = "text1" onkeydown = "checkForm ()">
<Input type = "button" name = "button1" value = "button">
</Form>
</Body>
</Html>

When you press enter, the focus is moved from the text box to the button. If you change it to "onkeypress", the focus will not be transferred or lost. However, if you change to "onkeyup", the focus will be lost and the page will be reloaded.

Test shows that the onkeydown event is first executed, followed by onkeypress, and finally onkeyup; onkeydown and onkeypress will affect the onkeyup execution. If all three event colleagues are alert, only two alert events will be displayed, and the alert events of the up event will not be displayed.

There is another difference between the three in Event Response: The characters entered in the onkeydown and onkeypress event response are not accepted by the system, and the input stream is accepted by the system in response to onkeyup. Since onkeydown is executed before onkeypress, we can see from the above example that the input stream is about to enter the system when onkeydown is triggered. That is to say, once the onkeydown event is complete, the input stream enters the system and cannot be changed. Therefore, the onkeydown event can be used to change the key that the user presses, while the onkeypress event is triggered after the input stream enters the system. However, the input stream is not processed by the system and cannot be changed; onkeyup occurs after the input stream is processed by the system.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.