KeyChar for Javascript Keyboard Events

Source: Internet
Author: User
Tags comparison table

Keydown, keyup, keypress event object in Javascript, keyCode! = KeyChar. Many people on the internet provide a comparison table between keyCode and keyChar. Therefore, according to the table of comparison, the following tool functions are generated. You only need to pass the event object as a parameter to get the keyChar. If the keyCode does not have the corresponding characters, the returned value is '\ 0 '.
Window. keyChar = (function (){
Var ssss = "";
Var SSSS = "";
 
// M and m are only rulers. The length of ssss and SSSS variables is used to align the rulers.
Var M = "0 1 2 3 4 ";
Var m = "01234567890123456789012345678901234567890123456789 ";
 
// Code table without shift
Ssss + = "01 ";
Ssss ++ = "23456789 abcdefghijklmnopqrstuvwxyz 0123 ";
Ssss + = "456789 * + -./";
Ssss + = "; = .-,/'";
Ssss + = "[] '";
 
// Code table with shift
SSSS + = ")! ";
SSSS + = "@ # $ % ^ & * (ABCDEFGHIJKLMNOPQRSTUVWXYZ 0123 ";
SSSS + = "456789 * + -./";
SSSS + = ": + >_< ?~ ";
SSSS + = "{| }";
 
// Split the code table into character Arrays
Var t = ssss. split ("");
Var T = SSSS. split ("");
 
// Replace all spaces with null characters
Var nullChar = String. fromCharCode (0 );
For (var I = 0; I <t. length; I ++ ){
(T [I] = "") & (t [I] = nullChar );
(T [I] = "") & (T [I] = nullChar );
}
 
// Several characters to be escaped, which will affect the layout in the table.
T [9] = "\ t ";
T [13] = "\ n ";
T [32] = "";
T [220] = "\\";
 
T [32] = "";
T [222] = '"';
 
Return function (event, isCheckShift ){
If (typeof isCheckShift = "undefined") {isCheckShift = true ;}
If (!! IsCheckShift & event. shiftKey ){
Return T [event. keyCode];
} Else {
Return t [event. keyCode];
}
};
})();
 
Author: "biancheng inn xuehai Wuxi"

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.