fix: use pressed key rather than its code (#3978)

Co-authored-by: Joel Jacob Stephen <70131076+JoelJacobStephen@users.noreply.github.com>
This commit is contained in:
Eduardo San Martin Morote
2024-04-19 19:05:13 +02:00
committed by GitHub
parent 51412549e8
commit 08c655235d
3 changed files with 15105 additions and 13034 deletions

View File

@@ -132,30 +132,23 @@ function generateKeybindingString(ev: KeyboardEvent): ShortcutKey | null {
function getPressedKey(ev: KeyboardEvent): Key | null {
// Sometimes the property code is not available on the KeyboardEvent object
const val = (ev.code ?? "").toLowerCase()
const key = (ev.key ?? "").toLowerCase()
// Check arrow keys
if (val === "arrowup") return "up"
else if (val === "arrowdown") return "down"
else if (val === "arrowleft") return "left"
else if (val === "arrowright") return "right"
if (key.startsWith("arrow")) {
return key.slice(5) as Key
}
// Check letter keys
const isLetter = val.startsWith("key")
if (isLetter) return val.substring(3) as Key
const isLetter = key.length === 1 && key >= "a" && key <= "z"
if (isLetter) return key as Key
// Check if number keys
const isDigit = val.startsWith("digit")
if (isDigit) return val.substring(5) as Key
const isDigit = key.length === 1 && key >= "0" && key <= "9"
if (isDigit) return key as Key
// Check if slash
if (val === "slash") return "/"
// Check if period
if (val === "period") return "."
// Check if enter
if (val === "enter") return "enter"
// Check if slash, period or enter
if (key === "/" || key === "." || key === "enter") return key
// If no other cases match, this is not a valid key
return null