Skip to content

Commit

Permalink
refer to KeyboardListener instead of globalKeyStateTracker in phet-in…
Browse files Browse the repository at this point in the history
  • Loading branch information
jessegreenberg committed Jul 19, 2023
1 parent 8fb7a33 commit f8ecfc9
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions doc/alternative-input-quickstart-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,24 +182,27 @@ keyboardDragListener.hotkeys = [
];
```

If your Node does not have a `KeyboardDragListener`, add hotkeys like this:
If your Node does not have a `KeyboardDragListener`, add hotkeys with `KeyboardListener` like this:

```js
globalKeyStateTracker.keydownEmitter.addListener( event => {

// Make sure the event is intended for this Node.
if ( this.pdomDisplayed && this.enabledProperty.value ) {
if ( KeyboardUtils.isKeyEvent( event, KeyboardUtils.KEY_ESCAPE ) ) {
// Escape
...
const keyboardListener = new KeyboardListener( {
keys: [ 'escape', 'j+0' ],
callback: ( event, listener ) => {
if ( listener.keysPressed === 'escape' ) {
// escape key was pressed
}
else if ( globalKeyStateTracker.altKeyDown &&
KeyboardUtils.isKeyEvent( event, KeyboardUtils.KEY_C ) ){
// Alt+C
...
else if ( listener.keysPressed === 'j+0' ) {
// j and 0 were pressed
}
}
},

// By making this listener "global" it will fire no matter where focus is in the document as long as
// myNode is visible and has input enabled. If this is false, callback will only fire when myNode has keyboard focus.
global: true
} );

myNode.addInputListener( keyboardListener );
```

Be careful not to add hotkeys that collide with other global hotkeys defined by PhET such as hotkeys that pan and zoom
Expand All @@ -208,7 +211,7 @@ See https://github.com/phetsims/phet-info/issues/188.

## Scenery Events

If the above are not sufficient you can add input listeners with Scenery's input system that are related to alternative
For more custom behavior you can add input listeners with Scenery's input system that are related to alternative
input. For example, if you want to add behavior whenever a Node has focus you can add a listener like this:

```js
Expand Down

0 comments on commit f8ecfc9

Please sign in to comment.