tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

KeyData.sys.mjs (10213B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 const KEY_DATA = {
      6  " ": { code: "Space" },
      7  "!": { code: "Digit1", shifted: true },
      8  "#": { code: "Digit3", shifted: true },
      9  $: { code: "Digit4", shifted: true },
     10  "%": { code: "Digit5", shifted: true },
     11  "&": { code: "Digit7", shifted: true },
     12  "'": { code: "Quote" },
     13  "(": { code: "Digit9", shifted: true },
     14  ")": { code: "Digit0", shifted: true },
     15  "*": { code: "Digit8", shifted: true },
     16  "+": { code: "Equal", shifted: true },
     17  ",": { code: "Comma" },
     18  "-": { code: "Minus" },
     19  ".": { code: "Period" },
     20  "/": { code: "Slash" },
     21  0: { code: "Digit0" },
     22  1: { code: "Digit1" },
     23  2: { code: "Digit2" },
     24  3: { code: "Digit3" },
     25  4: { code: "Digit4" },
     26  5: { code: "Digit5" },
     27  6: { code: "Digit6" },
     28  7: { code: "Digit7" },
     29  8: { code: "Digit8" },
     30  9: { code: "Digit9" },
     31  ":": { code: "Semicolon", shifted: true },
     32  ";": { code: "Semicolon" },
     33  "<": { code: "Comma", shifted: true },
     34  "=": { code: "Equal" },
     35  ">": { code: "Period", shifted: true },
     36  "?": { code: "Slash", shifted: true },
     37  "@": { code: "Digit2", shifted: true },
     38  A: { code: "KeyA", shifted: true },
     39  B: { code: "KeyB", shifted: true },
     40  C: { code: "KeyC", shifted: true },
     41  D: { code: "KeyD", shifted: true },
     42  E: { code: "KeyE", shifted: true },
     43  F: { code: "KeyF", shifted: true },
     44  G: { code: "KeyG", shifted: true },
     45  H: { code: "KeyH", shifted: true },
     46  I: { code: "KeyI", shifted: true },
     47  J: { code: "KeyJ", shifted: true },
     48  K: { code: "KeyK", shifted: true },
     49  L: { code: "KeyL", shifted: true },
     50  M: { code: "KeyM", shifted: true },
     51  N: { code: "KeyN", shifted: true },
     52  O: { code: "KeyO", shifted: true },
     53  P: { code: "KeyP", shifted: true },
     54  Q: { code: "KeyQ", shifted: true },
     55  R: { code: "KeyR", shifted: true },
     56  S: { code: "KeyS", shifted: true },
     57  T: { code: "KeyT", shifted: true },
     58  U: { code: "KeyU", shifted: true },
     59  V: { code: "KeyV", shifted: true },
     60  W: { code: "KeyW", shifted: true },
     61  X: { code: "KeyX", shifted: true },
     62  Y: { code: "KeyY", shifted: true },
     63  Z: { code: "KeyZ", shifted: true },
     64  "[": { code: "BracketLeft" },
     65  '"': { code: "Quote", shifted: true },
     66  "\\": { code: "Backslash" },
     67  "]": { code: "BracketRight" },
     68  "^": { code: "Digit6", shifted: true },
     69  _: { code: "Minus", shifted: true },
     70  "`": { code: "Backquote" },
     71  a: { code: "KeyA" },
     72  b: { code: "KeyB" },
     73  c: { code: "KeyC" },
     74  d: { code: "KeyD" },
     75  e: { code: "KeyE" },
     76  f: { code: "KeyF" },
     77  g: { code: "KeyG" },
     78  h: { code: "KeyH" },
     79  i: { code: "KeyI" },
     80  j: { code: "KeyJ" },
     81  k: { code: "KeyK" },
     82  l: { code: "KeyL" },
     83  m: { code: "KeyM" },
     84  n: { code: "KeyN" },
     85  o: { code: "KeyO" },
     86  p: { code: "KeyP" },
     87  q: { code: "KeyQ" },
     88  r: { code: "KeyR" },
     89  s: { code: "KeyS" },
     90  t: { code: "KeyT" },
     91  u: { code: "KeyU" },
     92  v: { code: "KeyV" },
     93  w: { code: "KeyW" },
     94  x: { code: "KeyX" },
     95  y: { code: "KeyY" },
     96  z: { code: "KeyZ" },
     97  "{": { code: "BracketLeft", shifted: true },
     98  "|": { code: "Backslash", shifted: true },
     99  "}": { code: "BracketRight", shifted: true },
    100  "~": { code: "Backquote", shifted: true },
    101  "\uE000": { key: "Unidentified", printable: false },
    102  "\uE001": { key: "Cancel", printable: false },
    103  "\uE002": { code: "Help", key: "Help", printable: false },
    104  "\uE003": { code: "Backspace", key: "Backspace", printable: false },
    105  "\uE004": { code: "Tab", key: "Tab", printable: false },
    106  "\uE005": { code: "", key: "Clear", printable: false },
    107  "\uE006": { code: "Enter", key: "Enter", printable: false },
    108  "\uE007": {
    109    code: "NumpadEnter",
    110    key: "Enter",
    111    location: 1,
    112    printable: false,
    113  },
    114  "\uE008": {
    115    code: "ShiftLeft",
    116    key: "Shift",
    117    location: 1,
    118    modifier: "shiftKey",
    119    printable: false,
    120  },
    121  "\uE009": {
    122    code: "ControlLeft",
    123    key: "Control",
    124    location: 1,
    125    modifier: "ctrlKey",
    126    printable: false,
    127  },
    128  "\uE00A": {
    129    code: "AltLeft",
    130    key: "Alt",
    131    location: 1,
    132    modifier: "altKey",
    133    printable: false,
    134  },
    135  "\uE00B": { code: "Pause", key: "Pause", printable: false },
    136  "\uE00C": { code: "Escape", key: "Escape", printable: false },
    137  "\uE00D": { code: "Space", key: " ", shifted: true },
    138  "\uE00E": { code: "PageUp", key: "PageUp", printable: false },
    139  "\uE00F": { code: "PageDown", key: "PageDown", printable: false },
    140  "\uE010": { code: "End", key: "End", printable: false },
    141  "\uE011": { code: "Home", key: "Home", printable: false },
    142  "\uE012": { code: "ArrowLeft", key: "ArrowLeft", printable: false },
    143  "\uE013": { code: "ArrowUp", key: "ArrowUp", printable: false },
    144  "\uE014": { code: "ArrowRight", key: "ArrowRight", printable: false },
    145  "\uE015": { code: "ArrowDown", key: "ArrowDown", printable: false },
    146  "\uE016": { code: "Insert", key: "Insert", printable: false },
    147  "\uE017": { code: "Delete", key: "Delete", printable: false },
    148  "\uE018": { code: "", key: ";" },
    149  "\uE019": { code: "NumpadEqual", key: "=", location: 3 },
    150  "\uE01A": { code: "Numpad0", key: "0", location: 3 },
    151  "\uE01B": { code: "Numpad1", key: "1", location: 3 },
    152  "\uE01C": { code: "Numpad2", key: "2", location: 3 },
    153  "\uE01D": { code: "Numpad3", key: "3", location: 3 },
    154  "\uE01E": { code: "Numpad4", key: "4", location: 3 },
    155  "\uE01F": { code: "Numpad5", key: "5", location: 3 },
    156  "\uE020": { code: "Numpad6", key: "6", location: 3 },
    157  "\uE021": { code: "Numpad7", key: "7", location: 3 },
    158  "\uE022": { code: "Numpad8", key: "8", location: 3 },
    159  "\uE023": { code: "Numpad9", key: "9", location: 3 },
    160  "\uE024": { code: "NumpadMultiply", key: "*", location: 3 },
    161  "\uE025": { code: "NumpadAdd", key: "+", location: 3 },
    162  "\uE026": { code: "NumpadComma", key: ",", location: 3 },
    163  "\uE027": { code: "NumpadSubtract", key: "-", location: 3 },
    164  "\uE028": { code: "NumpadDecimal", key: ".", location: 3 },
    165  "\uE029": { code: "NumpadDivide", key: "/", location: 3 },
    166  "\uE031": { code: "F1", key: "F1", printable: false },
    167  "\uE032": { code: "F2", key: "F2", printable: false },
    168  "\uE033": { code: "F3", key: "F3", printable: false },
    169  "\uE034": { code: "F4", key: "F4", printable: false },
    170  "\uE035": { code: "F5", key: "F5", printable: false },
    171  "\uE036": { code: "F6", key: "F6", printable: false },
    172  "\uE037": { code: "F7", key: "F7", printable: false },
    173  "\uE038": { code: "F8", key: "F8", printable: false },
    174  "\uE039": { code: "F9", key: "F9", printable: false },
    175  "\uE03A": { code: "F10", key: "F10", printable: false },
    176  "\uE03B": { code: "F11", key: "F11", printable: false },
    177  "\uE03C": { code: "F12", key: "F12", printable: false },
    178  "\uE03D": {
    179    code: "MetaLeft",
    180    key: "Meta",
    181    location: 1,
    182    modifier: "metaKey",
    183    printable: false,
    184  },
    185  "\uE040": { code: "", key: "ZenkakuHankaku", printable: false },
    186  "\uE050": {
    187    code: "ShiftRight",
    188    key: "Shift",
    189    location: 2,
    190    modifier: "shiftKey",
    191    printable: false,
    192  },
    193  "\uE051": {
    194    code: "ControlRight",
    195    key: "Control",
    196    location: 2,
    197    modifier: "ctrlKey",
    198    printable: false,
    199  },
    200  "\uE052": {
    201    code: "AltRight",
    202    key: "Alt",
    203    location: 2,
    204    modifier: "altKey",
    205    printable: false,
    206  },
    207  "\uE053": {
    208    code: "MetaRight",
    209    key: "Meta",
    210    location: 2,
    211    modifier: "metaKey",
    212    printable: false,
    213  },
    214  "\uE054": {
    215    code: "Numpad9",
    216    key: "PageUp",
    217    location: 3,
    218    printable: false,
    219    shifted: true,
    220  },
    221  "\uE055": {
    222    code: "Numpad3",
    223    key: "PageDown",
    224    location: 3,
    225    printable: false,
    226    shifted: true,
    227  },
    228  "\uE056": {
    229    code: "Numpad1",
    230    key: "End",
    231    location: 3,
    232    printable: false,
    233    shifted: true,
    234  },
    235  "\uE057": {
    236    code: "Numpad7",
    237    key: "Home",
    238    location: 3,
    239    printable: false,
    240    shifted: true,
    241  },
    242  "\uE058": {
    243    code: "Numpad4",
    244    key: "ArrowLeft",
    245    location: 3,
    246    printable: false,
    247    shifted: true,
    248  },
    249  "\uE059": {
    250    code: "Numpad8",
    251    key: "ArrowUp",
    252    location: 3,
    253    printable: false,
    254    shifted: true,
    255  },
    256  "\uE05A": {
    257    code: "Numpad6",
    258    key: "ArrowRight",
    259    location: 3,
    260    printable: false,
    261    shifted: true,
    262  },
    263  "\uE05B": {
    264    code: "Numpad2",
    265    key: "ArrowDown",
    266    location: 3,
    267    printable: false,
    268    shifted: true,
    269  },
    270  "\uE05C": {
    271    code: "Numpad0",
    272    key: "Insert",
    273    location: 3,
    274    printable: false,
    275    shifted: true,
    276  },
    277  "\uE05D": {
    278    code: "NumpadDecimal",
    279    key: "Delete",
    280    location: 3,
    281    printable: false,
    282    shifted: true,
    283  },
    284 };
    285 
    286 const lazy = {};
    287 
    288 ChromeUtils.defineLazyGetter(lazy, "SHIFT_DATA", () => {
    289  // Initialize the shift mapping
    290  const shiftData = new Map();
    291  const byCode = new Map();
    292  for (let [key, props] of Object.entries(KEY_DATA)) {
    293    if (props.code) {
    294      if (!byCode.has(props.code)) {
    295        byCode.set(props.code, [null, null]);
    296      }
    297      byCode.get(props.code)[props.shifted ? 1 : 0] = key;
    298    }
    299  }
    300  for (let [unshifted, shifted] of byCode.values()) {
    301    if (unshifted !== null && shifted !== null) {
    302      shiftData.set(unshifted, shifted);
    303    }
    304  }
    305  return shiftData;
    306 });
    307 
    308 export const keyData = {
    309  /**
    310   * Get key event data for a given key character.
    311   *
    312   * @param {string} rawKey
    313   *     Key for which to get data. This can either be the key codepoint
    314   *     itself or one of the codepoints in the range U+E000-U+E05D that
    315   *     WebDriver uses to represent keys not corresponding directly to
    316   *     a codepoint.
    317   * @returns {object} Key event data object.
    318   */
    319  getData(rawKey) {
    320    let keyData = { key: rawKey, location: 0, printable: true, shifted: false };
    321    if (KEY_DATA.hasOwnProperty(rawKey)) {
    322      keyData = { ...keyData, ...KEY_DATA[rawKey] };
    323    }
    324    return keyData;
    325  },
    326 
    327  /**
    328   * Get shifted key character for a given key character.
    329   *
    330   * For characters unaffected by the shift key, this returns the input.
    331   *
    332   * @param {string} rawKey Key for which to get shifted key.
    333   * @returns {string} Key string to use when the shift modifier is set.
    334   */
    335  getShiftedKey(rawKey) {
    336    return lazy.SHIFT_DATA.get(rawKey) ?? rawKey;
    337  },
    338 };