The use of keycode in JavaScript

Source: Internet
Author: User


KeyCode ()

 

The KeyCode property returns the code for the character of the value of the onkeypress event-triggered key, or for the key of the onkeydown or onkeyup event.

The differences between the two types of code are:

    • character code-a number that represents ASCII characters
    • keyboard code-a number that represents a real key on the keyboard

P> Two types of values are not equal, such as lowercase characters "w" and uppercase characters "W" have the same keyboard code, because they are on their keyboard ("W" Code is "87"), but they have different character codes, two character output is not the same ("W" and "W" character code is "119" and "87")-View the following examples to better understand.

Tip: If you need to know that the user is pressing a print key (such as "a" or "5"), we recommend that you use the onkeypress event. If you need to know that the user is pressing a function key (such as "F1", "CAPS LOCK" or "Home") you can use the onkeydown or onkeyup event.

Note: in Firefox, the KeyCode property is not valid in the onkeypress event (returns 0). Browser compatibility issues that you can use together with the which and KeyCode properties to resolve:

var x = Event.which | |  Event.keycode; Use whichOr KeyCode, which supports different browsers

Tip: List of all Unicode characters to view our full Unicode reference manual.

Tip: If you need to convert Unicode values to characters, you can use the fromCharCode () method.

Note: This property is read-only.

Note: the which and KeyCode properties provide a way to resolve browser compatibility, and the latest version of DOM events is recommended to replace this method with the key property.

Tip: If you want to see if "ALT", "CTRL", "META" or "SHIFT" keys are pressed, you can use Altkey, Ctrlkey, Metakey, or Shiftkey properties.

RELATED LINKS

The use of keycode in JavaScript

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.