From 014658a709754cc36c0727425521c9add2a485ac Mon Sep 17 00:00:00 2001 From: Jesse Date: Thu, 25 Apr 2024 15:38:01 -0400 Subject: [PATCH] don't include the shift key in keys so that isPressedProperty doesn't trigger from the shift key, see https://github.com/phetsims/scenery/issues/1570 --- js/listeners/KeyboardDragListener.ts | 46 +++++++++------------------- js/listeners/KeyboardListener.ts | 7 +++++ 2 files changed, 22 insertions(+), 31 deletions(-) diff --git a/js/listeners/KeyboardDragListener.ts b/js/listeners/KeyboardDragListener.ts index 7053a30a1..ac574d0ab 100644 --- a/js/listeners/KeyboardDragListener.ts +++ b/js/listeners/KeyboardDragListener.ts @@ -16,7 +16,7 @@ import Transform3 from '../../../dot/js/Transform3.js'; import Vector2 from '../../../dot/js/Vector2.js'; import EventType from '../../../tandem/js/EventType.js'; import Tandem from '../../../tandem/js/Tandem.js'; -import { KeyboardListener, KeyboardListenerOptions, KeyboardUtils, Node, PDOMPointer, scenery, SceneryEvent, TInputListener } from '../imports.js'; +import { globalKeyStateTracker, KeyboardListener, KeyboardListenerOptions, KeyboardUtils, Node, PDOMPointer, scenery, SceneryEvent, TInputListener } from '../imports.js'; import TProperty from '../../../axon/js/TProperty.js'; import optionize, { EmptySelfOptions } from '../../../phet-core/js/optionize.js'; import TReadOnlyProperty from '../../../axon/js/TReadOnlyProperty.js'; @@ -27,13 +27,14 @@ import CallbackTimer from '../../../axon/js/CallbackTimer.js'; import PickOptional from '../../../phet-core/js/types/PickOptional.js'; import platform from '../../../phet-core/js/platform.js'; import StrictOmit from '../../../phet-core/js/types/StrictOmit.js'; -import DerivedProperty from '../../../axon/js/DerivedProperty.js'; import { EnabledComponentOptions } from '../../../axon/js/EnabledComponent.js'; import Emitter from '../../../axon/js/Emitter.js'; -const allKeys = [ 'arrowLeft', 'arrowRight', 'arrowUp', 'arrowDown', 'w', 'a', 's', 'd', 'shift' ] as const; -const leftRightKeys = [ 'arrowLeft', 'arrowRight', 'a', 'd', 'shift' ] as const; -const upDownKeys = [ 'arrowUp', 'arrowDown', 'w', 's', 'shift' ] as const; +// 'shift' is not included in any list of keys because we don't want the KeyboardListener to be 'pressed' when only +// the shift key is down. State of the shift key is tracked by the globalKeyStateTracker. +const allKeys = [ 'arrowLeft', 'arrowRight', 'arrowUp', 'arrowDown', 'w', 'a', 's', 'd' ] as const; +const leftRightKeys = [ 'arrowLeft', 'arrowRight', 'a', 'd' ] as const; +const upDownKeys = [ 'arrowUp', 'arrowDown', 'w', 's' ] as const; type KeyboardDragListenerKeyStroke = typeof allKeys | typeof leftRightKeys | typeof upDownKeys; @@ -147,7 +148,6 @@ class KeyboardDragListener extends KeyboardListener( false ); private upKeyDownProperty = new TinyProperty( false ); private downKeyDownProperty = new TinyProperty( false ); - private shiftKeyDownProperty = new TinyProperty( false ); // Fires to conduct the start and end of a drag, added for PhET-iO interoperability private dragStartAction: PhetioAction<[ SceneryEvent ]>; @@ -180,9 +180,6 @@ class KeyboardDragListener extends KeyboardListener; - // The callback timer that is used to move the object during a drag operation to support animated motion and // motion every moveOnHoldInterval. private readonly callbackTimer: CallbackTimer; @@ -227,6 +224,9 @@ class KeyboardDragListener extends KeyboardListener>()( { keys: keys, + + // We still want to start drag operations when the shift modifier key is pressed, even though it is not + // listed in keys. ignoredModifierKeys: [ 'shift' ] }, options ); @@ -248,7 +248,6 @@ class KeyboardDragListener extends KeyboardListener { - this.shiftKeyDownProperty.value = pressedKeys.includes( 'shift' ); this.leftKeyDownProperty.value = pressedKeys.includes( 'arrowLeft' ) || pressedKeys.includes( 'a' ); this.rightKeyDownProperty.value = pressedKeys.includes( 'arrowRight' ) || pressedKeys.includes( 'd' ); this.upKeyDownProperty.value = pressedKeys.includes( 'arrowUp' ) || pressedKeys.includes( 'w' ); @@ -325,15 +324,17 @@ class KeyboardDragListener extends KeyboardListener { + this.isPressedProperty.lazyLink( dragKeysDown => { if ( dragKeysDown ) { this.startNextKeyboardEvent = true; } @@ -426,7 +418,6 @@ class KeyboardDragListener extends KeyboardListener { - this.movementKeyPressedProperty.dispose(); this.leftKeyDownProperty.dispose(); this.rightKeyDownProperty.dispose(); @@ -504,13 +495,6 @@ class KeyboardDragListener extends KeyboardListener extends EnabledComp this.enabledProperty.lazyLink( this.onEnabledPropertyChange.bind( this ) ); } + /** + * Whether this listener is currently activated with a press. + */ + public get isPressed(): boolean { + return this.isPressedProperty.value; + } + /** * Fired when the enabledProperty changes */