Use code instead of key (#1234)

* Update KeyboardMouseInputField to use .code instead of .key

* Update Display to use .code instead of .key

* Remove unused function
This commit is contained in:
toasted-nutbread 2021-01-14 19:08:05 -05:00 committed by GitHub
parent 33aeae4110
commit c1e4741f22
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 20 deletions

View File

@ -156,7 +156,7 @@ class KeyboardMouseInputField extends EventDispatcher {
// This is a hack and only works when both Shift and Alt are not pressed. // This is a hack and only works when both Shift and Alt are not pressed.
if ( if (
!modifiers.has('meta') && !modifiers.has('meta') &&
DocumentUtil.getKeyFromEvent(e) === 'Meta' && e.key === 'Meta' &&
!( !(
modifiers.size === 2 && modifiers.size === 2 &&
modifiers.has('shift') && modifiers.has('shift') &&
@ -170,10 +170,16 @@ class KeyboardMouseInputField extends EventDispatcher {
_isModifierKey(keyName) { _isModifierKey(keyName) {
switch (keyName) { switch (keyName) {
case 'Alt': case 'AltLeft':
case 'Control': case 'AltRight':
case 'Meta': case 'ControlLeft':
case 'Shift': case 'ControlRight':
case 'MetaLeft':
case 'MetaRight':
case 'ShiftLeft':
case 'ShiftRight':
case 'OSLeft':
case 'OSRight':
return true; return true;
default: default:
return false; return false;
@ -183,7 +189,8 @@ class KeyboardMouseInputField extends EventDispatcher {
_onModifierKeyDown(e) { _onModifierKeyDown(e) {
e.preventDefault(); e.preventDefault();
const key = DocumentUtil.getKeyFromEvent(e); let key = e.code;
if (key === 'Unidentified' || key === '') { key = void 0; }
if (this._keySupported) { if (this._keySupported) {
this._updateModifiers([...this._getModifierKeys(e)], this._isModifierKey(key) ? void 0 : key); this._updateModifiers([...this._getModifierKeys(e)], this._isModifierKey(key) ? void 0 : key);
} else { } else {

View File

@ -140,14 +140,14 @@ class Display extends EventDispatcher {
{key: 'Home', modifiers: ['alt'], action: 'firstEntry'}, {key: 'Home', modifiers: ['alt'], action: 'firstEntry'},
{key: 'ArrowUp', modifiers: ['alt'], action: 'previousEntry'}, {key: 'ArrowUp', modifiers: ['alt'], action: 'previousEntry'},
{key: 'ArrowDown', modifiers: ['alt'], action: 'nextEntry'}, {key: 'ArrowDown', modifiers: ['alt'], action: 'nextEntry'},
{key: 'B', modifiers: ['alt'], action: 'historyBackward'}, {key: 'KeyB', modifiers: ['alt'], action: 'historyBackward'},
{key: 'F', modifiers: ['alt'], action: 'historyForward'}, {key: 'KeyF', modifiers: ['alt'], action: 'historyForward'},
{key: 'K', modifiers: ['alt'], action: 'addNoteKanji'}, {key: 'KeyK', modifiers: ['alt'], action: 'addNoteKanji'},
{key: 'E', modifiers: ['alt'], action: 'addNoteTermKanji'}, {key: 'KeyE', modifiers: ['alt'], action: 'addNoteTermKanji'},
{key: 'R', modifiers: ['alt'], action: 'addNoteTermKana'}, {key: 'KeyR', modifiers: ['alt'], action: 'addNoteTermKana'},
{key: 'P', modifiers: ['alt'], action: 'playAudio'}, {key: 'KeyP', modifiers: ['alt'], action: 'playAudio'},
{key: 'V', modifiers: ['alt'], action: 'viewNote'}, {key: 'KeyV', modifiers: ['alt'], action: 'viewNote'},
{key: 'C', modifiers: ['ctrl'], action: 'copyHostSelection'} {key: 'KeyC', modifiers: ['ctrl'], action: 'copyHostSelection'}
]); ]);
this.registerMessageHandlers([ this.registerMessageHandlers([
['setMode', {async: false, handler: this._onMessageSetMode.bind(this)}] ['setMode', {async: false, handler: this._onMessageSetMode.bind(this)}]
@ -273,7 +273,7 @@ class Display extends EventDispatcher {
} }
onKeyDown(e) { onKeyDown(e) {
const key = DocumentUtil.getKeyFromEvent(e); const key = e.code;
const handlers = this._hotkeys.get(key); const handlers = this._hotkeys.get(key);
if (typeof handlers === 'undefined') { return false; } if (typeof handlers === 'undefined') { return false; }

View File

@ -227,11 +227,6 @@ class DocumentUtil {
return buttons; return buttons;
} }
static getKeyFromEvent(event) {
const key = event.key;
return (typeof key === 'string' ? (key.length === 1 ? key.toUpperCase() : key) : '');
}
static addFullscreenChangeEventListener(onFullscreenChanged, eventListenerCollection=null) { static addFullscreenChangeEventListener(onFullscreenChanged, eventListenerCollection=null) {
const target = document; const target = document;
const options = false; const options = false;