Copy codeThe Code is as follows:
(Function ($)
{
$. Extend ({
Key_fn: [], // stores the function corresponding to the bound character
Key_code: [], // stores characters
Key_bind: function (ch, callback ){
Var KeyCode = {a: 65, B: 66, c: 67, d: 68, e: 69, f: 70, g: 71, h: 72, I: 73, j: 74, k: 75, l: 76, m: 77, n: 78, o: 79, p: 80, q: 81, r: 82, s: 83, t: 84, u: 85, v: 86, w: 87, x: 88, y: 89, z: 90 };
If (KeyCode. hasOwnProperty (ch )){
$. Key_fn.push (callback );
$. Key_code.push (ch );
// You need to add an event for the first time.
If ($. key_fn.length = 1 ){
$ (Document). keypress (function (e ){
Var e = event | window. event;
Var k = e. keyCode | e. which;
For (var I = 0; I <$. key_fn.length; I ++ ){
//-32 compatible with lower case letters
If (k-32 = KeyCode [$. key_code [I] | k = KeyCode [$. key_code [I]) {
Log ('pressed binded key' + k );
$. Key_fn [I] ();
Break;
}
}
});
}
} Else
{
Alert ('binding events can only be letters ');
}
}
});
}) (JQuery );
The usage can be as follows:
Copy codeThe Code is as follows:
$. Key_bind ('F', set_table_full_screen );
$. Key_bind ('R', reloadthis );
Sometimes we need to add some shortcuts to the application. Every time we write the following code, we can easily bind a keyboard and corresponding operation functions through key_bin.
Copy codeThe Code is as follows:
$ (Document). keypress (function (){})