General keyboard shortcut JS function Encapsulation

Source: Internet
Author: User

Call directly when using:
JQuery (document). bind ('keylow', 'k', function (evt ){
// Write the function code executed by pressing the shortcut key here
});

The complete code is as follows:
/*
* JQuery Hotkeys Plugin
* Copyright 2010, John Resig
* Dual licensed under the MIT or GPL Version 2 licenses.
*
* Based upon the plugin by Tzury Bar Yochay:
* Http://github.com/tzuryby/hotkeys
*
* Original idea:
* Binny v a, http://www.openjs.com/scripts/events/keyboard_shortcuts/
*/

(Function (jQuery ){

JQuery. hotkeys = {
Version: "0.8 ",

SpecialKeys :{
8: "backspace", 9: "tab", 13: "return", 16: "shift", 17: "ctrl", 18: "alt", 19: "pause ",
20: "capslock", 27: "esc", 32: "space", 33: "pageup", 34: "pagedown", 35: "end", 36: "home ",
37: "left", 38: "up", 39: "right", 40: "down", 45: "insert", 46: "del ",
96: "0", 97: "1", 98: "2", 99: "3", 100: "4", 101: "5", 102: "6", 103: "7 ",
104: "8", 105: "9", 106: "*", 107: "+", 109: "-", 110 :". ", 111 :"/",
112: "f1", 113: "f2", 114: "f3", 115: "f4", 116: "f5", 117: "f6", 118: "f7", 119: "f8 ",
120: "f9", 121: "f10", 122: "f11", 123: "f12", 144: "numlock", 145: "scroll", 191: "/", 224: "meta"
},

ShiftNums :{
"'":"~ "," 1 ":"! "," 2 ":" @ "," 3 ":" # "," 4 ":" $ "," 5 ":" % "," 6 ": "^", "7 ":"&",
"8": "*", "9": "(", "0": ")", "-": "_", "=": "+ ", ";": ":", "'": "\" ",", ":" <",
".": "> ","/":"? "," \ ":" |"
}
};

Function keyHandler (handleObj ){
// Only care when a possible input has been specified
If (typeof handleObj. data! = "String "){
Return;
}

Var origHandler = handleObj. handler,
Keys = handleObj. data. toLowerCase (). split ("");

HandleObj. handler = function (event ){
// Don't fire in text-accepting inputs that we didn't directly bind
If (this! = Event.tar get & (/textarea | select/I. test (event.tar get. nodeName) |
Event.tar get. type = "text ")){
Return;
}

// Keypress represents characters, not special keys
Var special = event. type! = "Keypress" & jQuery. hotkeys. specialKeys [event. which],
Character = String. fromCharCode (event. which). toLowerCase (),
Key, modif = "", possible = {};

// Check combinations (alt | ctrl | shift + anything)
If (event. altKey & special! = "Alt "){
Modif + = "alt + ";
}

If (event. ctrlKey & special! = "Ctrl "){
Modif + = "ctrl + ";
}

// TODO: Need to make sure this works consistently implements SS platforms
If (event. metaKey &&! Event. ctrlKey & special! = "Meta "){
Modif + = "meta + ";
}

If (event. shiftKey & special! = "Shift "){
Modif + = "shift + ";
}

If (special ){
Possible [modif + special] = true;

} Else {
Possible [modif + character] = true;
Possible [modif + jQuery. hotkeys. shiftNums [character] = true;

// "$" Can be triggered as "Shift + 4" or "Shift + $" or just "$"
If (modif = "shift + "){
Possible [jQuery. hotkeys. shiftNums [character] = true;
}
}

For (var I = 0, l = keys. length; I <l; I ++ ){
If (possible [keys [I]) {
Return origHandler. apply (this, arguments );
}
}
};
}

JQuery. each (["keydown", "keyup", "keypress"], function (){
JQuery. event. special [this] = {add: keyHandler };
});

}) (JQuery );


Excerpted from code swallowed

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.