Administrator
2022-09-14 58d006e05dcf2a20d0ec5367dd03d66a61db6849
提交 | 用户 | 时间
58d006 1 /*
A 2  * jQuery Hotkeys Plugin
3  * Copyright 2010, John Resig
4  * Dual licensed under the MIT or GPL Version 2 licenses.
5  *
6  * Based upon the plugin by Tzury Bar Yochay:
7  * http://github.com/tzuryby/hotkeys
8  *
9  * Original idea by:
10  * Binny V A, http://www.openjs.com/scripts/events/keyboard_shortcuts/
11 */
12
13 (function(jQuery){
14
15     jQuery.hotkeys = {
16         version: "0.8+",
17
18         specialKeys: {
19             8: "backspace", 9: "tab", 13: "return", 16: "shift", 17: "ctrl", 18: "alt", 19: "pause",
20             20: "capslock", 27: "esc", 32: "space", 33: "pageup", 34: "pagedown", 35: "end", 36: "home",
21             37: "left", 38: "up", 39: "right", 40: "down", 45: "insert", 46: "del",
22             96: "0", 97: "1", 98: "2", 99: "3", 100: "4", 101: "5", 102: "6", 103: "7",
23             104: "8", 105: "9", 106: "*", 107: "+", 109: "-", 110: ".", 111 : "/",
24             112: "f1", 113: "f2", 114: "f3", 115: "f4", 116: "f5", 117: "f6", 118: "f7", 119: "f8",
25             120: "f9", 121: "f10", 122: "f11", 123: "f12", 144: "numlock", 145: "scroll", 188: ",", 190: ".",
26             191: "/", 224: "meta"
27         },
28
29         shiftNums: {
30             "`": "~", "1": "!", "2": "@", "3": "#", "4": "$", "5": "%", "6": "^", "7": "&",
31             "8": "*", "9": "(", "0": ")", "-": "_", "=": "+", ";": ": ", "'": "\"", ",": "<",
32             ".": ">",  "/": "?",  "\\": "|"
33         }
34     };
35
36     function keyHandler( handleObj ) {
37
38         var origHandler = handleObj.handler,
39             //use namespace as keys so it works with event delegation as well
40             //will also allow removing listeners of a specific key combination
41             //and support data objects
42             keys = (handleObj.namespace || "").toLowerCase().split(" ");
43             keys = jQuery.map(keys, function(key) { return key.split("."); });
44
45         //no need to modify handler if no keys specified
46         //Added keys[0].substring(0, 12) to work with jQuery ui 1.9.0
47         //Added accordion, tabs and menu, then jquery ui can use keys.
48
49             if (keys.length === 1 && (keys[0] === "" || 
50             keys[0].substring(0, 12) === "autocomplete"  || 
51             keys[0].substring(0, 9) === "accordion"  || 
52             keys[0].substring(0, 4) === "tabs"  || 
53             keys[0].substring(0, 4) === "menu")) {
54             return;
55         }
56
57         handleObj.handler = function( event ) {
58             // Don't fire in text-accepting inputs that we didn't directly bind to
59             // important to note that $.fn.prop is only available on jquery 1.6+
60             if ( this !== event.target && (/textarea|select/i.test( event.target.nodeName ) ||
61                 event.target.type === "text" || $(event.target).prop('contenteditable') == 'true' )) {
62                 return;
63             }
64
65             // Keypress represents characters, not special keys
66             var special = event.type !== "keypress" && jQuery.hotkeys.specialKeys[ event.which ],
67                 character = String.fromCharCode( event.which ).toLowerCase(),
68                 key, modif = "", possible = {};
69
70             // check combinations (alt|ctrl|shift+anything)
71             if ( event.altKey && special !== "alt" ) {
72                 modif += "alt_";
73             }
74
75             if ( event.ctrlKey && special !== "ctrl" ) {
76                 modif += "ctrl_";
77             }
78
79             // TODO: Need to make sure this works consistently across platforms
80             if ( event.metaKey && !event.ctrlKey && special !== "meta" ) {
81                 modif += "meta_";
82             }
83
84             if ( event.shiftKey && special !== "shift" ) {
85                 modif += "shift_";
86             }
87
88             if ( special ) {
89                 possible[ modif + special ] = true;
90
91             } else {
92                 possible[ modif + character ] = true;
93                 possible[ modif + jQuery.hotkeys.shiftNums[ character ] ] = true;
94
95                 // "$" can be triggered as "Shift+4" or "Shift+$" or just "$"
96                 if ( modif === "shift_" ) {
97                     possible[ jQuery.hotkeys.shiftNums[ character ] ] = true;
98                 }
99             }
100
101             for ( var i = 0, l = keys.length; i < l; i++ ) {
102                 if ( possible[ keys[i] ] ) {
103                     return origHandler.apply( this, arguments );
104                 }
105             }
106         };
107     }
108
109     jQuery.each([ "keydown", "keyup", "keypress" ], function() {
110         jQuery.event.special[ this ] = { add: keyHandler };
111     });
112
113 })( jQuery );