Skip to content

Commit

Permalink
RichText: Fix dead key input on Windows (#37777)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcsf authored Jan 10, 2022
1 parent fa0fc7d commit 7f31913
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion packages/rich-text/src/component/use-space.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,38 @@ export function useSpace() {
return;
}

const { keyCode, altKey, metaKey, ctrlKey } = event;
const { keyCode, altKey, metaKey, ctrlKey, key } = event;

// Only consider the space key without modifiers pressed.
if ( keyCode !== SPACE || altKey || metaKey || ctrlKey ) {
return;
}

// Disregard character composition that involves the Space key.
//
// @see https://github.com/WordPress/gutenberg/issues/35086
//
// For example, to input a standalone diacritic (like ´ or `) using a
// keyboard with dead keys, one must first press the dead key and then
// press the Space key.
//
// Many operating systems handle this in such a way that the second
// KeyboardEvent contains the property `keyCode: 229`. According to the
// spec, 229 allows the system to indicate that an Input Method Editor
// (IDE) is processing some key input.
//
// However, Windows doesn't use `keyCode: 229` for dead key composition,
// instead emitting an event with values `keyCode: SPACE` and `key: '´'`.
// That is why checking the `key` property for values other than `SPACE`
// is important.
//
// This should serve as a reminder that the `KeyboardEvent.keyCode`
// attribute is officially deprecated and that we should consider more
// consistent interfaces.
if ( key !== ' ' ) {
return;
}

event.target.ownerDocument.execCommand( 'insertText', false, ' ' );
event.preventDefault();
}
Expand Down

0 comments on commit 7f31913

Please sign in to comment.