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 3 // found in the LICENSE file. 4 5 /** 6 * @fileoverview Braille command definitions. 7 * These types are adapted from Chrome's private braille API. 8 * They can be found in the Chrome source repo at: 9 * src/chrome/common/extensions/api/braille_display_private.idl 10 * We define them here since they don't actually exist as bindings under 11 * chrome.brailleDisplayPrivate.*. 12 */ 13 14 goog.provide('cvox.BrailleDisplayState'); 15 goog.provide('cvox.BrailleKeyCommand'); 16 goog.provide('cvox.BrailleKeyEvent'); 17 18 goog.require('cvox.ChromeVox'); 19 20 21 /** 22 * The set of commands sent from a braille display. 23 * @enum {string} 24 */ 25 cvox.BrailleKeyCommand = { 26 PAN_LEFT: 'pan_left', 27 PAN_RIGHT: 'pan_right', 28 LINE_UP: 'line_up', 29 LINE_DOWN: 'line_down', 30 TOP: 'top', 31 BOTTOM: 'bottom', 32 ROUTING: 'routing', 33 SECONDARY_ROUTING: 'secondary_routing', 34 DOTS: 'dots', 35 STANDARD_KEY: 'standard_key' 36 }; 37 38 39 /** 40 * Represents a key event from a braille display. 41 * 42 * @typedef {{command: cvox.BrailleKeyCommand, 43 * displayPosition: (undefined|number), 44 * brailleDots: (undefined|number), 45 * standardKeyCode: (undefined|string), 46 * standardKeyChar: (undefined|string), 47 * altKey: (undefined|boolean), 48 * ctrlKey: (undefined|boolean), 49 * shiftKey: (undefined|boolean) 50 * }} 51 * command The name of the command. 52 * displayPosition The 0-based position relative to the start of the currently 53 * displayed text. Used for commands that involve routing 54 * keys or similar. The position is given in characters, 55 * not braille cells. 56 * brailleDots Dots that were pressed for braille input commands. Bit mask 57 * where bit 0 represents dot 1 etc. 58 * standardKeyCode DOM level 4 key code. 59 * standardKeyChar DOM key event character. 60 * altKey Whether the alt key was pressed. 61 * ctrlKey Whether the control key was pressed. 62 * shiftKey Whether the shift key was pressed. 63 */ 64 cvox.BrailleKeyEvent = {}; 65 66 67 /** 68 * Returns the numeric key code for a DOM level 4 key code string. 69 * NOTE: Only the key codes produced by the brailleDisplayPrivate API are 70 * supported. 71 * @param {string} code DOM level 4 key code. 72 * @return {number|undefined} The numeric key code, or {@code undefined} 73 * if unknown. 74 */ 75 cvox.BrailleKeyEvent.keyCodeToLegacyCode = function(code) { 76 return cvox.BrailleKeyEvent.legacyKeyCodeMap_[code]; 77 }; 78 79 80 /** 81 * Returns a char value appropriate for a synthezised key event for a given 82 * key code. 83 * @param {string} keyCode The DOM level 4 key code. 84 * @return {number} Integral character code. 85 */ 86 cvox.BrailleKeyEvent.keyCodeToCharValue = function(keyCode) { 87 /** @const */ 88 var SPECIAL_CODES = { 89 'Backspace': 0x08, 90 'Tab': 0x09, 91 'Enter': 0x0A 92 }; 93 // Note, the Chrome virtual keyboard falls back on the first character of the 94 // key code if the key is not one of the above. Do the same here. 95 return SPECIAL_CODES[keyCode] || keyCode.charCodeAt(0); 96 }; 97 98 99 /** 100 * Map from DOM level 4 key codes to legacy numeric key codes. 101 * @private {Object.<string, number>} 102 */ 103 cvox.BrailleKeyEvent.legacyKeyCodeMap_ = { 104 'Backspace': 8, 105 'Tab': 9, 106 'Enter': 13, 107 'Escape': 27, 108 'Home': 36, 109 'ArrowLeft': 37, 110 'ArrowUp': 38, 111 'ArrowRight': 39, 112 'ArrowDown': 40, 113 'PageUp': 33, 114 'PageDown': 34, 115 'End': 35, 116 'Insert': 45, 117 'Delete': 46 118 }; 119 120 // Add the F1 to F12 keys. 121 (function() { 122 for (var i = 0; i < 12; ++i) { 123 cvox.BrailleKeyEvent.legacyKeyCodeMap_['F' + (i + 1)] = 112 + i; 124 } 125 })(); 126 127 128 /** 129 * The state of a braille display as represented in the 130 * chrome.brailleDisplayPrivate API. 131 * @typedef {{available: boolean, textCellCount: (number|undefined)}} 132 */ 133 cvox.BrailleDisplayState; 134