react / react-0.13.3 / examples / basic-commonjs / node_modules / reactify / node_modules / react-tools / src / browser / ui / dom / getEventCharCode.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 getEventCharCode9* @typechecks static-only10*/1112"use strict";1314/**15* `charCode` represents the actual "character code" and is safe to use with16* `String.fromCharCode`. As such, only keys that correspond to printable17* characters produce a valid `charCode`, the only exception to this is Enter.18* The Tab-key is considered non-printable and does not have a `charCode`,19* presumably because it does not produce a tab-character in browsers.20*21* @param {object} nativeEvent Native browser event.22* @return {string} Normalized `charCode` property.23*/24function getEventCharCode(nativeEvent) {25var charCode;26var keyCode = nativeEvent.keyCode;2728if ('charCode' in nativeEvent) {29charCode = nativeEvent.charCode;3031// FF does not set `charCode` for the Enter-key, check against `keyCode`.32if (charCode === 0 && keyCode === 13) {33charCode = 13;34}35} else {36// IE8 does not implement `charCode`, but `keyCode` has the correct value.37charCode = keyCode;38}3940// Some non-printable keys are reported in `charCode`/`keyCode`, discard them.41// Must not discard the (non-)printable Enter-key.42if (charCode >= 32 || charCode === 13) {43return charCode;44}4546return 0;47}4849module.exports = getEventCharCode;505152