OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 goog.provide('cvox.ChromeVoxKbHandler'); |
| 6 |
| 7 goog.require('cvox.ChromeVox'); |
| 8 goog.require('cvox.ChromeVoxJSON'); |
| 9 goog.require('cvox.ChromeVoxSearch'); |
| 10 goog.require('cvox.ChromeVoxUserCommands'); |
| 11 goog.require('cvox.KeyUtil'); |
| 12 |
| 13 /** |
| 14 * @fileoverview Handles user keyboard input events. |
| 15 */ |
| 16 cvox.ChromeVoxKbHandler = {}; |
| 17 |
| 18 /** |
| 19 * Maps a key string in the form generated by KeyUtil.keyEventToString |
| 20 * (like "Ctrl+Alt+X") into the name of a command to execute. |
| 21 * |
| 22 * @type {Object} |
| 23 */ |
| 24 cvox.ChromeVoxKbHandler.keyToFunctionsTable = {}; |
| 25 |
| 26 /** |
| 27 * Loads the key bindings into the keyToFunctionsTable. |
| 28 * |
| 29 * @param {Object} keyToFunctionsTable The key bindings table. |
| 30 */ |
| 31 cvox.ChromeVoxKbHandler.loadKeyToFunctionsTable = function( |
| 32 keyToFunctionsTable) { |
| 33 console.log('Got keyToFunctionsTable: ' + |
| 34 JSON.stringify(keyToFunctionsTable)); |
| 35 cvox.ChromeVoxKbHandler.keyToFunctionsTable = keyToFunctionsTable; |
| 36 }; |
| 37 |
| 38 /** |
| 39 * Checks if ChromeVox must pass the enter key to the browser. |
| 40 * For example, if the user has focus on an input field, link, button, |
| 41 * etc., then that takes precedence over anything else ChromeVox |
| 42 * might be doing and so it must pass the enter key to the browser. |
| 43 * |
| 44 * @return {boolean} True if an Enter key event must be passed to the browser. |
| 45 */ |
| 46 cvox.ChromeVoxKbHandler.mustPassEnterKey = function() { |
| 47 return (document.activeElement && |
| 48 ((document.activeElement.tagName == 'INPUT') || |
| 49 (document.activeElement.tagName == 'A') || |
| 50 (document.activeElement.tagName == 'SELECT') || |
| 51 (document.activeElement.tagName == 'BUTTON') || |
| 52 (document.activeElement.tagName == 'TEXTAREA'))); |
| 53 }; |
| 54 |
| 55 /** |
| 56 * Handles key down events. |
| 57 * |
| 58 * @param {Object} evt The key down event to process. |
| 59 * @return {boolean} True if the default action should be performed. |
| 60 */ |
| 61 cvox.ChromeVoxKbHandler.basicKeyDownActionsListener = function(evt) { |
| 62 // The enter key can be handled either by ChromeVox or by the browser. |
| 63 if (evt.keyCode == 13) { |
| 64 // If the user is focused on something that explicitly takes the |
| 65 // enter key, that has precedence. Always let the key through. |
| 66 if (cvox.ChromeVoxKbHandler.mustPassEnterKey()) { |
| 67 return true; |
| 68 } |
| 69 // The only time ChromeVox should consider handling the enter |
| 70 // key is if the navigation manager is able to act on the current item. |
| 71 if (!cvox.ChromeVox.navigationManager.canActOnCurrentItem()) { |
| 72 return true; |
| 73 } |
| 74 } |
| 75 |
| 76 var keyStr = cvox.KeyUtil.keyEventToString(evt); |
| 77 var functionName = cvox.ChromeVoxKbHandler.keyToFunctionsTable[keyStr]; |
| 78 var func = cvox.ChromeVoxUserCommands.commands[functionName]; |
| 79 |
| 80 if (func) { |
| 81 if (cvox.ChromeVoxSearch.isActive()) { |
| 82 cvox.ChromeVoxSearch.hide(); |
| 83 cvox.ChromeVox.navigationManager.syncToSelection(); |
| 84 } |
| 85 return func(); |
| 86 } |
| 87 |
| 88 return true; |
| 89 }; |
OLD | NEW |