JavaScript onkeydown Event Starter instance (keyboard key pressed)

Source: Internet
Author: User

JavaScript onkeydown Events

The onkeydown event is triggered when a user presses a keyboard key. Unlike the onkeypress event, the onkeydown event responds to any key-pressed processing (including function keys), and the onkeypress event responds only to the processing of the character key as it is pressed.

Tips

The Internet Explorer/chrome Browser uses Event.keycode to retrieve the characters that are pressed, while browsers such as Netscape/firefox/opera use Event.which.

OnKeyDown gets the key that the user pressed

Here is an example of using the onkeydown event to get the user to press the keyboard key information:

Copy CodeThe code is as follows:
<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>

As shown in the example above, Event.keycode/event.which is given a numeric value (Unicode encoding) corresponding to a key, and the common key values correspond to the following:

Numeric value actual key value
48 to 57 0 to 9
65 to 90 A to Z (A to Z)
112 to 135 F1 to F24
8 BackSpace (BACKSPACE)
9 Tab
13 Enter (enter)
20 Caps_lock (Caps Lock)
32 Space (space bar)
37 Left (Rightwards Arrow)
38 Up (UP ARROW)
39 Right (Rightwards Arrow)
40 Down (down arrow)

In WEB applications, it is often possible to see the Event.keycode/event.which of the onkeydown event to get some of the user's keyboard operations to run some examples of use. If you press the CAPS LOCK key (20) when the user logs in, the CAPS Lock is indicated, and when the user presses the left and right arrows, the flip page is triggered.

After obtaining the Unicode encoded value, if you need to get the actual corresponding key value, you can get it through the fromCharCode method (String.fromCharCode ()) of the Srting object. Note that the characters obtained are always uppercase, and for some other function keys, the resulting characters may not be easy to read.

PS: Here we recommend an online query tool for JS event, and summarize the usual event types and functions of JS:

JavaScript event and Function description Daquan:

Http://tools.jb51.net/table/javascript_event

JavaScript onkeydown Event Starter instance (keyboard key pressed)

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.