JavaScript onkeydown event entry instance (a key on the keyboard is pressed), javascript button

Source: Internet
Author: User

JavaScript onkeydown event entry instance (a key on the keyboard is pressed), javascript button

JavaScript onkeydown event

An onkeydown event is triggered when you press the next keyboard button. Unlike the onkeypress event, the onkeydown event responds to the processing of any key press (including the function key), and The onkeypress event only responds to the processing after the character key is pressed.

Prompt

Internet Explorer/Chrome uses event. keyCode to retrieve the characters that have been pressed, whereas Netscape/Firefox/Opera and other browsers use event. which.

Onkeydown gets the key pressed by the user

The following is an example of using the onkeydown event to obtain the user's press key information:
Copy codeThe Code is as follows:
<Html>
<Body>
<Script type = "text/javascript">

Function noNumbers (e)
{
Var keynum;
Var keychar;

Keynum = window. event? E. keyCode: e. which;
Keychar = String. fromCharCode (keynum );
Alert (keynum + ':' + keychar );
}

</Script>
<Input type = "text" onkeydown = "return noNumbers (event)"/>
</Body>
</Html>

As shown in the preceding example, event. keyCode/event. which obtains the numeric value (Unicode encoding) corresponding to a key. The common key value corresponds to the following:

Numeric Value Actual key value
48 to 57 0 to 9
65 to 90 A to z (A to Z)
From 112 to 135 F1 to F24
8 BackSpace)
9 Tab
13 Enter (Press Enter)
20 Caps_Lock (Capital lock)
32 Space (Space key)
37 Left (Left arrow)
38 Up (Up arrow)
39 Right (Right arrow)
40 Down (Down arrow)

In Web applications, you can often see that you can use the event. keyCode/event. which of the onkeydown event to obtain some of your keyboard operations and run some examples. For example, when a user logs on, if the capital Lock key (20) is pressed, the system prompts the capital lock; when there is a page flip, if the user presses the left and right arrows to trigger up and down pages.

After obtaining the Unicode encoding value, you can obtain the actual key value through the fromCharCode method (String. fromCharCode () of the Srting object. Note that uppercase characters are always obtained, while some other function buttons may not be easy to read.


How does javascript obtain keyboard event press?

Status determination is performed before the binding event is executed. If this event has been executed once, it will not be executed until the keyup status is switched. If you press it again, it will take effect again.

In ACCESS, onkeypress is an event that occurs when the keyboard on the form is pressed, while onkeydown is an event that occurs when the keyboard on the form is pressed.

To understand the differences between the two operations, you must first understand the key actions, which are generally divided into two: one is press, the English is Keydown, the second is to open the key, called Keyup, and the two actions are called Keypress together

Onkeypress is an event triggered when a key is pressed and popped up.
Onkeydown event refers to an event that can be triggered by pressing a key and does not have to wait for a bounce.

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.