diff --git a/js/input/Hotkey.ts b/js/input/Hotkey.ts index 48c3875ee..e92d91019 100644 --- a/js/input/Hotkey.ts +++ b/js/input/Hotkey.ts @@ -10,7 +10,7 @@ * @author Jonathan Olson */ -import { EnglishKey, hotkeyManager, scenery } from '../imports.js'; +import { EnglishKey, EnglishStringToCodeMap, hotkeyManager, scenery } from '../imports.js'; import optionize from '../../../phet-core/js/optionize.js'; import EnabledComponent, { EnabledComponentOptions } from '../../../axon/js/EnabledComponent.js'; import TProperty from '../../../axon/js/TProperty.js'; @@ -158,6 +158,11 @@ export default class Hotkey extends EnabledComponent { this.keys = _.uniq( [ this.key, ...this.modifierKeys ] ); + // Make sure that every key has an entry in the EnglishStringToCodeMap + for ( const key of this.keys ) { + assert && assert( EnglishStringToCodeMap[ key ], `No codes for this key exists, do you need to add it to EnglishStringToCodeMap?: ${key}` ); + } + // Create a timer to handle the optional fire-on-hold feature. if ( this.fireOnHold && this.fireOnHoldTiming === 'custom' ) { this.fireOnHoldTimer = new CallbackTimer( { diff --git a/js/listeners/KeyboardListenerTests.ts b/js/listeners/KeyboardListenerTests.ts index 63d6b6550..963a352e9 100644 --- a/js/listeners/KeyboardListenerTests.ts +++ b/js/listeners/KeyboardListenerTests.ts @@ -66,13 +66,16 @@ QUnit.test( 'KeyboardListener Tests', assert => { bogusListener.dispose(); }, Error, 'Constructor should catch providing bad keys at runtime' ); - const a = new Node( { tagName: 'div' } ); + const a = new Node( { tagName: 'div', focusable: true } ); rootNode.addChild( a ); a.addInputListener( listener ); const domElementA = a.pdomInstances[ 0 ].peer!.primarySibling!; assert.ok( domElementA, 'pdom element needed' ); + // Hotkey uses the focused Trail to determine if it should fire, so we need to focus the element + a.focus(); + triggerKeydownEvent( domElementA, KeyboardUtils.KEY_TAB ); assert.ok( !callbackFired, 'should not fire on tab' ); triggerKeyupEvent( domElementA, KeyboardUtils.KEY_TAB ); @@ -103,7 +106,15 @@ QUnit.test( 'KeyboardListener Tests', assert => { } } } ); + a.addInputListener( listenerWithOverlappingKeys ); + + // TODO: https://github.com/phetsims/scenery/issues/1570, This is a workaround for a problem with hotkeyManager + // where it doesn't update the availableHotkeys when new input listeners are added. We can manually update by + // triggering focus changes. + a.blur(); + a.focus(); + triggerKeydownEvent( domElementA, KeyboardUtils.KEY_P, true ); assert.ok( !pFired, 'p should not fire because control key is down' ); assert.ok( ctrlPFired, 'ctrl P should have fired' );