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"