1. /**
    
  2.  * Copyright (c) Meta Platforms, Inc. and affiliates.
    
  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.  * @flow
    
  8.  */
    
  9. 
    
  10. /**
    
  11.  * `charCode` represents the actual "character code" and is safe to use with
    
  12.  * `String.fromCharCode`. As such, only keys that correspond to printable
    
  13.  * characters produce a valid `charCode`, the only exception to this is Enter.
    
  14.  * The Tab-key is considered non-printable and does not have a `charCode`,
    
  15.  * presumably because it does not produce a tab-character in browsers.
    
  16.  *
    
  17.  * @param {object} nativeEvent Native browser event.
    
  18.  * @return {number} Normalized `charCode` property.
    
  19.  */
    
  20. function getEventCharCode(nativeEvent: KeyboardEvent): number {
    
  21.   let charCode;
    
  22.   const keyCode = nativeEvent.keyCode;
    
  23. 
    
  24.   if ('charCode' in nativeEvent) {
    
  25.     charCode = nativeEvent.charCode;
    
  26. 
    
  27.     // FF does not set `charCode` for the Enter-key, check against `keyCode`.
    
  28.     if (charCode === 0 && keyCode === 13) {
    
  29.       charCode = 13;
    
  30.     }
    
  31.   } else {
    
  32.     // IE8 does not implement `charCode`, but `keyCode` has the correct value.
    
  33.     charCode = keyCode;
    
  34.   }
    
  35. 
    
  36.   // IE and Edge (on Windows) and Chrome / Safari (on Windows and Linux)
    
  37.   // report Enter as charCode 10 when ctrl is pressed.
    
  38.   if (charCode === 10) {
    
  39.     charCode = 13;
    
  40.   }
    
  41. 
    
  42.   // Some non-printable keys are reported in `charCode`/`keyCode`, discard them.
    
  43.   // Must not discard the (non-)printable Enter-key.
    
  44.   if (charCode >= 32 || charCode === 13) {
    
  45.     return charCode;
    
  46.   }
    
  47. 
    
  48.   return 0;
    
  49. }
    
  50. 
    
  51. export default getEventCharCode;