Files
react/components/use-keyboard/helper.ts
witt fc06a02335 feat: useKeyboard hooks (#541)
* feat(keyboard): create keyboard hooks

* feat(usekeyboard): redesign event handler to match keyboard events from browser

\

* test(usekeyboard): add testcase

* docs(usekeyboard): create new hooks document
2021-05-24 00:17:58 +08:00

28 lines
761 B
TypeScript

import { isMac } from '../utils/collections'
import { KeyMod } from './codes'
/* istanbul ignore next */
export const getCtrlKeysByPlatform = (): Record<string, 'metaKey' | 'ctrlKey'> => {
return {
CtrlCmd: isMac() ? 'metaKey' : 'ctrlKey',
WinCtrl: isMac() ? 'ctrlKey' : 'metaKey',
}
}
export const getActiveModMap = (
bindings: number[],
): Record<keyof typeof KeyMod, boolean> => {
const modBindings = bindings.filter((item: number) => !!KeyMod[item])
const activeModMap: Record<keyof typeof KeyMod, boolean> = {
CtrlCmd: false,
Shift: false,
Alt: false,
WinCtrl: false,
}
modBindings.forEach(code => {
const modKey = KeyMod[code] as keyof typeof KeyMod
activeModMap[modKey] = true
})
return activeModMap
}