Corresponding table of keycode code in JavaScript (function implementation)

Source: Internet
Author: User
<HTML>
<! -- // This code by hongseheike -->
<Head>
<Script language = "JavaScript">
Ns4 = (document. layers )? True: false;
Ie4 = (document. All )? True: false;
Function keydown (e ){
If (ns4 ){
VaR nkey = E. Which;
VaR iekey = 'ns browser now ';
VaR realkey = string. fromcharcode (E. Which );
}
If (ie4 ){
VaR iekey = event. keycode;
VaR nkey = 'ie browser now ';
VaR realkey = string. fromcharcode (event. keycode );
If (event. keycode = 32) {realkey = '\ 'space \''}
If (event. keycode = 13) {realkey = '\' press enter \''}
If (event. keycode = 27) {realkey = '\ 'esc \''}
If (event. keycode = 16) {realkey = '\ 'shift \''}
If (event. keycode = 17) {realkey = '\ 'ctrl \''}
If (event. keycode = 18) {realkey = '\ 'alt \''}
}
Alert ('ns browser key value: '+ nkey +' \ n' + 'ie browser key value: '+ iekey +' \ n' + 'actual key is' + realkey );
}
Document. onkeydown = keydown;
If (ns4 ){
Document. captureevents (event. keydown );}
</SCRIPT>
</Head>
<Body>
// JavaScript document
<HR>
<Center>
<H3> press any key .... </H3>
</Center>
</Body>
</Html>

**************************************** ******************************
Use JavaScript to intercept keyboard input on the web page
Use the keycode attribute of the event object to determine the Input key value
Eg: If (event. keycode = 13) Alert ("Enter !");
Key value table
A 0x65 u 0x85
B 0x66 v 0x86
C 0x67 w 0x87
D 0x68X0x88
E 0x69 Y 0x89
F 0x70 Z 0x90
G 0x71 0 0x48
H 0x72 1 0x49
I 0x73 2 0x50
J 0x74 3 0x51
K 0x75 4 0x52
L 0x76 5 0x53
M 0x77 6 0x54
N 0x78 7 0x55
O 0x79 8 0x56
P 0x80 9 0x57
Q 0x81 ESC 0x1b
R 0x82 CTRL 0x11
S 0x83 shift 0x10
T 0x84 enter 0xd

If you want to use the combination key, you can use event. ctrlkey, event. shiftkey, event. altkey to determine whether Ctrl, shift, and ALT keys are 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.