From f1bc31a4381f55c160868e9d00ff1b8e3d7e42e3 Mon Sep 17 00:00:00 2001 From: Aki Hamano <54422211+t-hamano@users.noreply.github.com> Date: Mon, 13 Feb 2023 18:14:50 +0900 Subject: [PATCH] Distraction free mode: Fix keyboard shortcut not working (#47900) * Distraction free mode: Fix keyboard shortcut not working on Windows * Include mac --- packages/keycodes/src/index.js | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/packages/keycodes/src/index.js b/packages/keycodes/src/index.js index 2aa1cb70fa3f3..cd31fef5fa719 100644 --- a/packages/keycodes/src/index.js +++ b/packages/keycodes/src/index.js @@ -390,6 +390,14 @@ export const isKeyboardEvent = mapValues( ) => { const mods = getModifiers( _isApple ); const eventMods = getEventModifiers( event ); + /** @type {Record} */ + const replacementWithShiftKeyMap = { + Comma: ',', + Backslash: '\\', + // Windows returns `\` for both IntlRo and IntlYen. + IntlRo: '\\', + IntlYen: '\\', + }; const modsDiff = mods.filter( ( mod ) => ! eventMods.includes( mod ) @@ -412,16 +420,17 @@ export const isKeyboardEvent = mapValues( key = String.fromCharCode( event.keyCode ).toLowerCase(); } - // Replace some characters to match the key indicated - // by the shortcut on Windows. - if ( ! _isApple() ) { - if ( - event.shiftKey && - character.length === 1 && - event.code === 'Comma' - ) { - key = ','; - } + // `event.key` returns the value of the key pressed, taking into the state of + // modifier keys such as `Shift`. If the shift key is pressed, a different + // value may be returned depending on the keyboard layout. It is necessary to + // convert to the physical key value that don't take into account keyboard + // layout or modifier key state. + if ( + event.shiftKey && + character.length === 1 && + replacementWithShiftKeyMap[ event.code ] + ) { + key = replacementWithShiftKeyMap[ event.code ]; } // For backwards compatibility.