Skip to content

Commit

Permalink
Convert FieldRadioButtonGroup to use global hotkeys, see #63
Browse files Browse the repository at this point in the history
  • Loading branch information
samreid committed Mar 7, 2024
1 parent 054eb10 commit 14788de
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions js/common-vsm/view/FieldRadioButtonGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,21 @@ export default class FieldRadioButtonGroup<T extends Field> extends RectangularR

// a listener that selects a field based on the keystroke, regardless of where focus is in the document
this.addInputListener( new KeyboardListener( {
keys: [ '1', '2', '3', '4', '5', '6' ] as const,
keys: [ 'alt+1', 'alt+2', 'alt+3', 'alt+4', 'alt+5', 'alt+6' ] as const,
global: true,
callback: ( event, keysPressed ) => {
const key = parseInt( keysPressed, 10 );
const key = parseInt( keysPressed.substring( keysPressed.indexOf( '+' ) + 1 ), 10 );
fieldProperty.value = fields[ key - 1 ];

// Move focus to the radio button that was selected. Without this line, focus would incorrectly remain
// on the previous button.
this.getButtonForValue( fieldProperty.value ).focus();
// on the previous button. Only do this if a radio button already had focus, otherwise it would steal focus
for ( let i = 0; i < fieldRadioButtons.length; i++ ) {
const button = this.getButtonForValue( fieldRadioButtons[ i ].value );
if ( button.focused ) {
this.getButtonForValue( fieldProperty.value ).focus();
break;
}
}
}
} ) );
}
Expand Down

0 comments on commit 14788de

Please sign in to comment.