<script language= "JavaScript" >function KeyEvent () {if (event.keycode==13) alert ("You hit Enter");} Document.onkeydown = keyevent;//Register Document.onkeydown Event
</script>
When the user presses the ENTER key, the KeyEvent () function is executed. Can do a carriage return to submit the form, or other events!
KeyCode is the code that returns the key to the KeyDown what KeyUp event occurred, and the Unicode character of the KeyPress event, and if you want to use a key combination, you can take advantage of event.ctrlkey,event.shiftkey,event. Altkey determines whether the CTRL key, the SHIFT key, and the ALT key are pressed.
| Common KeyCode |
| Key |
Code |
Key |
Code |
| Left |
37 |
Up |
38 |
| Right |
39 |
Down |
40 |
| 1 |
49 |
2 |
50 |
| 3 |
51 |
A |
65 |
| Shift |
16 |
Ctrl |
17 |
| Alt |
18 |
Enter |
13 |
Example:
<! DOCTYPE HTML>
< HTML >
< Head >
< title > keyboard events keycode</title>
</ Head >
< Body >
left up right down<BR />
CTRL + Up ctrl + down<br/>
CTRL + 1 ctrl + 2 & Nbsp; ctrl + 3<br/>
A ctrl + a ctrl + SHIFT + a<br/>
< Script type="Text/javascript">
Document.onkeydown = function (event) {
Event = Event | | window.event;
Bctrlkey = Event.ctrlkey;
Bshiftkey = Event.shiftkey;
Switch (event.keycode) {
Case 37:
Alert ("left");
Break
Case 38:
if (Bctrlkey) {
Alert ("Ctrl+up");
Break
}
Alert ("Up");
Break
Case 39:
Alert ("right");
Break
Case 40:
if (Bctrlkey) {
Alert ("Ctrl+down");
Break
}
Alert ("Down");
Break
Case 49:
Bctrlkey && (Alert ("ctrl+1"));
Break
Case 50:
Bctrlkey && (Alert ("ctrl+2"));
Break
Case 51:
Bctrlkey && (Alert ("ctrl+3"));
Break
Case 65:
if (Bctrlkey) {
if (Bshiftkey) {
Alert ("Custom");
Break
}
Alert ("Select All");
Break
}
Alert ("a");
Break
Case 67:
Bctrlkey && (Alert ("Copy"));
Break
Case 86:
Bctrlkey && (Alert ("paste"));
Break
}
return false;
}
</ Script >
</ Body >
</ HTML >
---------------------------------------------------------------------------------------------
<script language= "javascript" >
function KeyEvent () {
if (event.keycode==13)
Alert ("#$%#%#^^%");
}
document. onkeydown = KeyEvent;
</script>
The Event.keycode value for 37,38,39,40 corresponds to the pressed direction key divided by left, top, right, bottom
"Javascript:if (Event.keycode < 45 | | Event.keycode >) Event.returnvalue = false;");
if (event.keycode==13) on behalf of the press is the carriage return keyboard
"javascript:if (Event.keycode > && event.keycode <) Event.returnvalue = false;");
----------------------------------------------------------------------------------------------
< reference: Http://lfsp.btwlo.com/keycode.html & http://www.cnblogs.com/xingqiba/archive/2012/06/12/2546148.html & http://www.cnblogs.com/kuikui/archive/2012/02/09/2343645.html>
JS KeyCode Event Response