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 modifiers = []; | 123 var isKeyDown = (e.type == 'keydown'); |
124 if (e.keyCode == 16 || e.shiftKey) { | 124 var keyCodeToModifier = {16: 'SHIFT', 17: 'CTRL', 18: 'ALT'}; |
125 modifiers.push('SHIFT'); | 125 var modifierWithKeyCode = keyCodeToModifier[e.keyCode]; |
| 126 var isPressed = {'SHIFT': e.shiftKey, 'CTRL': e.ctrlKey, 'ALT': e.altKey}; |
| 127 // if e.keyCode is one of Shift, Ctrl and Alt, isPressed should |
| 128 // be changed because the key currently pressed |
| 129 // does not affect the values of e.shiftKey, e.ctrlKey and e.altKey |
| 130 if(modifierWithKeyCode){ |
| 131 isPressed[modifierWithKeyCode] = isKeyDown; |
126 } | 132 } |
127 if (e.keyCode == 17 || e.ctrlKey) { | 133 // make the result array |
128 modifiers.push('CTRL'); | 134 return ['SHIFT', 'CTRL', 'ALT'].filter( |
129 } | 135 function(modifier) { |
130 if (e.keyCode == 18 || e.altKey) { | 136 return isPressed[modifier]; |
131 modifiers.push('ALT'); | 137 }).sort(); |
132 } | |
133 return modifiers.sort(); | |
134 } | 138 } |
135 | 139 |
136 /** | 140 /** |
137 * Returns an ID of the key. | 141 * Returns an ID of the key. |
138 */ | 142 */ |
139 function keyId(identifier, i) { | 143 function keyId(identifier, i) { |
140 return identifier + '-key-' + i; | 144 return identifier + '-key-' + i; |
141 } | 145 } |
142 | 146 |
143 /** | 147 /** |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 if (pos && pos.length > 0) { | 262 if (pos && pos.length > 0) { |
259 chars.push(hex2char(pos)); | 263 chars.push(hex2char(pos)); |
260 } | 264 } |
261 } | 265 } |
262 return chars.join(' '); | 266 return chars.join(' '); |
263 } | 267 } |
264 | 268 |
265 /** | 269 /** |
266 * Updates the whole keyboard. | 270 * Updates the whole keyboard. |
267 */ | 271 */ |
268 function update(e) { | 272 function update(modifiers) { |
269 var modifiers = getModifiers(e); | |
270 var instructions = document.getElementById('instructions'); | 273 var instructions = document.getElementById('instructions'); |
271 if (modifiers.length == 0) { | 274 if (modifiers.length == 0) { |
272 instructions.style.visibility = 'visible'; | 275 instructions.style.visibility = 'visible'; |
273 } else { | 276 } else { |
274 instructions.style.visibility = 'hidden'; | 277 instructions.style.visibility = 'hidden'; |
275 } | 278 } |
276 | 279 |
277 var keyboardGlyphData = getKeyboardGlyphData(); | 280 var keyboardGlyphData = getKeyboardGlyphData(); |
278 var shortcutData = getShortcutData(); | 281 var shortcutData = getShortcutData(); |
279 var layout = getLayouts()[keyboardGlyphData.layoutName]; | 282 var layout = getLayouts()[keyboardGlyphData.layoutName]; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 var format = keyData.format; | 318 var format = keyData.format; |
316 if (format == 'left' || format == 'right') { | 319 if (format == 'left' || format == 'right') { |
317 shortcutText.style.textAlign = format; | 320 shortcutText.style.textAlign = format; |
318 keyText.style.textAlign = format; | 321 keyText.style.textAlign = format; |
319 } | 322 } |
320 } | 323 } |
321 } | 324 } |
322 } | 325 } |
323 | 326 |
324 /** | 327 /** |
325 * A callback furnction for the onkeydown event. | 328 * A callback function for onkeydown and onkeyup events. |
326 */ | 329 */ |
327 function keydown(e) { | 330 function handleKeyEvent(e){ |
| 331 var modifiers = getModifiers(e); |
328 if (!getKeyboardOverlayId()) { | 332 if (!getKeyboardOverlayId()) { |
329 return; | 333 return; |
330 } | 334 } |
331 update(e); | 335 update(modifiers); |
332 } | 336 } |
333 | 337 |
334 /** | 338 /** |
335 * A callback furnction for the onkeyup event. | |
336 */ | |
337 function keyup(e) { | |
338 if (!getKeyboardOverlayId()) { | |
339 return; | |
340 } | |
341 update(); | |
342 } | |
343 | |
344 /** | |
345 * Initializes the layout of the keys. | 339 * Initializes the layout of the keys. |
346 */ | 340 */ |
347 function initLayout() { | 341 function initLayout() { |
348 var layout = getLayouts()[getKeyboardGlyphData().layoutName]; | 342 var layout = getLayouts()[getKeyboardGlyphData().layoutName]; |
349 var keyboard = document.body; | 343 var keyboard = document.body; |
350 var minX = window.innerWidth; | 344 var minX = window.innerWidth; |
351 var maxX = 0; | 345 var maxX = 0; |
352 var minY = window.innerHeight; | 346 var minY = window.innerHeight; |
353 var maxY = 0; | 347 var maxY = 0; |
354 var multiplier = 1.38 * window.innerWidth / BASE_KEYBOARD.width; | 348 var multiplier = 1.38 * window.innerWidth / BASE_KEYBOARD.width; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
417 instructionsHideText.className = 'keyboard-overlay-instructions-hide-text'; | 411 instructionsHideText.className = 'keyboard-overlay-instructions-hide-text'; |
418 instructionsHideText.innerHTML = templateData.keyboardOverlayInstructionsHide; | 412 instructionsHideText.innerHTML = templateData.keyboardOverlayInstructionsHide; |
419 instructions.appendChild(instructionsHideText); | 413 instructions.appendChild(instructionsHideText); |
420 keyboard.appendChild(instructions); | 414 keyboard.appendChild(instructions); |
421 } | 415 } |
422 | 416 |
423 /** | 417 /** |
424 * A callback function for the onload event of the body element. | 418 * A callback function for the onload event of the body element. |
425 */ | 419 */ |
426 function init() { | 420 function init() { |
427 document.addEventListener('keydown', keydown); | 421 document.addEventListener('keydown', handleKeyEvent); |
428 document.addEventListener('keyup', keyup); | 422 document.addEventListener('keyup', handleKeyEvent); |
429 chrome.send('getKeyboardOverlayId'); | 423 chrome.send('getKeyboardOverlayId'); |
430 } | 424 } |
431 | 425 |
432 /** | 426 /** |
433 * Initializes the global keyboad overlay ID and the layout of keys. | 427 * Initializes the global keyboad overlay ID and the layout of keys. |
434 * Called after sending the 'getKeyboardOverlayId' message. | 428 * Called after sending the 'getKeyboardOverlayId' message. |
435 */ | 429 */ |
436 function initKeyboardOverlayId(overlayId) { | 430 function initKeyboardOverlayId(overlayId) { |
437 // Libcros returns an empty string when it cannot find the keyboard overlay ID | 431 // Libcros returns an empty string when it cannot find the keyboard overlay ID |
438 // corresponding to the current input method. | 432 // corresponding to the current input method. |
439 // In such a case, fallback to the default ID (en_US). | 433 // In such a case, fallback to the default ID (en_US). |
440 if (overlayId) { | 434 if (overlayId) { |
441 keyboardOverlayId = overlayId; | 435 keyboardOverlayId = overlayId; |
442 } | 436 } |
443 while(document.body.firstChild) { | 437 while(document.body.firstChild) { |
444 document.body.removeChild(document.body.firstChild); | 438 document.body.removeChild(document.body.firstChild); |
445 } | 439 } |
446 initLayout(); | 440 initLayout(); |
447 update(); | 441 update(); |
448 } | 442 } |
449 | 443 |
450 document.addEventListener('DOMContentLoaded', init); | 444 document.addEventListener('DOMContentLoaded', init); |
OLD | NEW |