diff --git a/src/const/NonPrintableKeysDictionary.js b/src/const/NonPrintableKeysDictionary.js index 7769f6a0..90af4cf3 100644 --- a/src/const/NonPrintableKeysDictionary.js +++ b/src/const/NonPrintableKeysDictionary.js @@ -1,27 +1,10 @@ /** - * Dictionary of keys that do not natively have a keypress event + * Dictionary of keys whose name is not a single symbol or character */ -const NonPrintableKeysDictionary = { - Shift: true, - Control: true, - Alt: true, - Meta: true, - Enter: true, - Tab: true, - Backspace: true, - ArrowRight: true, - ArrowLeft: true, - ArrowUp: true, - ArrowDown: true, - /** - * Caps lock is a strange case where it not only does not have a key press event, - * but it's keyup event is triggered when caps lock is toggled off - */ - CapsLock: true, -}; +import dictionaryFrom from '../utils/object/dictionaryFrom'; +import translateToKey from '../vendor/react-dom/translateToKey'; -for(let i = 1; i < 13; i++) { - NonPrintableKeysDictionary[`F${i}`] = true; -} +const NonPrintableKeysDictionary = + dictionaryFrom(Object.values(translateToKey), true); export default NonPrintableKeysDictionary; diff --git a/src/const/SpecialKeysDictionary.js b/src/const/SpecialKeysDictionary.js deleted file mode 100644 index 8ab22b3d..00000000 --- a/src/const/SpecialKeysDictionary.js +++ /dev/null @@ -1,16 +0,0 @@ -/** - * Dictionary of keys whose name is not a single symbol or character - */ -const SpecialKeysDictionary = { - Shift: true, - Control: true, - Alt: true, - Meta: true, - Enter: true, - Tab: true, - CapsLock: true, - Backspace: true, - Escape: true, -}; - -export default SpecialKeysDictionary; diff --git a/src/helpers/parsing-key-maps/isNonPrintableKeyName.js b/src/helpers/parsing-key-maps/isNonPrintableKeyName.js new file mode 100644 index 00000000..eb1f0385 --- /dev/null +++ b/src/helpers/parsing-key-maps/isNonPrintableKeyName.js @@ -0,0 +1,13 @@ +import NonPrintableKeysDictionary from '../../const/NonPrintableKeysDictionary'; + +/** + * Whether the specified key is a valid key name that is not a single character or + * symbol + * @param {ReactKeyName} keyName Name of the key + * @returns {boolean} Whether the key is a valid special key + */ +function isNonPrintableKeyName(keyName) { + return !!NonPrintableKeysDictionary[keyName]; +} + +export default isNonPrintableKeyName; diff --git a/src/helpers/parsing-key-maps/isSpecialKey.js b/src/helpers/parsing-key-maps/isSpecialKey.js deleted file mode 100644 index 5bb92056..00000000 --- a/src/helpers/parsing-key-maps/isSpecialKey.js +++ /dev/null @@ -1,13 +0,0 @@ -import SpecialKeysDictionary from '../../const/SpecialKeysDictionary'; - -/** - * Whether the specified key is a valid key name that is not a single character or - * symbol - * @param {ReactKeyName} key Name of the key - * @returns {boolean} Whether the key is a valid special key - */ -function isSpecialKey(key) { - return !!SpecialKeysDictionary[key]; -} - -export default isSpecialKey; diff --git a/src/helpers/parsing-key-maps/isValidKey.js b/src/helpers/parsing-key-maps/isValidKey.js index 6a73b817..ff0afc36 100644 --- a/src/helpers/parsing-key-maps/isValidKey.js +++ b/src/helpers/parsing-key-maps/isValidKey.js @@ -1,7 +1,10 @@ -import isSpecialKey from './isSpecialKey'; +import isNonPrintableKeyName from './isNonPrintableKeyName'; +import Configuration from '../../lib/Configuration'; function isValidKey(keyName) { - return isSpecialKey(keyName) || String.fromCharCode(keyName.charCodeAt(0)) === keyName; + return isNonPrintableKeyName(keyName) || + String.fromCharCode(keyName.charCodeAt(0)) === keyName || + Configuration.option('customKeyCodes')[keyName]; } export class InvalidKeyNameError extends Error { diff --git a/src/helpers/resolving-handlers/hasKeyPressEvent.js b/src/helpers/resolving-handlers/hasKeyPressEvent.js index 5588e402..1d37ac22 100644 --- a/src/helpers/resolving-handlers/hasKeyPressEvent.js +++ b/src/helpers/resolving-handlers/hasKeyPressEvent.js @@ -1,4 +1,4 @@ -import NonPrintableKeysDictionary from '../../const/NonPrintableKeysDictionary'; +import isNonPrintableKeyName from '../parsing-key-maps/isNonPrintableKeyName'; /** * Whether the specified key name is for a key that has a native keypress event @@ -6,7 +6,7 @@ import NonPrintableKeysDictionary from '../../const/NonPrintableKeysDictionary'; * @returns {Boolean} Whether the key has a native keypress event */ function hasKeyPressEvent(keyName) { - return !NonPrintableKeysDictionary[keyName]; + return !isNonPrintableKeyName(keyName); } export default hasKeyPressEvent; diff --git a/src/vendor/react-dom/reactsGetEventKey.js b/src/vendor/react-dom/reactsGetEventKey.js index 210e1166..69b1e03e 100644 --- a/src/vendor/react-dom/reactsGetEventKey.js +++ b/src/vendor/react-dom/reactsGetEventKey.js @@ -8,6 +8,7 @@ */ import getEventCharCode from './getEventCharCode'; +import translateToKey from './translateToKey'; /** * Normalization of deprecated HTML5 `key` values @@ -28,50 +29,6 @@ const normalizeKey = { MozPrintableKey: 'Unidentified', }; -/** - * Translation from legacy `keyCode` to HTML5 `key` - * Only special keys supported, all others depend on keyboard layout or browser - * @see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#Key_names - */ -const translateToKey = { - '8': 'Backspace', - '9': 'Tab', - '12': 'Clear', - '13': 'Enter', - '16': 'Shift', - '17': 'Control', - '18': 'Alt', - '19': 'Pause', - '20': 'CapsLock', - '27': 'Escape', - '32': ' ', - '33': 'PageUp', - '34': 'PageDown', - '35': 'End', - '36': 'Home', - '37': 'ArrowLeft', - '38': 'ArrowUp', - '39': 'ArrowRight', - '40': 'ArrowDown', - '45': 'Insert', - '46': 'Delete', - '112': 'F1', - '113': 'F2', - '114': 'F3', - '115': 'F4', - '116': 'F5', - '117': 'F6', - '118': 'F7', - '119': 'F8', - '120': 'F9', - '121': 'F10', - '122': 'F11', - '123': 'F12', - '144': 'NumLock', - '145': 'ScrollLock', - '224': 'Meta', -}; - /** * @param {object} nativeEvent Native browser event. * @return {string} Normalized `key` property. diff --git a/src/vendor/react-dom/translateToKey.js b/src/vendor/react-dom/translateToKey.js new file mode 100644 index 00000000..5e0b78da --- /dev/null +++ b/src/vendor/react-dom/translateToKey.js @@ -0,0 +1,45 @@ +/** + * Translation from legacy `keyCode` to HTML5 `key` + * Only special keys supported, all others depend on keyboard layout or browser + * @see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent#Key_names + */ +const translateToKey = { + '8': 'Backspace', + '9': 'Tab', + '12': 'Clear', + '13': 'Enter', + '16': 'Shift', + '17': 'Control', + '18': 'Alt', + '19': 'Pause', + '20': 'CapsLock', + '27': 'Escape', + '32': ' ', + '33': 'PageUp', + '34': 'PageDown', + '35': 'End', + '36': 'Home', + '37': 'ArrowLeft', + '38': 'ArrowUp', + '39': 'ArrowRight', + '40': 'ArrowDown', + '45': 'Insert', + '46': 'Delete', + '112': 'F1', + '113': 'F2', + '114': 'F3', + '115': 'F4', + '116': 'F5', + '117': 'F6', + '118': 'F7', + '119': 'F8', + '120': 'F9', + '121': 'F10', + '122': 'F11', + '123': 'F12', + '144': 'NumLock', + '145': 'ScrollLock', + '224': 'Meta', +}; + +export default translateToKey;