From bf143ed0b8ba06e67e512978d14d6859833ed83d Mon Sep 17 00:00:00 2001 From: David Luzar Date: Tue, 10 Nov 2020 18:42:13 +0100 Subject: [PATCH] ignore CapsLock when handling keyDown event (#2373) * ignore CapsLock when handling keyDown event * fix `this` --- src/components/App.tsx | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/components/App.tsx b/src/components/App.tsx index 41f4e109..4ecc28f0 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -1518,6 +1518,30 @@ class App extends React.Component { // Input handling private onKeyDown = withBatchedUpdates((event: KeyboardEvent) => { + // normalize `event.key` when CapsLock is pressed #2372 + if ( + "Proxy" in window && + ((!event.shiftKey && /^[A-Z]$/.test(event.key)) || + (event.shiftKey && /^[a-z]$/.test(event.key))) + ) { + event = new Proxy(event, { + get(ev: any, prop) { + const value = ev[prop]; + if (typeof value === "function") { + // fix for Proxies hijacking `this` + return value.bind(ev); + } + return prop === "key" + ? // CapsLock inverts capitalization based on ShiftKey, so invert + // it back + event.shiftKey + ? ev.key.toUpperCase() + : ev.key.toLowerCase() + : value; + }, + }); + } + // ensures we don't prevent devTools select-element feature if (event[KEYS.CTRL_OR_CMD] && event.shiftKey && event.key === "C") { return;