summaryrefslogtreecommitdiff
path: root/chromium/chrome/browser/resources/chromeos/chromevox/host/chrome/braille_key_types.js
blob: b2b800ca5665ef5ed071ab09c3ffd912ba8b083e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview Braille command definitions.
 * These types are adapted from Chrome's private braille API.
 * They can be found in the Chrome source repo at:
 * src/chrome/common/extensions/api/braille_display_private.idl
 * We define them here since they don't actually exist as bindings under
 * chrome.brailleDisplayPrivate.*.
 */

goog.provide('cvox.BrailleDisplayState');
goog.provide('cvox.BrailleKeyCommand');
goog.provide('cvox.BrailleKeyEvent');

goog.require('cvox.ChromeVox');


/**
 * The set of commands sent from a braille display.
 * @enum {string}
 */
cvox.BrailleKeyCommand = {
  PAN_LEFT: 'pan_left',
  PAN_RIGHT: 'pan_right',
  LINE_UP: 'line_up',
  LINE_DOWN: 'line_down',
  TOP: 'top',
  BOTTOM: 'bottom',
  ROUTING: 'routing',
  SECONDARY_ROUTING: 'secondary_routing',
  DOTS: 'dots',
  STANDARD_KEY: 'standard_key'
};


/**
 * Represents a key event from a braille display.
 *
 * @typedef {{command: cvox.BrailleKeyCommand,
 *            displayPosition: (undefined|number),
 *            brailleDots: (undefined|number),
 *            standardKeyCode: (undefined|string),
 *            standardKeyChar: (undefined|string),
 *            altKey: (undefined|boolean),
 *            ctrlKey: (undefined|boolean),
 *            shiftKey: (undefined|boolean)
 *          }}
 *  command The name of the command.
 *  displayPosition The 0-based position relative to the start of the currently
 *                  displayed text.  Used for commands that involve routing
 *                  keys or similar.  The position is given in characters,
 *                  not braille cells.
 *  brailleDots Dots that were pressed for braille input commands.  Bit mask
 *              where bit 0 represents dot 1 etc.
 * standardKeyCode DOM level 4 key code.
 * standardKeyChar DOM key event character.
 * altKey Whether the alt key was pressed.
 * ctrlKey Whether the control key was pressed.
 * shiftKey Whether the shift key was pressed.
 */
cvox.BrailleKeyEvent = {};


/**
 * Returns the numeric key code for a DOM level 4 key code string.
 * NOTE: Only the key codes produced by the brailleDisplayPrivate API are
 * supported.
 * @param {string} code DOM level 4 key code.
 * @return {number|undefined} The numeric key code, or {@code undefined}
 *     if unknown.
 */
cvox.BrailleKeyEvent.keyCodeToLegacyCode = function(code) {
  return cvox.BrailleKeyEvent.legacyKeyCodeMap_[code];
};


/**
 * Returns a char value appropriate for a synthezised key event for a given
 * key code.
 * @param {string} keyCode The DOM level 4 key code.
 * @return {number} Integral character code.
 */
cvox.BrailleKeyEvent.keyCodeToCharValue = function(keyCode) {
  /** @const */
  var SPECIAL_CODES = {
    'Backspace': 0x08,
    'Tab': 0x09,
    'Enter': 0x0A
  };
  // Note, the Chrome virtual keyboard falls back on the first character of the
  // key code if the key is not one of the above.  Do the same here.
  return SPECIAL_CODES[keyCode] || keyCode.charCodeAt(0);
};


/**
 * Map from DOM level 4 key codes to legacy numeric key codes.
 * @private {Object.<string, number>}
 */
cvox.BrailleKeyEvent.legacyKeyCodeMap_ = {
  'Backspace': 8,
  'Tab': 9,
  'Enter': 13,
  'Escape': 27,
  'Home': 36,
  'ArrowLeft': 37,
  'ArrowUp': 38,
  'ArrowRight': 39,
  'ArrowDown': 40,
  'PageUp': 33,
  'PageDown': 34,
  'End': 35,
  'Insert': 45,
  'Delete': 46
};

// Add the F1 to F12 keys.
(function() {
  for (var i = 0; i < 12; ++i) {
    cvox.BrailleKeyEvent.legacyKeyCodeMap_['F' + (i + 1)] = 112 + i;
  }
})();


/**
 * The state of a braille display as represented in the
 * chrome.brailleDisplayPrivate API.
 * @typedef {{available: boolean, textCellCount: (number|undefined)}}
 */
cvox.BrailleDisplayState;