All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
package.src.parseHotkeys.ts Maven / Gradle / Ivy
import { Hotkey, KeyboardModifiers } from './types'
const reservedModifierKeywords = ['shift', 'alt', 'meta', 'mod', 'ctrl']
const mappedKeys: Record = {
esc: 'escape',
return: 'enter',
'.': 'period',
',': 'comma',
'-': 'slash',
' ': 'space',
'`': 'backquote',
'#': 'backslash',
'+': 'bracketright',
ShiftLeft: 'shift',
ShiftRight: 'shift',
AltLeft: 'alt',
AltRight: 'alt',
MetaLeft: 'meta',
MetaRight: 'meta',
OSLeft: 'meta',
OSRight: 'meta',
ControlLeft: 'ctrl',
ControlRight: 'ctrl',
}
export function mapKey(key?: string): string {
return ((key && mappedKeys[key]) || key || '')
.trim()
.toLowerCase()
.replace(/key|digit|numpad|arrow/, '')
}
export function isHotkeyModifier(key: string) {
return reservedModifierKeywords.includes(key)
}
export function parseKeysHookInput(keys: string, splitKey = ','): string[] {
return keys.split(splitKey)
}
export function parseHotkey(hotkey: string, combinationKey = '+', description?: string): Hotkey {
const keys = hotkey
.toLocaleLowerCase()
.split(combinationKey)
.map((k) => mapKey(k))
const modifiers: KeyboardModifiers = {
alt: keys.includes('alt'),
ctrl: keys.includes('ctrl') || keys.includes('control'),
shift: keys.includes('shift'),
meta: keys.includes('meta'),
mod: keys.includes('mod'),
}
const singleCharKeys = keys.filter((k) => !reservedModifierKeywords.includes(k))
return {
...modifiers,
keys: singleCharKeys,
description,
hotkey,
}
}