fix: capture keyboard event that has no key

This commit is contained in:
Enix 2019-09-24 16:38:10 +08:00 committed by EnixCoda
parent e15bab98fc
commit a3c8a155c2
No known key found for this signature in database
GPG key ID: 0C1A07377913A1DD

View file

@ -38,15 +38,20 @@ function parseKeyCode(code: string) {
function parseEvent(e: KeyboardEvent | React.KeyboardEvent) {
const { altKey: alt, shiftKey: shift, metaKey: meta, ctrlKey: ctrl } = e
const code = parseKeyCode(e.key)
const keys = { meta, ctrl, shift, alt, [code]: true }
const combination = parse(
Object.entries(keys)
.filter(([key, pressed]) => pressed)
.map(([key, pressed]) => key)
.join('+')
)
return combination
try {
const code = parseKeyCode(e.key)
const keys = { meta, ctrl, shift, alt, [code]: true }
const combination = parse(
Object.entries(keys)
.filter(([key, pressed]) => pressed)
.map(([key, pressed]) => key)
.join('+'),
)
return combination
} catch (err) {
const serializedKeyData = JSON.stringify({ keyCode: e.keyCode, key: e.key })
throw new Error(`Error parse keyboard event: ${serializedKeyData}`)
}
}
export default {