Skip to content

Commit

Permalink
attach globalKeyStateTracker listeners via scenery input, #1445
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Oct 26, 2022
1 parent 168b469 commit 786d7e4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
4 changes: 2 additions & 2 deletions js/accessibility/KeyStateTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,8 @@ class KeyStateTracker {
const addListenersToDocument = () => {

// attach with useCapture so that the keyStateTracker is updated before the events dispatch within Scenery
window.addEventListener( 'keyup', this.documentKeyupListener!, true );
window.addEventListener( 'keydown', this.documentKeydownListener!, true );
window.addEventListener( 'keyup', this.documentKeyupListener!, { capture: true } );
window.addEventListener( 'keydown', this.documentKeydownListener!, { capture: true } );
this.attachedToDocument = true;
};

Expand Down
1 change: 0 additions & 1 deletion js/accessibility/globalKeyStateTracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { KeyStateTrackerOptions } from './KeyStateTracker.js';
class GlobalKeyStateTracker extends KeyStateTracker {
public constructor( options?: KeyStateTrackerOptions ) {
super( options );
this.attachToWindow();
}
}

Expand Down
13 changes: 12 additions & 1 deletion js/input/BrowserEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@

import arrayRemove from '../../../phet-core/js/arrayRemove.js';
import platform from '../../../phet-core/js/platform.js';
import { BatchedDOMEventType, Display, Features, PDOMUtils, scenery } from '../imports.js';
import { BatchedDOMEventType, Display, Features, globalKeyStateTracker, PDOMUtils, scenery } from '../imports.js';

// Sometimes we need to add a listener that does absolutely nothing
const noop = () => {};

// Ensure we only attach global window listeners (display independent) once
let isGloballyAttached = false;

const BrowserEvents = {
/**
* Adds a Display to the list of displays that will be notified of input events.
Expand All @@ -29,6 +32,14 @@ const BrowserEvents = {
assert && assert( !_.includes( this.attachedDisplays, display ),
'A display cannot be concurrently attached to events more than one time' );

// Always first please
if ( !isGloballyAttached ) {
isGloballyAttached = true;

// never unattach because we don't know if there are other Displays listening to this.
globalKeyStateTracker.attachToWindow();
}

this.attachedDisplays.push( display );

if ( attachToWindow ) {
Expand Down

0 comments on commit 786d7e4

Please sign in to comment.