| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 /** | 5 /** |
| 6 * @fileoverview Script for ChromeOS keyboard explorer. | 6 * @fileoverview Script for ChromeOS keyboard explorer. |
| 7 * | 7 * |
| 8 */ | 8 */ |
| 9 | 9 |
| 10 goog.provide('cvox.KbExplorer'); | 10 goog.provide('cvox.KbExplorer'); |
| 11 | 11 |
| 12 goog.require('BrailleCommandHandler'); |
| 13 goog.require('Spannable'); |
| 14 goog.require('cvox.BrailleKeyCommand'); |
| 12 goog.require('cvox.ChromeVoxKbHandler'); | 15 goog.require('cvox.ChromeVoxKbHandler'); |
| 13 goog.require('cvox.CommandStore'); | 16 goog.require('cvox.CommandStore'); |
| 14 goog.require('cvox.KeyMap'); | 17 goog.require('cvox.KeyMap'); |
| 15 goog.require('cvox.KeyUtil'); | 18 goog.require('cvox.KeyUtil'); |
| 16 | 19 |
| 17 | |
| 18 /** | 20 /** |
| 19 * Class to manage the keyboard explorer. | 21 * Class to manage the keyboard explorer. |
| 20 * @constructor | 22 * @constructor |
| 21 */ | 23 */ |
| 22 cvox.KbExplorer = function() { }; | 24 cvox.KbExplorer = function() { }; |
| 23 | 25 |
| 24 | 26 |
| 25 /** | 27 /** |
| 26 * Initialize keyboard explorer. | 28 * Initialize keyboard explorer. |
| 27 */ | 29 */ |
| 28 cvox.KbExplorer.init = function() { | 30 cvox.KbExplorer.init = function() { |
| 29 var backgroundWindow = chrome.extension.getBackgroundPage(); | 31 var backgroundWindow = chrome.extension.getBackgroundPage(); |
| 30 backgroundWindow.addEventListener( | 32 backgroundWindow.addEventListener( |
| 31 'keydown', cvox.KbExplorer.onKeyDown, true); | 33 'keydown', cvox.KbExplorer.onKeyDown, true); |
| 32 backgroundWindow.addEventListener('keyup', cvox.KbExplorer.onKeyUp, true); | 34 backgroundWindow.addEventListener('keyup', cvox.KbExplorer.onKeyUp, true); |
| 33 backgroundWindow.addEventListener( | 35 backgroundWindow.addEventListener( |
| 34 'keypress', cvox.KbExplorer.onKeyPress, true); | 36 'keypress', cvox.KbExplorer.onKeyPress, true); |
| 37 chrome.brailleDisplayPrivate.onKeyEvent.addListener( |
| 38 cvox.KbExplorer.onBrailleKeyEvent); |
| 39 chrome.accessibilityPrivate.onAccessibilityGesture.addListener( |
| 40 cvox.KbExplorer.onAccessibilityGesture); |
| 35 | 41 |
| 36 window.onbeforeunload = function(evt) { | 42 window.onbeforeunload = function(evt) { |
| 37 backgroundWindow.removeEventListener( | 43 backgroundWindow.removeEventListener( |
| 38 'keydown', cvox.KbExplorer.onKeyDown, true); | 44 'keydown', cvox.KbExplorer.onKeyDown, true); |
| 39 backgroundWindow.removeEventListener( | 45 backgroundWindow.removeEventListener( |
| 40 'keyup', cvox.KbExplorer.onKeyUp, true); | 46 'keyup', cvox.KbExplorer.onKeyUp, true); |
| 41 backgroundWindow.removeEventListener( | 47 backgroundWindow.removeEventListener( |
| 42 'keypress', cvox.KbExplorer.onKeyPress, true); | 48 'keypress', cvox.KbExplorer.onKeyPress, true); |
| 49 chrome.brailleDisplayPrivate.onKeyEvent.removeListener( |
| 50 cvox.KbExplorer.onBrailleKeyEvent); |
| 51 chrome.accessibilityPrivate.onAccessibilityGesture.removeListener( |
| 52 cvox.KbExplorer.onAccessibilityGesture); |
| 43 }; | 53 }; |
| 44 if (localStorage['useNext'] == 'true') { | 54 if (localStorage['useNext'] == 'true') { |
| 45 cvox.ChromeVoxKbHandler.handlerKeyMap = cvox.KeyMap.fromNext(); | 55 cvox.ChromeVoxKbHandler.handlerKeyMap = cvox.KeyMap.fromNext(); |
| 46 cvox.ChromeVox.modKeyStr = 'Search'; | 56 cvox.ChromeVox.modKeyStr = 'Search'; |
| 47 } else { | 57 } else { |
| 48 cvox.ChromeVoxKbHandler.handlerKeyMap = cvox.KeyMap.fromDefaults(); | 58 cvox.ChromeVoxKbHandler.handlerKeyMap = cvox.KeyMap.fromDefaults(); |
| 49 cvox.ChromeVox.modKeyStr = 'Search+Shift'; | 59 cvox.ChromeVox.modKeyStr = 'Search+Shift'; |
| 50 } | 60 } |
| 51 cvox.ChromeVoxKbHandler.commandHandler = cvox.KbExplorer.onCommand; | 61 cvox.ChromeVoxKbHandler.commandHandler = cvox.KbExplorer.onCommand; |
| 62 $('instruction').focus(); |
| 52 }; | 63 }; |
| 53 | 64 |
| 54 | 65 |
| 55 /** | 66 /** |
| 56 * Handles keydown events by speaking the human understandable name of the key. | 67 * Handles keydown events by speaking the human understandable name of the key. |
| 57 * @param {Event} evt key event. | 68 * @param {Event} evt key event. |
| 58 * @return {boolean} True if the default action should be performed. | 69 * @return {boolean} True if the default action should be performed. |
| 59 */ | 70 */ |
| 60 cvox.KbExplorer.onKeyDown = function(evt) { | 71 cvox.KbExplorer.onKeyDown = function(evt) { |
| 61 chrome.extension.getBackgroundPage()['speak']( | 72 chrome.extension.getBackgroundPage()['speak']( |
| 62 cvox.KeyUtil.getReadableNameForKeyCode(evt.keyCode), false, {}); | 73 cvox.KeyUtil.getReadableNameForKeyCode(evt.keyCode), false, {pitch: 0}); |
| 63 | 74 |
| 64 // Allow Ctrl+W to be handled. | 75 // Allow Ctrl+W to be handled. |
| 65 if (evt.keyCode == 87 && evt.ctrlKey) { | 76 if (evt.keyCode == 87 && evt.ctrlKey) { |
| 66 return true; | 77 return true; |
| 67 } | 78 } |
| 68 | 79 |
| 69 cvox.ChromeVoxKbHandler.basicKeyDownActionsListener(evt); | 80 cvox.ChromeVoxKbHandler.basicKeyDownActionsListener(evt); |
| 70 | 81 cvox.KbExplorer.clearRange(); |
| 71 evt.preventDefault(); | 82 evt.preventDefault(); |
| 72 evt.stopPropagation(); | 83 evt.stopPropagation(); |
| 73 return false; | 84 return false; |
| 74 }; | 85 }; |
| 75 | 86 |
| 76 | 87 |
| 77 /** | 88 /** |
| 78 * Handles keyup events. | 89 * Handles keyup events. |
| 79 * @param {Event} evt key event. | 90 * @param {Event} evt key event. |
| 80 */ | 91 */ |
| 81 cvox.KbExplorer.onKeyUp = function(evt) { | 92 cvox.KbExplorer.onKeyUp = function(evt) { |
| 93 cvox.KbExplorer.clearRange(); |
| 82 evt.preventDefault(); | 94 evt.preventDefault(); |
| 83 evt.stopPropagation(); | 95 evt.stopPropagation(); |
| 84 }; | 96 }; |
| 85 | 97 |
| 86 | 98 |
| 87 /** | 99 /** |
| 88 * Handles keypress events. | 100 * Handles keypress events. |
| 89 * @param {Event} evt key event. | 101 * @param {Event} evt key event. |
| 90 */ | 102 */ |
| 91 cvox.KbExplorer.onKeyPress = function(evt) { | 103 cvox.KbExplorer.onKeyPress = function(evt) { |
| 104 cvox.KbExplorer.clearRange(); |
| 92 evt.preventDefault(); | 105 evt.preventDefault(); |
| 93 evt.stopPropagation(); | 106 evt.stopPropagation(); |
| 94 }; | 107 }; |
| 95 | 108 |
| 96 /** | 109 /** |
| 110 * @param {cvox.BrailleKeyEvent} evt The key event. |
| 111 */ |
| 112 cvox.KbExplorer.onBrailleKeyEvent = function(evt) { |
| 113 var msgid; |
| 114 var msgArgs = []; |
| 115 var text; |
| 116 switch (evt.command) { |
| 117 case cvox.BrailleKeyCommand.PAN_LEFT: |
| 118 msgid = 'braille_pan_left'; |
| 119 break; |
| 120 case cvox.BrailleKeyCommand.PAN_RIGHT: |
| 121 msgid = 'braille_pan_right'; |
| 122 break; |
| 123 case cvox.BrailleKeyCommand.LINE_UP: |
| 124 msgid = 'braille_line_up'; |
| 125 break; |
| 126 case cvox.BrailleKeyCommand.LINE_DOWN: |
| 127 msgid = 'braille_line_down'; |
| 128 break; |
| 129 case cvox.BrailleKeyCommand.TOP: |
| 130 msgid = 'braille_top'; |
| 131 break; |
| 132 case cvox.BrailleKeyCommand.BOTTOM: |
| 133 msgid = 'braille_bottom'; |
| 134 break; |
| 135 break; |
| 136 case cvox.BrailleKeyCommand.ROUTING: |
| 137 case cvox.BrailleKeyCommand.SECONDARY_ROUTING: |
| 138 msgid = 'braille_routing'; |
| 139 msgArgs.push(/** @type {number} */ (evt.displayPosition + 1)); |
| 140 break; |
| 141 case cvox.BrailleKeyCommand.CHORD: |
| 142 if (!evt.brailleDots) |
| 143 return; |
| 144 var command = |
| 145 BrailleCommandHandler.getCommand(evt.brailleDots); |
| 146 if (command && cvox.KbExplorer.onCommand(command)) |
| 147 return; |
| 148 // Fall through. |
| 149 case cvox.BrailleKeyCommand.DOTS: |
| 150 if (!evt.brailleDots) |
| 151 return; |
| 152 text = BrailleCommandHandler.makeShortcutText(evt.brailleDots); |
| 153 break; |
| 154 case cvox.BrailleKeyCommand.STANDARD_KEY: |
| 155 break; |
| 156 } |
| 157 if (msgid) |
| 158 text = Msgs.getMsg(msgid, msgArgs); |
| 159 cvox.KbExplorer.output(text || evt.command); |
| 160 cvox.KbExplorer.clearRange(); |
| 161 }; |
| 162 |
| 163 /** |
| 164 * Handles accessibility gestures from the touch screen. |
| 165 * @param {string} gesture The gesture to handle, based on the AXGesture enum |
| 166 * defined in ui/accessibility/ax_enums.idl |
| 167 */ |
| 168 cvox.KbExplorer.onAccessibilityGesture = function(gesture) { |
| 169 // TODO(dmazzoni): implement. |
| 170 }; |
| 171 |
| 172 /** |
| 97 * Queues up command description. | 173 * Queues up command description. |
| 98 * @param {string} command | 174 * @param {string} command |
| 99 * @return {boolean|undefined} True if command existed and was handled. | 175 * @return {boolean|undefined} True if command existed and was handled. |
| 100 */ | 176 */ |
| 101 cvox.KbExplorer.onCommand = function(command) { | 177 cvox.KbExplorer.onCommand = function(command) { |
| 102 var msg = cvox.CommandStore.messageForCommand(command); | 178 var msg = cvox.CommandStore.messageForCommand(command); |
| 103 if (msg) { | 179 if (msg) { |
| 104 var commandText = Msgs.getMsg(msg); | 180 var commandText = Msgs.getMsg(msg); |
| 105 chrome.extension.getBackgroundPage()['speak'](commandText); | 181 cvox.KbExplorer.output(commandText); |
| 182 cvox.KbExplorer.clearRange(); |
| 106 return true; | 183 return true; |
| 107 } | 184 } |
| 108 }; | 185 }; |
| 186 |
| 187 /** |
| 188 * @param {string} text |
| 189 * @param {string=} opt_braille If different from text. |
| 190 */ |
| 191 cvox.KbExplorer.output = function(text, opt_braille) { |
| 192 chrome.extension.getBackgroundPage()['speak'](text); |
| 193 chrome.extension.getBackgroundPage().cvox.ChromeVox.braille.write( |
| 194 {text: new Spannable(opt_braille || text)}); |
| 195 }; |
| 196 |
| 197 /** Clears ChromeVox range. */ |
| 198 cvox.KbExplorer.clearRange = function() { |
| 199 chrome.extension.getBackgroundPage()[ |
| 200 'ChromeVoxState']['instance']['setCurrentRange'](null); |
| 201 }; |
| OLD | NEW |