Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
Add support for non-latin characters
Browse files Browse the repository at this point in the history
  • Loading branch information
Belchy06 committed Dec 13, 2023
1 parent 87b9c9a commit 8b838f4
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion Frontend/library/src/Inputs/KeyboardController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,21 @@ export class KeyboardController {
* Registers document keyboard events with the controller
*/
registerKeyBoardEvents() {
const compositionEndHandler = (ev: CompositionEvent) => this.handleOnCompositionEnd(ev);
const keyDownHandler = (ev: KeyboardEvent) => this.handleOnKeyDown(ev);
const keyUpHandler = (ev: KeyboardEvent) => this.handleOnKeyUp(ev);
const keyPressHandler = (ev: KeyboardEvent) => this.handleOnKeyPress(ev);

document.addEventListener("compositionend", compositionEndHandler);
document.addEventListener("keydown", keyDownHandler);
document.addEventListener("keyup", keyUpHandler);

//This has been deprecated as at Jun 13 2021
document.addEventListener("keypress", keyPressHandler);

this.keyboardEventListenerTracker.addUnregisterCallback(
() => document.removeEventListener("compositionend", compositionEndHandler)
);
this.keyboardEventListenerTracker.addUnregisterCallback(
() => document.removeEventListener("keydown", keyDownHandler)
);
Expand All @@ -184,7 +189,7 @@ export class KeyboardController {
*/
handleOnKeyDown(keyboardEvent: KeyboardEvent) {
const keyCode = this.getKeycode(keyboardEvent);
if (!keyCode) {
if (!keyCode || keyCode === 229) {
return;
}

Expand Down Expand Up @@ -263,6 +268,37 @@ export class KeyboardController {
toStreamerHandlers.get('KeyPress')([charCode]);
}

/**
* Handle whenever composition ends (eg chinese simplified)
* @param compositionEvent - the composition event
*/
handleOnCompositionEnd(compositionEvent: CompositionEvent) {
if (compositionEvent.data && compositionEvent.data.length) {
compositionEvent.data.split('').forEach((char) => {
// This keydown, keypress, keyup flow is required to mimic the way characters are
// normally triggered
this.handleOnKeyDown(
new KeyboardEvent('keydown', {
keyCode: char.toUpperCase().charCodeAt(0),
charCode: char.charCodeAt(0)
})
);
this.handleOnKeyPress(
new KeyboardEvent('keypress', {
keyCode: char.toUpperCase().charCodeAt(0),
charCode: char.charCodeAt(0)
})
);
this.handleOnKeyUp(
new KeyboardEvent('keyup', {
keyCode: char.toUpperCase().charCodeAt(0),
charCode: char.charCodeAt(0)
})
);
});
}
}

/**
* Gets the Keycode of the Key pressed
* @param keyboardEvent - Key board Event
Expand Down

0 comments on commit 8b838f4

Please sign in to comment.