JavaScript onkeydown Event Entry instance (keyboard key pressed) _ Basics

Source: Internet
Author: User
Tags numeric value

JavaScript onkeydown Events

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

Tips

Internet explorer/chrome Browsers use Event.keycode to retrieve the pressed characters, 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 Code code 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 gets the numeric value (Unicode encoding) of 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 (SPACEBAR)
37 Left (left-hand arrow)
38 Up (UP ARROW)
39 Right (left arrow)
40 Down (down arrow)

In WEB applications, it is often possible to see the use of the onkeydown event Event.keycode/event.which to get some of the user's keyboard operations, thus running some examples of usage. If the lock key (20) is pressed when the user is logged on, it is prompted to lock the caps; when there is a page, if the user presses the left and right arrows, triggering up and down the page.

After the Unicode encoding value is obtained, the actual corresponding key value can be obtained by srting the fromCharCode method (String.fromCharCode ()) of the object. Note that the character is always uppercase and the resulting character may not be easy to read for some other function keys.

PS: Here again for you to recommend a JS event on the online query tool, summed up JS commonly used event types and function functions:

JavaScript event and Feature description encyclopedia:

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

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.