Skip to content

Commit

Permalink
Distraction free mode: Fix keyboard shortcut not working on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
t-hamano committed Feb 9, 2023
1 parent 86d527c commit fd78509
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions packages/keycodes/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,14 @@ export const isKeyboardEvent = mapValues(
) => {
const mods = getModifiers( _isApple );
const eventMods = getEventModifiers( event );
/** @type {Record<string,string>} */
const replacementWithShiftKeyMap = {
Comma: ',',
Backslash: '\\',
// Windows returns `\` for both IntlRo and IntlYen.
IntlRo: '\\',
IntlYen: '\\',
};

const modsDiff = mods.filter(
( mod ) => ! eventMods.includes( mod )
Expand All @@ -412,16 +420,18 @@ 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. For now, this process is limited to Windows only.
if (
! _isApple() &&
event.shiftKey &&
character.length === 1 &&
replacementWithShiftKeyMap[ event.code ]
) {
key = replacementWithShiftKeyMap[ event.code ];
}

// For backwards compatibility.
Expand Down

0 comments on commit fd78509

Please sign in to comment.