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