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. | 5 // TODO(elichtenberg): Move into custom logger class or somewhere else. |
6 let debuggingEnabled = true; | 6 let debuggingEnabled = true; |
7 | 7 |
8 /** | 8 /** |
9 * Class to handle keyboard input. | 9 * Class to handle keyboard input. |
10 * | 10 * |
11 * @constructor | 11 * @constructor |
12 * @param {SwitchAccessInterface} switchAccess | 12 * @param {SwitchAccessInterface} switchAccess |
13 */ | 13 */ |
14 function KeyboardHandler(switchAccess) { | 14 function KeyboardHandler(switchAccess) { |
15 /** | 15 /** |
16 * SwitchAccess reference. | 16 * SwitchAccess reference. |
17 * @private {SwitchAccessInterface} | 17 * @private {SwitchAccessInterface} |
18 */ | 18 */ |
19 this.switchAccess_ = switchAccess; | 19 this.switchAccess_ = switchAccess; |
20 | 20 |
21 /** | |
22 * User preferences. | |
23 * | |
24 * @private {SwitchAccessPrefs} | |
25 */ | |
26 this.prefs_ = switchAccess.switchAccessPrefs; | |
27 | |
21 this.init_(); | 28 this.init_(); |
22 } | 29 } |
23 | 30 |
24 KeyboardHandler.prototype = { | 31 KeyboardHandler.prototype = { |
25 /** | 32 /** |
26 * Set up key listener. | 33 * Set up key listener. |
27 * | 34 * |
28 * @private | 35 * @private |
29 */ | 36 */ |
30 init_: function() { | 37 init_: function() { |
31 // Capture keycodes for keys 1 through 4, and 6 through 9. | 38 this.updateSwitchAccessKeys(); |
32 let keyCodes = ['1', '2', '3', '4', '6', '7', '8', '9'].map( | 39 document.addEventListener('keyup', this.handleKeyEvent_.bind(this)); |
33 key => key.charCodeAt(0)); | 40 }, |
41 | |
42 /** | |
43 * Update the keyboard keys captured by Switch Access to those stored in | |
44 * prefs. | |
45 */ | |
46 updateSwitchAccessKeys: function() { | |
47 let keyCodes = []; | |
48 for (let command of this.prefs_.getCommands()) | |
49 keyCodes.push(this.keyCodeFor_(command)); | |
34 chrome.accessibilityPrivate.setSwitchAccessKeys(keyCodes); | 50 chrome.accessibilityPrivate.setSwitchAccessKeys(keyCodes); |
35 document.addEventListener('keyup', this.handleKeyEvent_.bind(this)); | 51 }, |
52 | |
53 /** | |
54 * Return the key code that |command| maps to. | |
55 * | |
56 * @param {string} command | |
57 * @return {number} | |
58 */ | |
59 keyCodeFor_: function(command) { | |
60 return this.prefs_.getNumberPref(command); | |
36 }, | 61 }, |
37 | 62 |
38 /** | 63 /** |
39 * Handle a keyboard event by calling the appropriate SwitchAccess functions. | 64 * Handle a keyboard event by calling the appropriate SwitchAccess functions. |
40 * | 65 * |
41 * @param {!Event} event | 66 * @param {!Event} event |
42 * @private | 67 * @private |
43 */ | 68 */ |
44 handleKeyEvent_: function(event) { | 69 handleKeyEvent_: function(event) { |
45 switch (event.key) { | 70 let key = event.key.toUpperCase(); |
46 case '1': | 71 switch (event.keyCode) { |
dmazzoni
2017/06/15 22:37:39
One possibility here would be a map from command n
elichtenberg
2017/06/20 01:02:30
Done. Put command map in commands.js
| |
47 console.log('1 = go to next element'); | 72 case this.keyCodeFor_('next'): |
73 console.log(key + ' = go to next element'); | |
48 this.switchAccess_.moveToNode(true); | 74 this.switchAccess_.moveToNode(true); |
49 break; | 75 break; |
50 case '2': | 76 case this.keyCodeFor_('previous'): |
51 console.log('2 = go to previous element'); | 77 console.log(key + ' = go to previous element'); |
52 this.switchAccess_.moveToNode(false); | 78 this.switchAccess_.moveToNode(false); |
53 break; | 79 break; |
54 case '3': | 80 case this.keyCodeFor_('select'): |
55 console.log('3 = select element'); | 81 console.log(key + ' = select element'); |
56 this.switchAccess_.selectCurrentNode(); | 82 this.switchAccess_.selectCurrentNode(); |
57 break; | 83 break; |
58 case '4': | 84 case this.keyCodeFor_('options'): |
59 this.switchAccess_.showOptionsPage(); | 85 this.switchAccess_.showOptionsPage(); |
60 break; | 86 break; |
61 } | 87 } |
62 if (debuggingEnabled) { | 88 if (debuggingEnabled) { |
63 switch (event.key) { | 89 switch (event.keyCode) { |
64 case '6': | 90 case this.keyCodeFor_('debugNext'): |
65 console.log('6 = go to next element (debug mode)'); | 91 console.log(key + ' = go to next element (debug mode)'); |
66 this.switchAccess_.debugMoveToNext(); | 92 this.switchAccess_.debugMoveToNext(); |
67 break; | 93 break; |
68 case '7': | 94 case this.keyCodeFor_('debugPrevious'): |
69 console.log('7 = go to previous element (debug mode)'); | 95 console.log(key + ' = go to previous element (debug mode)'); |
70 this.switchAccess_.debugMoveToPrevious(); | 96 this.switchAccess_.debugMoveToPrevious(); |
71 break; | 97 break; |
72 case '8': | 98 case this.keyCodeFor_('debugChild'): |
73 console.log('8 = go to child element (debug mode)'); | 99 console.log(key + ' = go to child element (debug mode)'); |
74 this.switchAccess_.debugMoveToFirstChild(); | 100 this.switchAccess_.debugMoveToFirstChild(); |
75 break; | 101 break; |
76 case '9': | 102 case this.keyCodeFor_('debugParent'): |
77 console.log('9 = go to parent element (debug mode)'); | 103 console.log(key + ' = go to parent element (debug mode)'); |
78 this.switchAccess_.debugMoveToParent(); | 104 this.switchAccess_.debugMoveToParent(); |
79 break; | 105 break; |
80 } | 106 } |
81 } | 107 } |
82 this.switchAccess_.performedUserAction(); | 108 this.switchAccess_.performedUserAction(); |
83 } | 109 } |
84 }; | 110 }; |
OLD | NEW |