提交 | 用户 | 时间
|
58d006
|
1 |
/*jslint browser: true*/ |
A |
2 |
/*jslint jquery: true*/ |
|
3 |
|
|
4 |
/* |
|
5 |
* jQuery Hotkeys Plugin |
|
6 |
* Copyright 2010, John Resig |
|
7 |
* Dual licensed under the MIT or GPL Version 2 licenses. |
|
8 |
* |
|
9 |
* Based upon the plugin by Tzury Bar Yochay: |
|
10 |
* http://github.com/tzuryby/hotkeys |
|
11 |
* |
|
12 |
* Original idea by: |
|
13 |
* Binny V A, http://www.openjs.com/scripts/events/keyboard_shortcuts/ |
|
14 |
*/ |
|
15 |
|
|
16 |
/* |
|
17 |
* One small change is: now keys are passed by object { keys: '...' } |
|
18 |
* Might be useful, when you want to pass some other data to your handler |
|
19 |
*/ |
|
20 |
|
|
21 |
(function(jQuery) { |
|
22 |
|
|
23 |
jQuery.hotkeys = { |
|
24 |
version: "0.8", |
|
25 |
|
|
26 |
specialKeys: { |
|
27 |
8: "backspace", |
|
28 |
9: "tab", |
|
29 |
10: "return", |
|
30 |
13: "return", |
|
31 |
16: "shift", |
|
32 |
17: "ctrl", |
|
33 |
18: "alt", |
|
34 |
19: "pause", |
|
35 |
20: "capslock", |
|
36 |
27: "esc", |
|
37 |
32: "space", |
|
38 |
33: "pageup", |
|
39 |
34: "pagedown", |
|
40 |
35: "end", |
|
41 |
36: "home", |
|
42 |
37: "left", |
|
43 |
38: "up", |
|
44 |
39: "right", |
|
45 |
40: "down", |
|
46 |
45: "insert", |
|
47 |
46: "del", |
|
48 |
59: ";", |
|
49 |
61: "=", |
|
50 |
96: "0", |
|
51 |
97: "1", |
|
52 |
98: "2", |
|
53 |
99: "3", |
|
54 |
100: "4", |
|
55 |
101: "5", |
|
56 |
102: "6", |
|
57 |
103: "7", |
|
58 |
104: "8", |
|
59 |
105: "9", |
|
60 |
106: "*", |
|
61 |
107: "+", |
|
62 |
109: "-", |
|
63 |
110: ".", |
|
64 |
111: "/", |
|
65 |
112: "f1", |
|
66 |
113: "f2", |
|
67 |
114: "f3", |
|
68 |
115: "f4", |
|
69 |
116: "f5", |
|
70 |
117: "f6", |
|
71 |
118: "f7", |
|
72 |
119: "f8", |
|
73 |
120: "f9", |
|
74 |
121: "f10", |
|
75 |
122: "f11", |
|
76 |
123: "f12", |
|
77 |
144: "numlock", |
|
78 |
145: "scroll", |
|
79 |
173: "-", |
|
80 |
186: ";", |
|
81 |
187: "=", |
|
82 |
188: ",", |
|
83 |
189: "-", |
|
84 |
190: ".", |
|
85 |
191: "/", |
|
86 |
192: "`", |
|
87 |
219: "[", |
|
88 |
220: "\\", |
|
89 |
221: "]", |
|
90 |
222: "'" |
|
91 |
}, |
|
92 |
|
|
93 |
shiftNums: { |
|
94 |
"`": "~", |
|
95 |
"1": "!", |
|
96 |
"2": "@", |
|
97 |
"3": "#", |
|
98 |
"4": "$", |
|
99 |
"5": "%", |
|
100 |
"6": "^", |
|
101 |
"7": "&", |
|
102 |
"8": "*", |
|
103 |
"9": "(", |
|
104 |
"0": ")", |
|
105 |
"-": "_", |
|
106 |
"=": "+", |
|
107 |
";": ": ", |
|
108 |
"'": "\"", |
|
109 |
",": "<", |
|
110 |
".": ">", |
|
111 |
"/": "?", |
|
112 |
"\\": "|" |
|
113 |
}, |
|
114 |
|
|
115 |
// excludes: button, checkbox, file, hidden, image, password, radio, reset, search, submit, url |
|
116 |
textAcceptingInputTypes: [ |
|
117 |
"text", "password", "number", "email", "url", "range", "date", "month", "week", "time", "datetime", |
|
118 |
"datetime-local", "search", "color", "tel"], |
|
119 |
|
|
120 |
options: { |
|
121 |
filterTextInputs: true |
|
122 |
} |
|
123 |
}; |
|
124 |
|
|
125 |
function keyHandler(handleObj) { |
|
126 |
if (typeof handleObj.data === "string") { |
|
127 |
handleObj.data = { |
|
128 |
keys: handleObj.data |
|
129 |
}; |
|
130 |
} |
|
131 |
|
|
132 |
// Only care when a possible input has been specified |
|
133 |
if (!handleObj.data || !handleObj.data.keys || typeof handleObj.data.keys !== "string") { |
|
134 |
return; |
|
135 |
} |
|
136 |
|
|
137 |
var origHandler = handleObj.handler, |
|
138 |
keys = handleObj.data.keys.toLowerCase().split(" "); |
|
139 |
|
|
140 |
handleObj.handler = function(event) { |
|
141 |
// Don't fire in text-accepting inputs that we didn't directly bind to |
|
142 |
if (this !== event.target && (/textarea|select/i.test(event.target.nodeName) || |
|
143 |
(jQuery.hotkeys.options.filterTextInputs && |
|
144 |
jQuery.inArray(event.target.type, jQuery.hotkeys.textAcceptingInputTypes) > -1))) { |
|
145 |
return; |
|
146 |
} |
|
147 |
|
|
148 |
var special = event.type !== "keypress" && jQuery.hotkeys.specialKeys[event.which], |
|
149 |
character = String.fromCharCode(event.which).toLowerCase(), |
|
150 |
modif = "", |
|
151 |
possible = {}; |
|
152 |
|
|
153 |
jQuery.each(["alt", "ctrl", "shift"], function(index, specialKey) { |
|
154 |
|
|
155 |
if (event[specialKey + 'Key'] && special !== specialKey) { |
|
156 |
modif += specialKey + '+'; |
|
157 |
} |
|
158 |
}); |
|
159 |
|
|
160 |
// metaKey is triggered off ctrlKey erronously |
|
161 |
if (event.metaKey && !event.ctrlKey && special !== "meta") { |
|
162 |
modif += "meta+"; |
|
163 |
} |
|
164 |
|
|
165 |
if (event.metaKey && special !== "meta" && modif.indexOf("alt+ctrl+shift+") > -1) { |
|
166 |
modif = modif.replace("alt+ctrl+shift+", "hyper+"); |
|
167 |
} |
|
168 |
|
|
169 |
if (special) { |
|
170 |
possible[modif + special] = true; |
|
171 |
} |
|
172 |
else { |
|
173 |
possible[modif + character] = true; |
|
174 |
possible[modif + jQuery.hotkeys.shiftNums[character]] = true; |
|
175 |
|
|
176 |
// "$" can be triggered as "Shift+4" or "Shift+$" or just "$" |
|
177 |
if (modif === "shift+") { |
|
178 |
possible[jQuery.hotkeys.shiftNums[character]] = true; |
|
179 |
} |
|
180 |
} |
|
181 |
|
|
182 |
for (var i = 0, l = keys.length; i < l; i++) { |
|
183 |
if (possible[keys[i]]) { |
|
184 |
return origHandler.apply(this, arguments); |
|
185 |
} |
|
186 |
} |
|
187 |
}; |
|
188 |
} |
|
189 |
|
|
190 |
jQuery.each(["keydown", "keyup", "keypress"], function() { |
|
191 |
jQuery.event.special[this] = { |
|
192 |
add: keyHandler |
|
193 |
}; |
|
194 |
}); |
|
195 |
|
|
196 |
})(jQuery || this.jQuery || window.jQuery); |