1 | /**
|
2 | * Copyright (c) 2013-present, Facebook, Inc.
|
3 | *
|
4 | * This source code is licensed under the MIT license found in the
|
5 | * LICENSE file in the root directory of this source tree.
|
6 | *
|
7 | */
|
8 |
|
9 | ;
|
10 |
|
11 | /**
|
12 | * `charCode` represents the actual "character code" and is safe to use with
|
13 | * `String.fromCharCode`. As such, only keys that correspond to printable
|
14 | * characters produce a valid `charCode`, the only exception to this is Enter.
|
15 | * The Tab-key is considered non-printable and does not have a `charCode`,
|
16 | * presumably because it does not produce a tab-character in browsers.
|
17 | *
|
18 | * @param {object} nativeEvent Native browser event.
|
19 | * @return {number} Normalized `charCode` property.
|
20 | */
|
21 |
|
22 | function getEventCharCode(nativeEvent) {
|
23 | var charCode;
|
24 | var keyCode = nativeEvent.keyCode;
|
25 |
|
26 | if ('charCode' in nativeEvent) {
|
27 | charCode = nativeEvent.charCode;
|
28 |
|
29 | // FF does not set `charCode` for the Enter-key, check against `keyCode`.
|
30 | if (charCode === 0 && keyCode === 13) {
|
31 | charCode = 13;
|
32 | }
|
33 | } else {
|
34 | // IE8 does not implement `charCode`, but `keyCode` has the correct value.
|
35 | charCode = keyCode;
|
36 | }
|
37 |
|
38 | // Some non-printable keys are reported in `charCode`/`keyCode`, discard them.
|
39 | // Must not discard the (non-)printable Enter-key.
|
40 | if (charCode >= 32 || charCode === 13) {
|
41 | return charCode;
|
42 | }
|
43 |
|
44 | return 0;
|
45 | }
|
46 |
|
47 | module.exports = getEventCharCode; |
\ | No newline at end of file |