{"size": 2767, "ext": "js", "max_stars_repo_path": "components/global/get-event-key.js", "max_stars_repo_name": "edklimov/ring-ui", "max_stars_repo_stars_event_min_datetime": "2017-08-02T14:16:01.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-31T09:24:10.000Z", "max_issues_repo_path": "components/global/get-event-key.js", "max_issues_repo_name": "edklimov/ring-ui", "max_issues_repo_issues_event_min_datetime": "2017-08-05T01:26:35.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-31T10:45:01.000Z", "max_forks_repo_path": "components/global/get-event-key.js", "max_forks_repo_name": "edklimov/ring-ui", "max_forks_repo_forks_event_min_datetime": "2017-08-03T12:55:12.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-31T09:47:44.000Z"}
{"max_stars_count": 3080, "max_issues_count": 1209, "max_forks_count": 202, "avg_line_length": 24.4867256637, "max_line_length": 80, "alphanum_fraction": 0.6331767257}
// react-dom getEventKey function extracted

const normalizeKey = {
  Esc: 'Escape',
  Spacebar: ' ',
  Left: 'ArrowLeft',
  Up: 'ArrowUp',
  Right: 'ArrowRight',
  Down: 'ArrowDown',
  Del: 'Delete',
  Win: 'OS',
  Menu: 'ContextMenu',
  Apps: 'ContextMenu',
  Scroll: 'ScrollLock',
  MozPrintableKey: 'Unidentified'
};

const translateToKey = {
  8: 'Backspace',
  9: 'Tab',
  12: 'Clear',
  13: 'Enter',
  16: 'Shift',
  17: 'Control',
  18: 'Alt',
  19: 'Pause',
  20: 'CapsLock',
  27: 'Escape',
  32: ' ',
  33: 'PageUp',
  34: 'PageDown',
  35: 'End',
  36: 'Home',
  37: 'ArrowLeft',
  38: 'ArrowUp',
  39: 'ArrowRight',
  40: 'ArrowDown',
  45: 'Insert',
  46: 'Delete',
  112: 'F1',
  113: 'F2',
  114: 'F3',
  115: 'F4',
  116: 'F5',
  117: 'F6',
  118: 'F7',
  119: 'F8',
  120: 'F9',
  121: 'F10',
  122: 'F11',
  123: 'F12',
  144: 'NumLock',
  145: 'ScrollLock',
  224: 'Meta'
};

const ENTER = 13;
const SPACE = 32;

function getEventCharCode(nativeEvent) {
  let charCode;
  const keyCode = nativeEvent.keyCode;

  if ('charCode' in nativeEvent) {
    charCode = nativeEvent.charCode;

    // FF does not set `charCode` for the Enter-key, check against `keyCode`.
    if (charCode === 0 && keyCode === ENTER) {
      charCode = ENTER;
    }
  } else {
    // IE8 does not implement `charCode`, but `keyCode` has the correct value.
    charCode = keyCode;
  }

  // Some non-printable keys are reported in `charCode`/`keyCode`, discard them.
  // Must not discard the (non-)printable Enter-key.
  if (charCode >= SPACE || charCode === ENTER) {
    return charCode;
  }
  return 0;
}


export default function getEventKey(nativeEvent) {
  if (nativeEvent.key) {
    // Normalize inconsistent values reported by browsers due to
    // implementations of a working draft specification.

    // FireFox implements `key` but returns `MozPrintableKey` for all
    // printable characters (normalized to `Unidentified`), ignore it.
    const key = normalizeKey[nativeEvent.key] || nativeEvent.key;
    if (key !== 'Unidentified') {
      return key;
    }
  }

  // Browser does not implement `key`, polyfill as much of it as we can.
  if (nativeEvent.type === 'keypress') {
    const charCode = getEventCharCode(nativeEvent);

    // The enter-key is technically both printable and non-printable and can
    // thus be captured by `keypress`, no other non-printable key should.
    return charCode === SPACE ? 'Enter' : String.fromCharCode(charCode);
  }
  if (nativeEvent.type === 'keydown' || nativeEvent.type === 'keyup') {
    // While user keyboard layout determines the actual meaning of each
    // `keyCode` value, almost all function keys have a universal value.
    return translateToKey[nativeEvent.keyCode] || 'Unidentified';
  }
  return '';
}
