react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / browser / ui / dom / getEventKey.js
81159 views/**1* Copyright 2013-2014, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under the BSD-style license found in the5* LICENSE file in the root directory of this source tree. An additional grant6* of patent rights can be found in the PATENTS file in the same directory.7*8* @providesModule getEventKey9* @typechecks static-only10*/1112"use strict";1314var getEventCharCode = require('getEventCharCode');1516/**17* Normalization of deprecated HTML5 `key` values18* @see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#Key_names19*/20var normalizeKey = {21'Esc': 'Escape',22'Spacebar': ' ',23'Left': 'ArrowLeft',24'Up': 'ArrowUp',25'Right': 'ArrowRight',26'Down': 'ArrowDown',27'Del': 'Delete',28'Win': 'OS',29'Menu': 'ContextMenu',30'Apps': 'ContextMenu',31'Scroll': 'ScrollLock',32'MozPrintableKey': 'Unidentified'33};3435/**36* Translation from legacy `keyCode` to HTML5 `key`37* Only special keys supported, all others depend on keyboard layout or browser38* @see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#Key_names39*/40var translateToKey = {418: 'Backspace',429: 'Tab',4312: 'Clear',4413: 'Enter',4516: 'Shift',4617: 'Control',4718: 'Alt',4819: 'Pause',4920: 'CapsLock',5027: 'Escape',5132: ' ',5233: 'PageUp',5334: 'PageDown',5435: 'End',5536: 'Home',5637: 'ArrowLeft',5738: 'ArrowUp',5839: 'ArrowRight',5940: 'ArrowDown',6045: 'Insert',6146: 'Delete',62112: 'F1', 113: 'F2', 114: 'F3', 115: 'F4', 116: 'F5', 117: 'F6',63118: 'F7', 119: 'F8', 120: 'F9', 121: 'F10', 122: 'F11', 123: 'F12',64144: 'NumLock',65145: 'ScrollLock',66224: 'Meta'67};6869/**70* @param {object} nativeEvent Native browser event.71* @return {string} Normalized `key` property.72*/73function getEventKey(nativeEvent) {74if (nativeEvent.key) {75// Normalize inconsistent values reported by browsers due to76// implementations of a working draft specification.7778// FireFox implements `key` but returns `MozPrintableKey` for all79// printable characters (normalized to `Unidentified`), ignore it.80var key = normalizeKey[nativeEvent.key] || nativeEvent.key;81if (key !== 'Unidentified') {82return key;83}84}8586// Browser does not implement `key`, polyfill as much of it as we can.87if (nativeEvent.type === 'keypress') {88var charCode = getEventCharCode(nativeEvent);8990// The enter-key is technically both printable and non-printable and can91// thus be captured by `keypress`, no other non-printable key should.92return charCode === 13 ? 'Enter' : String.fromCharCode(charCode);93}94if (nativeEvent.type === 'keydown' || nativeEvent.type === 'keyup') {95// While user keyboard layout determines the actual meaning of each96// `keyCode` value, almost all function keys have a universal value.97return translateToKey[nativeEvent.keyCode] || 'Unidentified';98}99return '';100}101102module.exports = getEventKey;103104105