From 14788de6d2d40fff332156e5928cc2cb4754e3b0 Mon Sep 17 00:00:00 2001 From: Sam Reid Date: Thu, 7 Mar 2024 12:59:32 -0700 Subject: [PATCH] Convert FieldRadioButtonGroup to use global hotkeys, see https://github.com/phetsims/projectile-data-lab/issues/63 --- js/common-vsm/view/FieldRadioButtonGroup.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/js/common-vsm/view/FieldRadioButtonGroup.ts b/js/common-vsm/view/FieldRadioButtonGroup.ts index 7d851d08..b88af905 100644 --- a/js/common-vsm/view/FieldRadioButtonGroup.ts +++ b/js/common-vsm/view/FieldRadioButtonGroup.ts @@ -80,14 +80,21 @@ export default class FieldRadioButtonGroup 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; + } + } } } ) ); }