JavaScript Event Learning 11th Chapter key to detect _javascript skills

Source: Internet
Author: User
Tags lowercase
The first problem is that there is no standard for keystroke events, according to the specification: the event model that contains input devices such as keyboards will be interpreted later in the DOM specification.

As we have learned, browsers do not have a standard at the beginning of the design, and we all like to do experiments, the final product although useful, but certainly caused a compatibility problem. The key issue is no exception: There are two properties that detect what key the user pressed, although there are reasons why two attributes are required, but not all browsers support it.

In addition, there are some important differences between KeyPress and KeyDown and KeyUp.

Finally, the difference between Windows and Mac, on the Mac to detect what the user pressed the key is more than in Windows difficult n times.

KeyCode and CharCode
The two attributes that can detect what the user has pressed on the key are KeyCode and charcode. Simply put: KeyCode is used to detect that the user actually presses the key on the keyboard, while CharCode is the ASCII code that gives the character to be typed. There are some minor issues to note: The KeyCode of uppercase A and lowercase a is the same, because they are a key on the keyboard; but CharCode is different because they are two distinct characters.

IE and opera do not support CharCode. However, they will hold the character information in the KeyCode, but only in the onkeypress case, the Onkeydown/up case contains the information of the key.

Character and number keys
Let's start with a simple example. The ASCII code for lowercase A is 97, and the upper case is 65. So in both cases, when the user knocks the same key on the keyboard, when the key value is 65 (equivalent to an uppercase a)

KeyCode

CharCode


Therefore, in the case of onkeydown/up, you can get the key value from the KeyCode. In onkeypress case, want to get the character value to use: Evt.charcode | | Evt.keycode.

Punctuation
I decided not to test the punctuation key. I suspect that this is related not only to the browser and the operating system, but also to the keyboard settings and the default language. I generally use the Dutch version of Windows, if compared with the United States version of the 101-key keyboard is very different, I do not feel surprised.

such as shift+, the key should come out, but the result of the ASCII code I tested was '? ' Of When I found out about the problem, I decided not to waste my time on the punctuation key. The

Special key
Function key is a key that cannot be printed but has a certain function. such as shift, ESC, enter, and so on are all function keys.

Some instructions:
1, General, Mac reliability is worse than Windows, some keys may not detect

2, IE does not trigger the following key KeyPress event: Delete, end, enter, escape, function key, Home, INSERT, pageup/down and tab.

3, under the onkeypress event, Safari gives the following keys a very strange keycode value: Delete, end, function key, home, and Pageup.down. But it's normal under the onkeydown/up. The

4, Alt,cmd,ctrl, and Shfit keys are not detectable on the Mac, except opera below. However, you can use Altkey,ctrlkey,shfitkey these attributes.

If you need to detect these keys, you can probe the keycode on the onkeydown/up below to help yourself, onkeypress and charcode forget it. The

text has a list of large key values, as well as a test box, where interested child shoes can be used.
Translate address: http://www.quirksmode.org/js/keys.html

Reprint Please keep the following information
Author: North Jade (TW: @rehawk)

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.