Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81159 views
1
/**
2
* Copyright 2013-2014, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule getEventCharCode
10
* @typechecks static-only
11
*/
12
13
"use strict";
14
15
/**
16
* `charCode` represents the actual "character code" and is safe to use with
17
* `String.fromCharCode`. As such, only keys that correspond to printable
18
* characters produce a valid `charCode`, the only exception to this is Enter.
19
* The Tab-key is considered non-printable and does not have a `charCode`,
20
* presumably because it does not produce a tab-character in browsers.
21
*
22
* @param {object} nativeEvent Native browser event.
23
* @return {string} Normalized `charCode` property.
24
*/
25
function getEventCharCode(nativeEvent) {
26
var charCode;
27
var keyCode = nativeEvent.keyCode;
28
29
if ('charCode' in nativeEvent) {
30
charCode = nativeEvent.charCode;
31
32
// FF does not set `charCode` for the Enter-key, check against `keyCode`.
33
if (charCode === 0 && keyCode === 13) {
34
charCode = 13;
35
}
36
} else {
37
// IE8 does not implement `charCode`, but `keyCode` has the correct value.
38
charCode = keyCode;
39
}
40
41
// Some non-printable keys are reported in `charCode`/`keyCode`, discard them.
42
// Must not discard the (non-)printable Enter-key.
43
if (charCode >= 32 || charCode === 13) {
44
return charCode;
45
}
46
47
return 0;
48
}
49
50
module.exports = getEventCharCode;
51
52