Skip to content

Commit

Permalink
example of createGlobal, see phetsims/scenery#1570
Browse files Browse the repository at this point in the history
  • Loading branch information
jessegreenberg committed Apr 26, 2024
1 parent edc2f57 commit 86181c0
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions doc/alternative-input-quickstart-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,29 +219,38 @@ functionality.

## Hotkeys

Hotkeys are added with `KeyboardListener` like this:
Hotkeys are added with `KeyboardListener`. A KeyboardListener can be added to a Node, and will fire its callback
whenever the specified keys are pressed while the Node has focus. Here is an example:

```ts

const keyboardListener = new KeyboardListener( {
keys: [ 'escape', 'j+0' ],
callback: ( event, keysPressed, listener ) => {
fire: ( event, keysPressed, listener ) => {
if ( keysPressed === 'escape' ) {
// escape key was pressed
}
else if ( 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 );
```

You can also add a global Hotkey that will fire regardless of which Node has focus. As long as the target Node can
receive input events, the listener will fire. Here is an example:

```ts
const globalKeyboardListener = KeyboardListener.createGlobal( targetNode, {
keys: [ 'alt+r' ],
fire: ( event, keysPressed, listener ) => {
// alt+r was pressed
}
} );
```

Be careful not to add hotkeys that collide with other global hotkeys defined by PhET such as hotkeys that pan and zoom
into the sim. We need a list of global hotkeys or a way to automatically prevent collisions but do not have that yet. It
will throw an assertion at runtime though if there is an overlap. See https://github.com/phetsims/phet-info/issues/188.
Expand Down

0 comments on commit 86181c0

Please sign in to comment.