OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2009 Apple Inc. All rights reserved. | 2 * Copyright (C) 2009 Apple Inc. All rights reserved. |
3 * Copyright (C) 2009 Google Inc. All rights reserved. | 3 * Copyright (C) 2009 Google Inc. All rights reserved. |
4 * | 4 * |
5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
6 * modification, are permitted provided that the following conditions | 6 * modification, are permitted provided that the following conditions |
7 * are met: | 7 * are met: |
8 * | 8 * |
9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
(...skipping 14 matching lines...) Expand all Loading... |
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 */ | 28 */ |
29 | 29 |
30 /** | 30 /** |
31 * @constructor | 31 * @constructor |
32 */ | 32 */ |
33 WebInspector.KeyboardShortcut = function() | 33 WebInspector.KeyboardShortcut = function() |
34 { | 34 { |
35 } | 35 }; |
36 | 36 |
37 /** | 37 /** |
38 * Constants for encoding modifier key set as a bit mask. | 38 * Constants for encoding modifier key set as a bit mask. |
39 * @see #_makeKeyFromCodeAndModifiers | 39 * @see #_makeKeyFromCodeAndModifiers |
40 */ | 40 */ |
41 WebInspector.KeyboardShortcut.Modifiers = { | 41 WebInspector.KeyboardShortcut.Modifiers = { |
42 None: 0, // Constant for empty modifiers set. | 42 None: 0, // Constant for empty modifiers set. |
43 Shift: 1, | 43 Shift: 1, |
44 Ctrl: 2, | 44 Ctrl: 2, |
45 Alt: 4, | 45 Alt: 4, |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 * @param {number|string} keyCode The code of the key, or a character "a-z" whic
h is converted to a keyCode value. | 136 * @param {number|string} keyCode The code of the key, or a character "a-z" whic
h is converted to a keyCode value. |
137 * @param {number=} modifiers Optional list of modifiers passed as additional pa
rameters. | 137 * @param {number=} modifiers Optional list of modifiers passed as additional pa
rameters. |
138 * @return {number} | 138 * @return {number} |
139 */ | 139 */ |
140 WebInspector.KeyboardShortcut.makeKey = function(keyCode, modifiers) | 140 WebInspector.KeyboardShortcut.makeKey = function(keyCode, modifiers) |
141 { | 141 { |
142 if (typeof keyCode === "string") | 142 if (typeof keyCode === "string") |
143 keyCode = keyCode.charCodeAt(0) - (/^[a-z]/.test(keyCode) ? 32 : 0); | 143 keyCode = keyCode.charCodeAt(0) - (/^[a-z]/.test(keyCode) ? 32 : 0); |
144 modifiers = modifiers || WebInspector.KeyboardShortcut.Modifiers.None; | 144 modifiers = modifiers || WebInspector.KeyboardShortcut.Modifiers.None; |
145 return WebInspector.KeyboardShortcut._makeKeyFromCodeAndModifiers(keyCode, m
odifiers); | 145 return WebInspector.KeyboardShortcut._makeKeyFromCodeAndModifiers(keyCode, m
odifiers); |
146 } | 146 }; |
147 | 147 |
148 /** | 148 /** |
149 * @param {?KeyboardEvent} keyboardEvent | 149 * @param {?KeyboardEvent} keyboardEvent |
150 * @return {number} | 150 * @return {number} |
151 */ | 151 */ |
152 WebInspector.KeyboardShortcut.makeKeyFromEvent = function(keyboardEvent) | 152 WebInspector.KeyboardShortcut.makeKeyFromEvent = function(keyboardEvent) |
153 { | 153 { |
154 var modifiers = WebInspector.KeyboardShortcut.Modifiers.None; | 154 var modifiers = WebInspector.KeyboardShortcut.Modifiers.None; |
155 if (keyboardEvent.shiftKey) | 155 if (keyboardEvent.shiftKey) |
156 modifiers |= WebInspector.KeyboardShortcut.Modifiers.Shift; | 156 modifiers |= WebInspector.KeyboardShortcut.Modifiers.Shift; |
157 if (keyboardEvent.ctrlKey) | 157 if (keyboardEvent.ctrlKey) |
158 modifiers |= WebInspector.KeyboardShortcut.Modifiers.Ctrl; | 158 modifiers |= WebInspector.KeyboardShortcut.Modifiers.Ctrl; |
159 if (keyboardEvent.altKey) | 159 if (keyboardEvent.altKey) |
160 modifiers |= WebInspector.KeyboardShortcut.Modifiers.Alt; | 160 modifiers |= WebInspector.KeyboardShortcut.Modifiers.Alt; |
161 if (keyboardEvent.metaKey) | 161 if (keyboardEvent.metaKey) |
162 modifiers |= WebInspector.KeyboardShortcut.Modifiers.Meta; | 162 modifiers |= WebInspector.KeyboardShortcut.Modifiers.Meta; |
163 | 163 |
164 // Use either a real or a synthetic keyCode (for events originating from ext
ensions). | 164 // Use either a real or a synthetic keyCode (for events originating from ext
ensions). |
165 var keyCode = keyboardEvent.keyCode || keyboardEvent["__keyCode"]; | 165 var keyCode = keyboardEvent.keyCode || keyboardEvent["__keyCode"]; |
166 return WebInspector.KeyboardShortcut._makeKeyFromCodeAndModifiers(keyCode, m
odifiers); | 166 return WebInspector.KeyboardShortcut._makeKeyFromCodeAndModifiers(keyCode, m
odifiers); |
167 } | 167 }; |
168 | 168 |
169 /** | 169 /** |
170 * @param {?KeyboardEvent} keyboardEvent | 170 * @param {?KeyboardEvent} keyboardEvent |
171 * @return {number} | 171 * @return {number} |
172 */ | 172 */ |
173 WebInspector.KeyboardShortcut.makeKeyFromEventIgnoringModifiers = function(keybo
ardEvent) | 173 WebInspector.KeyboardShortcut.makeKeyFromEventIgnoringModifiers = function(keybo
ardEvent) |
174 { | 174 { |
175 var keyCode = keyboardEvent.keyCode || keyboardEvent["__keyCode"]; | 175 var keyCode = keyboardEvent.keyCode || keyboardEvent["__keyCode"]; |
176 return WebInspector.KeyboardShortcut._makeKeyFromCodeAndModifiers(keyCode, W
ebInspector.KeyboardShortcut.Modifiers.None); | 176 return WebInspector.KeyboardShortcut._makeKeyFromCodeAndModifiers(keyCode, W
ebInspector.KeyboardShortcut.Modifiers.None); |
177 } | 177 }; |
178 | 178 |
179 /** | 179 /** |
180 * @param {(?KeyboardEvent|?MouseEvent)} event | 180 * @param {(?KeyboardEvent|?MouseEvent)} event |
181 * @return {boolean} | 181 * @return {boolean} |
182 */ | 182 */ |
183 WebInspector.KeyboardShortcut.eventHasCtrlOrMeta = function(event) | 183 WebInspector.KeyboardShortcut.eventHasCtrlOrMeta = function(event) |
184 { | 184 { |
185 return WebInspector.isMac() ? event.metaKey && !event.ctrlKey : event.ctrlKe
y && !event.metaKey; | 185 return WebInspector.isMac() ? event.metaKey && !event.ctrlKey : event.ctrlKe
y && !event.metaKey; |
186 } | 186 }; |
187 | 187 |
188 /** | 188 /** |
189 * @param {!Event} event | 189 * @param {!Event} event |
190 * @return {boolean} | 190 * @return {boolean} |
191 */ | 191 */ |
192 WebInspector.KeyboardShortcut.hasNoModifiers = function(event) | 192 WebInspector.KeyboardShortcut.hasNoModifiers = function(event) |
193 { | 193 { |
194 return !event.ctrlKey && !event.shiftKey && !event.altKey && !event.metaKey; | 194 return !event.ctrlKey && !event.shiftKey && !event.altKey && !event.metaKey; |
195 } | 195 }; |
196 | 196 |
197 /** @typedef {!{key: number, name: string}} */ | 197 /** @typedef {!{key: number, name: string}} */ |
198 WebInspector.KeyboardShortcut.Descriptor; | 198 WebInspector.KeyboardShortcut.Descriptor; |
199 | 199 |
200 /** | 200 /** |
201 * @param {string|!WebInspector.KeyboardShortcut.Key} key | 201 * @param {string|!WebInspector.KeyboardShortcut.Key} key |
202 * @param {number=} modifiers | 202 * @param {number=} modifiers |
203 * @return {!WebInspector.KeyboardShortcut.Descriptor} | 203 * @return {!WebInspector.KeyboardShortcut.Descriptor} |
204 */ | 204 */ |
205 WebInspector.KeyboardShortcut.makeDescriptor = function(key, modifiers) | 205 WebInspector.KeyboardShortcut.makeDescriptor = function(key, modifiers) |
206 { | 206 { |
207 return { | 207 return { |
208 key: WebInspector.KeyboardShortcut.makeKey(typeof key === "string" ? key
: key.code, modifiers), | 208 key: WebInspector.KeyboardShortcut.makeKey(typeof key === "string" ? key
: key.code, modifiers), |
209 name: WebInspector.KeyboardShortcut.shortcutToString(key, modifiers) | 209 name: WebInspector.KeyboardShortcut.shortcutToString(key, modifiers) |
210 }; | 210 }; |
211 } | 211 }; |
212 | 212 |
213 /** | 213 /** |
214 * @param {string} shortcut | 214 * @param {string} shortcut |
215 * @return {?WebInspector.KeyboardShortcut.Descriptor} | 215 * @return {?WebInspector.KeyboardShortcut.Descriptor} |
216 */ | 216 */ |
217 WebInspector.KeyboardShortcut.makeDescriptorFromBindingShortcut = function(short
cut) | 217 WebInspector.KeyboardShortcut.makeDescriptorFromBindingShortcut = function(short
cut) |
218 { | 218 { |
219 var parts = shortcut.split(/\+(?!$)/); | 219 var parts = shortcut.split(/\+(?!$)/); |
220 var modifiers = 0; | 220 var modifiers = 0; |
221 var keyString; | 221 var keyString; |
222 for (var i = 0; i < parts.length; ++i) { | 222 for (var i = 0; i < parts.length; ++i) { |
223 if (typeof WebInspector.KeyboardShortcut.Modifiers[parts[i]] !== "undefi
ned") { | 223 if (typeof WebInspector.KeyboardShortcut.Modifiers[parts[i]] !== "undefi
ned") { |
224 modifiers |= WebInspector.KeyboardShortcut.Modifiers[parts[i]]; | 224 modifiers |= WebInspector.KeyboardShortcut.Modifiers[parts[i]]; |
225 continue; | 225 continue; |
226 } | 226 } |
227 console.assert(i === parts.length - 1, "Only one key other than modifier
is allowed in shortcut <" + shortcut + ">"); | 227 console.assert(i === parts.length - 1, "Only one key other than modifier
is allowed in shortcut <" + shortcut + ">"); |
228 keyString = parts[i]; | 228 keyString = parts[i]; |
229 break; | 229 break; |
230 } | 230 } |
231 console.assert(keyString, "Modifiers-only shortcuts are not allowed (encount
ered <" + shortcut + ">)"); | 231 console.assert(keyString, "Modifiers-only shortcuts are not allowed (encount
ered <" + shortcut + ">)"); |
232 if (!keyString) | 232 if (!keyString) |
233 return null; | 233 return null; |
234 | 234 |
235 var key = WebInspector.KeyboardShortcut.Keys[keyString] || WebInspector.Keyb
oardShortcut.KeyBindings[keyString]; | 235 var key = WebInspector.KeyboardShortcut.Keys[keyString] || WebInspector.Keyb
oardShortcut.KeyBindings[keyString]; |
236 if (key && key.shiftKey) | 236 if (key && key.shiftKey) |
237 modifiers |= WebInspector.KeyboardShortcut.Modifiers.Shift; | 237 modifiers |= WebInspector.KeyboardShortcut.Modifiers.Shift; |
238 return WebInspector.KeyboardShortcut.makeDescriptor(key ? key : keyString, m
odifiers); | 238 return WebInspector.KeyboardShortcut.makeDescriptor(key ? key : keyString, m
odifiers); |
239 } | 239 }; |
240 | 240 |
241 /** | 241 /** |
242 * @param {string|!WebInspector.KeyboardShortcut.Key} key | 242 * @param {string|!WebInspector.KeyboardShortcut.Key} key |
243 * @param {number=} modifiers | 243 * @param {number=} modifiers |
244 * @return {string} | 244 * @return {string} |
245 */ | 245 */ |
246 WebInspector.KeyboardShortcut.shortcutToString = function(key, modifiers) | 246 WebInspector.KeyboardShortcut.shortcutToString = function(key, modifiers) |
247 { | 247 { |
248 return WebInspector.KeyboardShortcut._modifiersToString(modifiers) + WebInsp
ector.KeyboardShortcut._keyName(key); | 248 return WebInspector.KeyboardShortcut._modifiersToString(modifiers) + WebInsp
ector.KeyboardShortcut._keyName(key); |
249 } | 249 }; |
250 | 250 |
251 /** | 251 /** |
252 * @param {string|!WebInspector.KeyboardShortcut.Key} key | 252 * @param {string|!WebInspector.KeyboardShortcut.Key} key |
253 * @return {string} | 253 * @return {string} |
254 */ | 254 */ |
255 WebInspector.KeyboardShortcut._keyName = function(key) | 255 WebInspector.KeyboardShortcut._keyName = function(key) |
256 { | 256 { |
257 if (typeof key === "string") | 257 if (typeof key === "string") |
258 return key.toUpperCase(); | 258 return key.toUpperCase(); |
259 if (typeof key.name === "string") | 259 if (typeof key.name === "string") |
260 return key.name; | 260 return key.name; |
261 return key.name[WebInspector.platform()] || key.name.other || ""; | 261 return key.name[WebInspector.platform()] || key.name.other || ""; |
262 } | 262 }; |
263 | 263 |
264 /** | 264 /** |
265 * @param {number} keyCode | 265 * @param {number} keyCode |
266 * @param {?number} modifiers | 266 * @param {?number} modifiers |
267 * @return {number} | 267 * @return {number} |
268 */ | 268 */ |
269 WebInspector.KeyboardShortcut._makeKeyFromCodeAndModifiers = function(keyCode, m
odifiers) | 269 WebInspector.KeyboardShortcut._makeKeyFromCodeAndModifiers = function(keyCode, m
odifiers) |
270 { | 270 { |
271 return (keyCode & 255) | (modifiers << 8); | 271 return (keyCode & 255) | (modifiers << 8); |
272 }; | 272 }; |
273 | 273 |
274 /** | 274 /** |
275 * @param {number} key | 275 * @param {number} key |
276 * @return {!{keyCode: number, modifiers: number}} | 276 * @return {!{keyCode: number, modifiers: number}} |
277 */ | 277 */ |
278 WebInspector.KeyboardShortcut.keyCodeAndModifiersFromKey = function(key) | 278 WebInspector.KeyboardShortcut.keyCodeAndModifiersFromKey = function(key) |
279 { | 279 { |
280 return { keyCode: key & 255, modifiers: key >> 8 }; | 280 return { keyCode: key & 255, modifiers: key >> 8 }; |
281 } | 281 }; |
282 | 282 |
283 /** | 283 /** |
284 * @param {number|undefined} modifiers | 284 * @param {number|undefined} modifiers |
285 * @return {string} | 285 * @return {string} |
286 */ | 286 */ |
287 WebInspector.KeyboardShortcut._modifiersToString = function(modifiers) | 287 WebInspector.KeyboardShortcut._modifiersToString = function(modifiers) |
288 { | 288 { |
289 var isMac = WebInspector.isMac(); | 289 var isMac = WebInspector.isMac(); |
290 var m = WebInspector.KeyboardShortcut.Modifiers; | 290 var m = WebInspector.KeyboardShortcut.Modifiers; |
291 var modifierNames = new Map([ | 291 var modifierNames = new Map([ |
292 [m.Ctrl, isMac ? "Ctrl\u2004" : "Ctrl\u200A+\u200A"], | 292 [m.Ctrl, isMac ? "Ctrl\u2004" : "Ctrl\u200A+\u200A"], |
293 [m.Alt, isMac ? "opt\u2004" : "Alt\u200A+\u200A"], | 293 [m.Alt, isMac ? "opt\u2004" : "Alt\u200A+\u200A"], |
294 [m.Shift, isMac ? "\u21e7\u2004" : "Shift\u200A+\u200A"], | 294 [m.Shift, isMac ? "\u21e7\u2004" : "Shift\u200A+\u200A"], |
295 [m.Meta, isMac ? "\u2318\u2004" : "Win\u200A+\u200A"] | 295 [m.Meta, isMac ? "\u2318\u2004" : "Win\u200A+\u200A"] |
296 ]); | 296 ]); |
297 return [m.Meta, m.Ctrl, m.Alt, m.Shift].map(mapModifiers).join(""); | 297 return [m.Meta, m.Ctrl, m.Alt, m.Shift].map(mapModifiers).join(""); |
298 | 298 |
299 /** | 299 /** |
300 * @param {number} m | 300 * @param {number} m |
301 * @return {string} | 301 * @return {string} |
302 */ | 302 */ |
303 function mapModifiers(m) | 303 function mapModifiers(m) |
304 { | 304 { |
305 return modifiers & m ? /** @type {string} */ (modifierNames.get(m)) : ""
; | 305 return modifiers & m ? /** @type {string} */ (modifierNames.get(m)) : ""
; |
306 } | 306 } |
307 }; | 307 }; |
308 | 308 |
309 WebInspector.KeyboardShortcut.SelectAll = WebInspector.KeyboardShortcut.makeKey(
"a", WebInspector.KeyboardShortcut.Modifiers.CtrlOrMeta); | 309 WebInspector.KeyboardShortcut.SelectAll = WebInspector.KeyboardShortcut.makeKey(
"a", WebInspector.KeyboardShortcut.Modifiers.CtrlOrMeta); |
OLD | NEW |