Skip to content

Commit

Permalink
add an ignoredModifierKeys option to Hotkey, see #1621
Browse files Browse the repository at this point in the history
  • Loading branch information
jessegreenberg committed Mar 28, 2024
1 parent 01b68e4 commit 5868022
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 6 additions & 1 deletion js/input/Hotkey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ type SelfOptions = {
// Note that the release of a modifier key may "activate" the hotkey for "fire-on-hold", but not for "fire-on-down".
modifierKeys?: EnglishKey[];

// TODO: consider the ability to "not" match modifier keys? https://github.com/phetsims/scenery/issues/1621
// A set of modifier keys that can be down and the hotkey will still fire. Essentially ignoring the modifier
// key behavior for this key.
ignoredModifierKeys?: EnglishKey[];

// Called as fire() when the hotkey is tired
fire?: ( event: KeyboardEvent | null ) => void;
Expand Down Expand Up @@ -67,6 +69,7 @@ export default class Hotkey extends EnabledComponent {
// Straight from options
public readonly key: EnglishKey;
public readonly modifierKeys: EnglishKey[];
public readonly ignoredModifierKeys: EnglishKey[];
public readonly fire: ( event: KeyboardEvent | null ) => void;
public readonly fireOnDown: boolean;
public readonly fireOnHold: boolean;
Expand Down Expand Up @@ -97,6 +100,7 @@ export default class Hotkey extends EnabledComponent {

const options = optionize<HotkeyOptions, SelfOptions, EnabledComponentOptions>()( {
modifierKeys: [],
ignoredModifierKeys: [],
fire: _.noop,
fireOnDown: true,
fireOnHold: false,
Expand All @@ -110,6 +114,7 @@ export default class Hotkey extends EnabledComponent {
// Store public things
this.key = options.key;
this.modifierKeys = options.modifierKeys;
this.ignoredModifierKeys = options.ignoredModifierKeys;
this.fire = options.fire;
this.fireOnDown = options.fireOnDown;
this.fireOnHold = options.fireOnHold;
Expand Down
3 changes: 2 additions & 1 deletion js/input/hotkeyManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ class HotkeyManager {

// See whether the modifier keys match
return this.modifierKeys.every( modifierKey => {
return englishKeysDown.has( modifierKey ) === hotkey.modifierKeys.includes( modifierKey );
return englishKeysDown.has( modifierKey ) === hotkey.modifierKeys.includes( modifierKey ) ||
hotkey.ignoredModifierKeys.includes( modifierKey );
} );
} );

Expand Down

0 comments on commit 5868022

Please sign in to comment.