Skip to content

Commit

Permalink
Fix #198: Consolidate and expand list of non-printable keys
Browse files Browse the repository at this point in the history
  • Loading branch information
greena13 committed Jun 17, 2019
1 parent 5361346 commit 1135f84
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 99 deletions.
27 changes: 5 additions & 22 deletions src/const/NonPrintableKeysDictionary.js
Original file line number Diff line number Diff line change
@@ -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;
16 changes: 0 additions & 16 deletions src/const/SpecialKeysDictionary.js

This file was deleted.

13 changes: 13 additions & 0 deletions src/helpers/parsing-key-maps/isNonPrintableKeyName.js
Original file line number Diff line number Diff line change
@@ -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;
13 changes: 0 additions & 13 deletions src/helpers/parsing-key-maps/isSpecialKey.js

This file was deleted.

7 changes: 5 additions & 2 deletions src/helpers/parsing-key-maps/isValidKey.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/resolving-handlers/hasKeyPressEvent.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
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
* @param {NormalizedKeyName} keyName Name of the key
* @returns {Boolean} Whether the key has a native keypress event
*/
function hasKeyPressEvent(keyName) {
return !NonPrintableKeysDictionary[keyName];
return !isNonPrintableKeyName(keyName);
}

export default hasKeyPressEvent;
45 changes: 1 addition & 44 deletions src/vendor/react-dom/reactsGetEventKey.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import getEventCharCode from './getEventCharCode';
import translateToKey from './translateToKey';

/**
* Normalization of deprecated HTML5 `key` values
Expand All @@ -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.
Expand Down
45 changes: 45 additions & 0 deletions src/vendor/react-dom/translateToKey.js
Original file line number Diff line number Diff line change
@@ -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;

0 comments on commit 1135f84

Please sign in to comment.