The difference between CharCode and KeyCode

Source: Internet
Author: User

Get keyboard keys under standard browser We can use E.which, but there is no such attribute under non-standard, so keycode is used in most cases, but there is a compatibility problem. Let's look at the difference between his two:

Onkeydown:e.keycode has value

Onkeyup:e.keycode has value

Onkeypress:e.keycode is 0

Onkeydown:e.charcode is 0

Onkeyup:e.charcode is 0

Onkeypress:e.charcode has value

Conclusion: CharCode Returns the letter code of the onkeypress Event trigger key value.

KeyCode returns the code for the key of the onkeydown or onkeyup event.

Do some compatibility processing:

function MyFunction (e) {    | | (e = window.event);     if NULL ) {        null ? e.charcode:e.keycode;    }}

The difference between CharCode and KeyCode

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.