OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 // TODO(elichtenberg): Move into custom logger class or somewhere else. | |
6 let debuggingEnabled = true; | |
7 | |
8 /** | 5 /** |
9 * Class to handle keyboard input. | 6 * Class to handle keyboard input. |
10 * | 7 * |
11 * @constructor | 8 * @constructor |
12 * @param {SwitchAccessInterface} switchAccess | 9 * @param {SwitchAccessInterface} switchAccess |
13 */ | 10 */ |
14 function KeyboardHandler(switchAccess) { | 11 function KeyboardHandler(switchAccess) { |
15 /** | 12 /** |
16 * SwitchAccess reference. | 13 * SwitchAccess reference. |
| 14 * |
17 * @private {SwitchAccessInterface} | 15 * @private {SwitchAccessInterface} |
18 */ | 16 */ |
19 this.switchAccess_ = switchAccess; | 17 this.switchAccess_ = switchAccess; |
20 | 18 |
21 this.init_(); | 19 this.init_(); |
22 } | 20 } |
23 | 21 |
24 KeyboardHandler.prototype = { | 22 KeyboardHandler.prototype = { |
25 /** | 23 /** |
26 * Set up key listener. | 24 * Set up key listener. |
27 * | 25 * |
28 * @private | 26 * @private |
29 */ | 27 */ |
30 init_: function() { | 28 init_: function() { |
31 // Capture keycodes for keys 1 through 4, and 6 through 9. | 29 this.updateSwitchAccessKeys(); |
32 let keyCodes = | |
33 ['1', '2', '3', '4', '6', '7', '8', '9'].map(key => key.charCodeAt(0)); | |
34 chrome.accessibilityPrivate.setSwitchAccessKeys(keyCodes); | |
35 document.addEventListener('keyup', this.handleKeyEvent_.bind(this)); | 30 document.addEventListener('keyup', this.handleKeyEvent_.bind(this)); |
36 }, | 31 }, |
37 | 32 |
38 /** | 33 /** |
39 * Handle a keyboard event by calling the appropriate SwitchAccess functions. | 34 * Update the keyboard keys captured by Switch Access to those stored in |
| 35 * prefs. |
| 36 */ |
| 37 updateSwitchAccessKeys: function() { |
| 38 let keyCodes = []; |
| 39 for (let command of this.switchAccess_.getCommands()) { |
| 40 let keyCode = this.keyCodeFor_(command); |
| 41 if ((keyCode >= '0'.charCodeAt(0) && keyCode <= '9'.charCodeAt(0)) || |
| 42 (keyCode >= 'A'.charCodeAt(0) && keyCode <= 'Z'.charCodeAt(0))) |
| 43 keyCodes.push(keyCode); |
| 44 } |
| 45 chrome.accessibilityPrivate.setSwitchAccessKeys(keyCodes); |
| 46 }, |
| 47 |
| 48 /** |
| 49 * Return the key code that |command| maps to. |
| 50 * |
| 51 * @param {string} command |
| 52 * @return {number} |
| 53 */ |
| 54 keyCodeFor_: function(command) { |
| 55 return this.switchAccess_.getNumberPref(command); |
| 56 }, |
| 57 |
| 58 /** |
| 59 * Run the command associated with the passed keyboard event. |
40 * | 60 * |
41 * @param {!Event} event | 61 * @param {!Event} event |
42 * @private | 62 * @private |
43 */ | 63 */ |
44 handleKeyEvent_: function(event) { | 64 handleKeyEvent_: function(event) { |
45 switch (event.key) { | 65 for (let command of this.switchAccess_.getCommands()) { |
46 case '1': | 66 if (this.keyCodeFor_(command) === event.keyCode) { |
47 console.log('1 = go to next element'); | 67 let key = event.key.toUpperCase(); |
48 this.switchAccess_.moveToNode(true); | 68 console.log('\'' + key + '\' pressed for command: ' + command); |
49 break; | 69 this.switchAccess_.runCommand(command); |
50 case '2': | 70 this.switchAccess_.performedUserAction(); |
51 console.log('2 = go to previous element'); | 71 return; |
52 this.switchAccess_.moveToNode(false); | |
53 break; | |
54 case '3': | |
55 console.log('3 = select element'); | |
56 this.switchAccess_.selectCurrentNode(); | |
57 break; | |
58 case '4': | |
59 this.switchAccess_.showOptionsPage(); | |
60 break; | |
61 } | |
62 if (debuggingEnabled) { | |
63 switch (event.key) { | |
64 case '6': | |
65 console.log('6 = go to next element (debug mode)'); | |
66 this.switchAccess_.debugMoveToNext(); | |
67 break; | |
68 case '7': | |
69 console.log('7 = go to previous element (debug mode)'); | |
70 this.switchAccess_.debugMoveToPrevious(); | |
71 break; | |
72 case '8': | |
73 console.log('8 = go to child element (debug mode)'); | |
74 this.switchAccess_.debugMoveToFirstChild(); | |
75 break; | |
76 case '9': | |
77 console.log('9 = go to parent element (debug mode)'); | |
78 this.switchAccess_.debugMoveToParent(); | |
79 break; | |
80 } | 72 } |
81 } | 73 } |
82 this.switchAccess_.performedUserAction(); | 74 }, |
83 } | |
84 }; | 75 }; |
OLD | NEW |