You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.5 KiB
51 lines
1.5 KiB
10 years ago
|
/**
|
||
|
* Copyright 2013-2015, Facebook, Inc.
|
||
|
* All rights reserved.
|
||
|
*
|
||
|
* This source code is licensed under the BSD-style license found in the
|
||
|
* LICENSE file in the root directory of this source tree. An additional grant
|
||
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||
|
*
|
||
|
* @providesModule getEventCharCode
|
||
|
* @typechecks static-only
|
||
|
*/
|
||
|
|
||
|
'use strict';
|
||
|
|
||
|
/**
|
||
|
* `charCode` represents the actual "character code" and is safe to use with
|
||
|
* `String.fromCharCode`. As such, only keys that correspond to printable
|
||
|
* characters produce a valid `charCode`, the only exception to this is Enter.
|
||
|
* The Tab-key is considered non-printable and does not have a `charCode`,
|
||
|
* presumably because it does not produce a tab-character in browsers.
|
||
|
*
|
||
|
* @param {object} nativeEvent Native browser event.
|
||
|
* @return {string} Normalized `charCode` property.
|
||
|
*/
|
||
|
function getEventCharCode(nativeEvent) {
|
||
|
var charCode;
|
||
|
var 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 === 13) {
|
||
|
charCode = 13;
|
||
|
}
|
||
|
} 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 >= 32 || charCode === 13) {
|
||
|
return charCode;
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
module.exports = getEventCharCode;
|