OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 var BASE_KEYBOARD = { | 5 var BASE_KEYBOARD = { |
6 top: 0, | 6 top: 0, |
7 left: 0, | 7 left: 0, |
8 width: 1237, | 8 width: 1237, |
9 height: 514 | 9 height: 514 |
10 }; | 10 }; |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
113 return result; | 113 return result; |
114 } | 114 } |
115 | 115 |
116 /** | 116 /** |
117 * Returns a list of modifiers from the key event. | 117 * Returns a list of modifiers from the key event. |
118 */ | 118 */ |
119 function getModifiers(e) { | 119 function getModifiers(e) { |
120 if (!e) { | 120 if (!e) { |
121 return []; | 121 return []; |
122 } | 122 } |
123 var isKeyDown = (e.type == 'keydown'); | |
124 var modifierToKeyCode = {'SHIFT': 16, 'CTRL': 17, 'ALT': 18}; | |
123 var modifiers = []; | 125 var modifiers = []; |
mazda
2011/04/18 11:00:20
Please move the declaration right before line 137.
| |
124 if (e.keyCode == 16 || e.shiftKey) { | 126 var modifier; |
mazda
2011/04/18 11:00:20
How about moving modifier to for-loop?
for (var m
| |
125 modifiers.push('SHIFT'); | 127 var pressed = {'SHIFT': e.shiftKey, 'CTRL': e.ctrlKey, 'ALT': e.altKey}; |
128 // if e.keyCode is one of Shift, Ctrl and Alt, pressed should | |
129 // be changed because the key currently pressed | |
130 // does not affect the values of e.shiftKey, e.ctrlKey and e.altKey | |
131 for (modifier in modifierToKeyCode) { | |
132 if (e.keyCode == modifierToKeyCode[modifier]) { | |
133 pressed[modifier] = isKeyDown; | |
134 } | |
126 } | 135 } |
127 if (e.keyCode == 17 || e.ctrlKey) { | 136 // make the result array |
128 modifiers.push('CTRL'); | 137 for (modifier in pressed) { |
129 } | 138 if (pressed[modifier]) { |
130 if (e.keyCode == 18 || e.altKey) { | 139 modifiers.push(modifier); |
131 modifiers.push('ALT'); | 140 } |
132 } | 141 } |
133 return modifiers.sort(); | 142 return modifiers.sort(); |
134 } | 143 } |
135 | 144 |
136 /** | 145 /** |
137 * Returns an ID of the key. | 146 * Returns an ID of the key. |
138 */ | 147 */ |
139 function keyId(identifier, i) { | 148 function keyId(identifier, i) { |
140 return identifier + '-key-' + i; | 149 return identifier + '-key-' + i; |
141 } | 150 } |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
258 if (pos && pos.length > 0) { | 267 if (pos && pos.length > 0) { |
259 chars.push(hex2char(pos)); | 268 chars.push(hex2char(pos)); |
260 } | 269 } |
261 } | 270 } |
262 return chars.join(' '); | 271 return chars.join(' '); |
263 } | 272 } |
264 | 273 |
265 /** | 274 /** |
266 * Updates the whole keyboard. | 275 * Updates the whole keyboard. |
267 */ | 276 */ |
268 function update(e) { | 277 function update(modifiers) { |
269 var modifiers = getModifiers(e); | |
270 var instructions = document.getElementById('instructions'); | 278 var instructions = document.getElementById('instructions'); |
271 if (modifiers.length == 0) { | 279 if (modifiers.length == 0) { |
272 instructions.style.visibility = 'visible'; | 280 instructions.style.visibility = 'visible'; |
273 } else { | 281 } else { |
274 instructions.style.visibility = 'hidden'; | 282 instructions.style.visibility = 'hidden'; |
275 } | 283 } |
276 | 284 |
277 var keyboardGlyphData = getKeyboardGlyphData(); | 285 var keyboardGlyphData = getKeyboardGlyphData(); |
278 var shortcutData = getShortcutData(); | 286 var shortcutData = getShortcutData(); |
279 var layout = getLayouts()[keyboardGlyphData.layoutName]; | 287 var layout = getLayouts()[keyboardGlyphData.layoutName]; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
318 keyText.style.textAlign = format; | 326 keyText.style.textAlign = format; |
319 } | 327 } |
320 } | 328 } |
321 } | 329 } |
322 } | 330 } |
323 | 331 |
324 /** | 332 /** |
325 * A callback furnction for the onkeydown event. | 333 * A callback furnction for the onkeydown event. |
326 */ | 334 */ |
327 function keydown(e) { | 335 function keydown(e) { |
336 var modifiers = getModifiers(e); | |
328 if (!getKeyboardOverlayId()) { | 337 if (!getKeyboardOverlayId()) { |
329 return; | 338 return; |
330 } | 339 } |
331 update(e); | 340 update(modifiers); |
332 } | 341 } |
333 | 342 |
334 /** | 343 /** |
335 * A callback furnction for the onkeyup event. | 344 * A callback furnction for the onkeyup event. |
336 */ | 345 */ |
337 function keyup(e) { | 346 function keyup(e) { |
mazda
2011/04/18 11:00:20
How about merging this function into keydown?
| |
347 var modifiers = getModifiers(e); | |
338 if (!getKeyboardOverlayId()) { | 348 if (!getKeyboardOverlayId()) { |
339 return; | 349 return; |
340 } | 350 } |
341 update(); | 351 update(modifiers); |
342 } | 352 } |
343 | 353 |
344 /** | 354 /** |
345 * Initializes the layout of the keys. | 355 * Initializes the layout of the keys. |
346 */ | 356 */ |
347 function initLayout() { | 357 function initLayout() { |
348 var layout = getLayouts()[getKeyboardGlyphData().layoutName]; | 358 var layout = getLayouts()[getKeyboardGlyphData().layoutName]; |
349 var keyboard = document.body; | 359 var keyboard = document.body; |
350 var minX = window.innerWidth; | 360 var minX = window.innerWidth; |
351 var maxX = 0; | 361 var maxX = 0; |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
441 keyboardOverlayId = overlayId; | 451 keyboardOverlayId = overlayId; |
442 } | 452 } |
443 while(document.body.firstChild) { | 453 while(document.body.firstChild) { |
444 document.body.removeChild(document.body.firstChild); | 454 document.body.removeChild(document.body.firstChild); |
445 } | 455 } |
446 initLayout(); | 456 initLayout(); |
447 update(); | 457 update(); |
448 } | 458 } |
449 | 459 |
450 document.addEventListener('DOMContentLoaded', init); | 460 document.addEventListener('DOMContentLoaded', init); |
OLD | NEW |