@witchcraft/spellcraft 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +579 -0
- package/dist/core/EmulatedEvent.d.ts +10 -0
- package/dist/core/EmulatedEvent.js +19 -0
- package/dist/core/Emulator.d.ts +74 -0
- package/dist/core/Emulator.js +153 -0
- package/dist/core/ShortcutManagerManager.d.ts +103 -0
- package/dist/core/ShortcutManagerManager.js +267 -0
- package/dist/core/addCommand.d.ts +7 -0
- package/dist/core/addCommand.js +7 -0
- package/dist/core/addKey.d.ts +7 -0
- package/dist/core/addKey.js +7 -0
- package/dist/core/addShortcut.d.ts +7 -0
- package/dist/core/addShortcut.js +7 -0
- package/dist/core/attach.d.ts +10 -0
- package/dist/core/attach.js +13 -0
- package/dist/core/createCommand.d.ts +4 -0
- package/dist/core/createCommand.js +11 -0
- package/dist/core/createCommands.d.ts +11 -0
- package/dist/core/createCommands.js +20 -0
- package/dist/core/createCondition.d.ts +7 -0
- package/dist/core/createCondition.js +11 -0
- package/dist/core/createContext.d.ts +2 -0
- package/dist/core/createContext.js +8 -0
- package/dist/core/createKey.d.ts +10 -0
- package/dist/core/createKey.js +35 -0
- package/dist/core/createKeys.d.ts +5 -0
- package/dist/core/createKeys.js +36 -0
- package/dist/core/createManager.d.ts +81 -0
- package/dist/core/createManager.js +83 -0
- package/dist/core/createManagerEventListeners.d.ts +4 -0
- package/dist/core/createManagerEventListeners.js +130 -0
- package/dist/core/createManagerOptions.d.ts +8 -0
- package/dist/core/createManagerOptions.js +17 -0
- package/dist/core/createShortcut.d.ts +8 -0
- package/dist/core/createShortcut.js +24 -0
- package/dist/core/createShortcuts.d.ts +13 -0
- package/dist/core/createShortcuts.js +20 -0
- package/dist/core/detach.d.ts +10 -0
- package/dist/core/detach.js +6 -0
- package/dist/core/index.d.ts +32 -0
- package/dist/core/index.js +32 -0
- package/dist/core/removeCommand.d.ts +7 -0
- package/dist/core/removeCommand.js +7 -0
- package/dist/core/removeKey.d.ts +7 -0
- package/dist/core/removeKey.js +7 -0
- package/dist/core/removeShortcut.d.ts +7 -0
- package/dist/core/removeShortcut.js +7 -0
- package/dist/core/setCommandProp.d.ts +17 -0
- package/dist/core/setCommandProp.js +96 -0
- package/dist/core/setCommandsProp.d.ts +15 -0
- package/dist/core/setCommandsProp.js +94 -0
- package/dist/core/setKeyProp.d.ts +16 -0
- package/dist/core/setKeyProp.js +47 -0
- package/dist/core/setKeysProp.d.ts +10 -0
- package/dist/core/setKeysProp.js +166 -0
- package/dist/core/setManagerProp.d.ts +34 -0
- package/dist/core/setManagerProp.js +54 -0
- package/dist/core/setShortcutProp.d.ts +9 -0
- package/dist/core/setShortcutProp.js +50 -0
- package/dist/core/setShortcutsProp.d.ts +12 -0
- package/dist/core/setShortcutsProp.js +93 -0
- package/dist/defaults/KeysSorter.d.ts +35 -0
- package/dist/defaults/KeysSorter.js +20 -0
- package/dist/defaults/Stringifier.d.ts +66 -0
- package/dist/defaults/Stringifier.js +119 -0
- package/dist/defaults/defaultConditionEquals.d.ts +19 -0
- package/dist/defaults/defaultConditionEquals.js +5 -0
- package/dist/defaults/defaultManagerCallback.d.ts +7 -0
- package/dist/defaults/defaultManagerCallback.js +5 -0
- package/dist/helpers/KnownError.d.ts +8 -0
- package/dist/helpers/KnownError.js +3 -0
- package/dist/helpers/calculateAndSetPositionAndWidth.d.ts +13 -0
- package/dist/helpers/calculateAndSetPositionAndWidth.js +18 -0
- package/dist/helpers/calculateLayoutSize.d.ts +9 -0
- package/dist/helpers/calculateLayoutSize.js +13 -0
- package/dist/helpers/doesShortcutConflict.d.ts +18 -0
- package/dist/helpers/doesShortcutConflict.js +42 -0
- package/dist/helpers/equalsCommand.d.ts +7 -0
- package/dist/helpers/equalsCommand.js +4 -0
- package/dist/helpers/equalsContext.d.ts +7 -0
- package/dist/helpers/equalsContext.js +19 -0
- package/dist/helpers/equalsShortcut.d.ts +9 -0
- package/dist/helpers/equalsShortcut.js +7 -0
- package/dist/helpers/forceClear.d.ts +12 -0
- package/dist/helpers/forceClear.js +16 -0
- package/dist/helpers/forceUpdateNativeKeysState.d.ts +5 -0
- package/dist/helpers/forceUpdateNativeKeysState.js +4 -0
- package/dist/helpers/generateKeyShortcutMap.d.ts +23 -0
- package/dist/helpers/generateKeyShortcutMap.js +64 -0
- package/dist/helpers/getKeyFromEventCode.d.ts +15 -0
- package/dist/helpers/getKeyFromEventCode.js +36 -0
- package/dist/helpers/getKeyFromIdOrVariant.d.ts +4 -0
- package/dist/helpers/getKeyFromIdOrVariant.js +26 -0
- package/dist/helpers/getKeyboardLayoutMap.d.ts +5 -0
- package/dist/helpers/getKeyboardLayoutMap.js +7 -0
- package/dist/helpers/getLabel.d.ts +9 -0
- package/dist/helpers/getLabel.js +6 -0
- package/dist/helpers/getTriggerableShortcut.d.ts +6 -0
- package/dist/helpers/getTriggerableShortcut.js +25 -0
- package/dist/helpers/index.d.ts +28 -0
- package/dist/helpers/index.js +28 -0
- package/dist/helpers/isValidManager.d.ts +3 -0
- package/dist/helpers/isValidManager.js +20 -0
- package/dist/helpers/isValidShortcut.d.ts +3 -0
- package/dist/helpers/isValidShortcut.js +10 -0
- package/dist/helpers/labelWithEvent.d.ts +13 -0
- package/dist/helpers/labelWithEvent.js +20 -0
- package/dist/helpers/labelWithKeyboardMap.d.ts +15 -0
- package/dist/helpers/labelWithKeyboardMap.js +17 -0
- package/dist/helpers/managerToStorableClone.d.ts +12 -0
- package/dist/helpers/managerToStorableClone.js +13 -0
- package/dist/helpers/onKeyboardLayoutChange.d.ts +10 -0
- package/dist/helpers/onKeyboardLayoutChange.js +7 -0
- package/dist/helpers/safeSetManagerChain.d.ts +20 -0
- package/dist/helpers/safeSetManagerChain.js +37 -0
- package/dist/helpers/shortcutCanExecuteIn.d.ts +4 -0
- package/dist/helpers/shortcutCanExecuteIn.js +9 -0
- package/dist/helpers/shortcutIsTriggerableBy.d.ts +2 -0
- package/dist/helpers/shortcutIsTriggerableBy.js +5 -0
- package/dist/helpers/shortcutSwapChords.d.ts +75 -0
- package/dist/helpers/shortcutSwapChords.js +118 -0
- package/dist/helpers/virtualPress.d.ts +13 -0
- package/dist/helpers/virtualPress.js +15 -0
- package/dist/helpers/virtualRelease.d.ts +5 -0
- package/dist/helpers/virtualRelease.js +15 -0
- package/dist/helpers/virtualToggle.d.ts +7 -0
- package/dist/helpers/virtualToggle.js +16 -0
- package/dist/internal/addToChain.d.ts +3 -0
- package/dist/internal/addToChain.js +27 -0
- package/dist/internal/areValidKeys.d.ts +7 -0
- package/dist/internal/areValidKeys.js +28 -0
- package/dist/internal/areValidVariants.d.ts +4 -0
- package/dist/internal/areValidVariants.js +45 -0
- package/dist/internal/checkTrigger.d.ts +2 -0
- package/dist/internal/checkTrigger.js +47 -0
- package/dist/internal/checkUntrigger.d.ts +2 -0
- package/dist/internal/checkUntrigger.js +18 -0
- package/dist/internal/cloneLastChord.d.ts +4 -0
- package/dist/internal/cloneLastChord.js +5 -0
- package/dist/internal/containsPossibleToggleChords.d.ts +13 -0
- package/dist/internal/containsPossibleToggleChords.js +65 -0
- package/dist/internal/errorTextAdd.d.ts +1 -0
- package/dist/internal/errorTextAdd.js +11 -0
- package/dist/internal/errorTextInUse.d.ts +1 -0
- package/dist/internal/errorTextInUse.js +8 -0
- package/dist/internal/errorTextRemove.d.ts +1 -0
- package/dist/internal/errorTextRemove.js +8 -0
- package/dist/internal/getModifierState.d.ts +2 -0
- package/dist/internal/getModifierState.js +7 -0
- package/dist/internal/getPressedKeys.d.ts +7 -0
- package/dist/internal/getPressedKeys.js +18 -0
- package/dist/internal/getPressedModifierKeys.d.ts +2 -0
- package/dist/internal/getPressedModifierKeys.js +10 -0
- package/dist/internal/getPressedNonModifierKeys.d.ts +7 -0
- package/dist/internal/getPressedNonModifierKeys.js +11 -0
- package/dist/internal/inChain.d.ts +5 -0
- package/dist/internal/inChain.js +14 -0
- package/dist/internal/isValidChain.d.ts +10 -0
- package/dist/internal/isValidChain.js +22 -0
- package/dist/internal/isValidChord.d.ts +11 -0
- package/dist/internal/isValidChord.js +59 -0
- package/dist/internal/isValidCommand.d.ts +12 -0
- package/dist/internal/isValidCommand.js +20 -0
- package/dist/internal/keyOrder.d.ts +6 -0
- package/dist/internal/keyOrder.js +14 -0
- package/dist/internal/removeFromChain.d.ts +3 -0
- package/dist/internal/removeFromChain.js +32 -0
- package/dist/internal/safeSetEmulatedToggleState.d.ts +4 -0
- package/dist/internal/safeSetEmulatedToggleState.js +13 -0
- package/dist/internal/setKeysState.d.ts +8 -0
- package/dist/internal/setKeysState.js +42 -0
- package/dist/internal/updateNativeKeysState.d.ts +14 -0
- package/dist/internal/updateNativeKeysState.js +58 -0
- package/dist/layouts/createLayout.d.ts +25 -0
- package/dist/layouts/createLayout.js +221 -0
- package/dist/module.d.mts +13 -0
- package/dist/module.json +9 -0
- package/dist/module.mjs +40 -0
- package/dist/runtime/composables/useLabeledByKeyboardLayoutMap.d.ts +9 -0
- package/dist/runtime/composables/useLabeledByKeyboardLayoutMap.js +19 -0
- package/dist/runtime/composables/usePointerCoords.d.ts +32 -0
- package/dist/runtime/composables/usePointerCoords.js +17 -0
- package/dist/runtime/composables/useShortcutManagerContextCount.d.ts +14 -0
- package/dist/runtime/composables/useShortcutManagerContextCount.js +61 -0
- package/dist/runtime/composables/useShortcutManagerKeysLayout.d.ts +18 -0
- package/dist/runtime/composables/useShortcutManagerKeysLayout.js +22 -0
- package/dist/runtime/composables/useShortcutManagerVirtualPress.d.ts +6 -0
- package/dist/runtime/composables/useShortcutManagerVirtualPress.js +24 -0
- package/dist/runtime/types.d.ts +10 -0
- package/dist/runtime/types.js +1 -0
- package/dist/runtime/utils/shortcutToId.d.ts +5 -0
- package/dist/runtime/utils/shortcutToId.js +6 -0
- package/dist/types/commands.d.ts +113 -0
- package/dist/types/commands.js +0 -0
- package/dist/types/condition.d.ts +29 -0
- package/dist/types/condition.js +0 -0
- package/dist/types/context.d.ts +18 -0
- package/dist/types/context.js +0 -0
- package/dist/types/enums.d.ts +186 -0
- package/dist/types/enums.js +70 -0
- package/dist/types/general.d.ts +92 -0
- package/dist/types/general.js +0 -0
- package/dist/types/index.d.ts +8 -0
- package/dist/types/index.js +8 -0
- package/dist/types/keys.d.ts +332 -0
- package/dist/types/keys.js +0 -0
- package/dist/types/manager.d.ts +249 -0
- package/dist/types/manager.js +0 -0
- package/dist/types/plugins.d.ts +1 -0
- package/dist/types/plugins.js +0 -0
- package/dist/types/shortcuts.d.ts +144 -0
- package/dist/types/shortcuts.js +0 -0
- package/dist/types/utils.d.ts +1 -0
- package/dist/types/utils.js +0 -0
- package/dist/types.d.mts +3 -0
- package/dist/utils/chainContainsSubset.d.ts +27 -0
- package/dist/utils/chainContainsSubset.js +45 -0
- package/dist/utils/cloneChain.d.ts +2 -0
- package/dist/utils/cloneChain.js +11 -0
- package/dist/utils/cloneKey.d.ts +3 -0
- package/dist/utils/cloneKey.js +26 -0
- package/dist/utils/containsKey.d.ts +7 -0
- package/dist/utils/containsKey.js +4 -0
- package/dist/utils/dedupeKeys.d.ts +9 -0
- package/dist/utils/dedupeKeys.js +9 -0
- package/dist/utils/equalsKey.d.ts +12 -0
- package/dist/utils/equalsKey.js +13 -0
- package/dist/utils/equalsKeys.d.ts +24 -0
- package/dist/utils/equalsKeys.js +15 -0
- package/dist/utils/index.d.ts +14 -0
- package/dist/utils/index.js +14 -0
- package/dist/utils/isAnyKey.d.ts +5 -0
- package/dist/utils/isAnyKey.js +5 -0
- package/dist/utils/isMouseKey.d.ts +5 -0
- package/dist/utils/isMouseKey.js +20 -0
- package/dist/utils/isNormalKey.d.ts +5 -0
- package/dist/utils/isNormalKey.js +5 -0
- package/dist/utils/isTriggerKey.d.ts +5 -0
- package/dist/utils/isTriggerKey.js +3 -0
- package/dist/utils/isWheelKey.d.ts +5 -0
- package/dist/utils/isWheelKey.js +11 -0
- package/dist/utils/mapKeys.d.ts +8 -0
- package/dist/utils/mapKeys.js +5 -0
- package/dist/utils/removeKeys.d.ts +7 -0
- package/dist/utils/removeKeys.js +4 -0
- package/package.json +156 -0
- package/src/core/EmulatedEvent.ts +29 -0
- package/src/core/Emulator.ts +185 -0
- package/src/core/ShortcutManagerManager.ts +380 -0
- package/src/core/addCommand.ts +24 -0
- package/src/core/addKey.ts +25 -0
- package/src/core/addShortcut.ts +24 -0
- package/src/core/attach.ts +27 -0
- package/src/core/createCommand.ts +24 -0
- package/src/core/createCommands.ts +45 -0
- package/src/core/createCondition.ts +21 -0
- package/src/core/createContext.ts +14 -0
- package/src/core/createKey.ts +59 -0
- package/src/core/createKeys.ts +68 -0
- package/src/core/createManager.ts +209 -0
- package/src/core/createManagerEventListeners.ts +139 -0
- package/src/core/createManagerOptions.ts +29 -0
- package/src/core/createShortcut.ts +49 -0
- package/src/core/createShortcuts.ts +51 -0
- package/src/core/detach.ts +21 -0
- package/src/core/index.ts +35 -0
- package/src/core/removeCommand.ts +25 -0
- package/src/core/removeKey.ts +25 -0
- package/src/core/removeShortcut.ts +24 -0
- package/src/core/setCommandProp.ts +140 -0
- package/src/core/setCommandsProp.ts +128 -0
- package/src/core/setKeyProp.ts +80 -0
- package/src/core/setKeysProp.ts +205 -0
- package/src/core/setManagerProp.ts +111 -0
- package/src/core/setShortcutProp.ts +89 -0
- package/src/core/setShortcutsProp.ts +124 -0
- package/src/defaults/KeysSorter.ts +55 -0
- package/src/defaults/Stringifier.ts +234 -0
- package/src/defaults/defaultConditionEquals.ts +29 -0
- package/src/defaults/defaultManagerCallback.ts +14 -0
- package/src/helpers/KnownError.ts +13 -0
- package/src/helpers/calculateAndSetPositionAndWidth.ts +30 -0
- package/src/helpers/calculateLayoutSize.ts +22 -0
- package/src/helpers/doesShortcutConflict.ts +72 -0
- package/src/helpers/equalsCommand.ts +18 -0
- package/src/helpers/equalsContext.ts +29 -0
- package/src/helpers/equalsShortcut.ts +34 -0
- package/src/helpers/forceClear.ts +31 -0
- package/src/helpers/forceUpdateNativeKeysState.ts +9 -0
- package/src/helpers/generateKeyShortcutMap.ts +113 -0
- package/src/helpers/getKeyFromEventCode.ts +67 -0
- package/src/helpers/getKeyFromIdOrVariant.ts +33 -0
- package/src/helpers/getKeyboardLayoutMap.ts +13 -0
- package/src/helpers/getLabel.ts +15 -0
- package/src/helpers/getTriggerableShortcut.ts +37 -0
- package/src/helpers/index.ts +30 -0
- package/src/helpers/isValidManager.ts +29 -0
- package/src/helpers/isValidShortcut.ts +20 -0
- package/src/helpers/labelWithEvent.ts +41 -0
- package/src/helpers/labelWithKeyboardMap.ts +37 -0
- package/src/helpers/managerToStorableClone.ts +26 -0
- package/src/helpers/onKeyboardLayoutChange.ts +17 -0
- package/src/helpers/safeSetManagerChain.ts +66 -0
- package/src/helpers/shortcutCanExecuteIn.ts +24 -0
- package/src/helpers/shortcutIsTriggerableBy.ts +15 -0
- package/src/helpers/shortcutSwapChords.ts +240 -0
- package/src/helpers/virtualPress.ts +34 -0
- package/src/helpers/virtualRelease.ts +25 -0
- package/src/helpers/virtualToggle.ts +28 -0
- package/src/internal/addToChain.ts +40 -0
- package/src/internal/areValidKeys.ts +40 -0
- package/src/internal/areValidVariants.ts +59 -0
- package/src/internal/checkTrigger.ts +55 -0
- package/src/internal/checkUntrigger.ts +26 -0
- package/src/internal/cloneLastChord.ts +10 -0
- package/src/internal/containsPossibleToggleChords.ts +91 -0
- package/src/internal/errorTextAdd.ts +18 -0
- package/src/internal/errorTextInUse.ts +10 -0
- package/src/internal/errorTextRemove.ts +10 -0
- package/src/internal/getModifierState.ts +15 -0
- package/src/internal/getPressedKeys.ts +26 -0
- package/src/internal/getPressedModifierKeys.ts +14 -0
- package/src/internal/getPressedNonModifierKeys.ts +19 -0
- package/src/internal/inChain.ts +25 -0
- package/src/internal/isValidChain.ts +42 -0
- package/src/internal/isValidChord.ts +87 -0
- package/src/internal/isValidCommand.ts +35 -0
- package/src/internal/keyOrder.ts +24 -0
- package/src/internal/removeFromChain.ts +46 -0
- package/src/internal/safeSetEmulatedToggleState.ts +23 -0
- package/src/internal/setKeysState.ts +71 -0
- package/src/internal/updateNativeKeysState.ts +84 -0
- package/src/layouts/createLayout.ts +328 -0
- package/src/module.ts +62 -0
- package/src/runtime/composables/useLabeledByKeyboardLayoutMap.ts +28 -0
- package/src/runtime/composables/usePointerCoords.ts +40 -0
- package/src/runtime/composables/useShortcutManagerContextCount.ts +81 -0
- package/src/runtime/composables/useShortcutManagerKeysLayout.ts +42 -0
- package/src/runtime/composables/useShortcutManagerVirtualPress.ts +30 -0
- package/src/runtime/types.ts +10 -0
- package/src/runtime/utils/shortcutToId.ts +14 -0
- package/src/types/commands.ts +148 -0
- package/src/types/condition.ts +35 -0
- package/src/types/context.ts +19 -0
- package/src/types/enums.ts +236 -0
- package/src/types/general.ts +117 -0
- package/src/types/index.ts +10 -0
- package/src/types/keys.ts +385 -0
- package/src/types/manager.ts +374 -0
- package/src/types/plugins.ts +32 -0
- package/src/types/shortcuts.ts +204 -0
- package/src/types/utils.ts +40 -0
- package/src/utils/chainContainsSubset.ts +97 -0
- package/src/utils/cloneChain.ts +13 -0
- package/src/utils/cloneKey.ts +33 -0
- package/src/utils/containsKey.ts +17 -0
- package/src/utils/dedupeKeys.ts +23 -0
- package/src/utils/equalsKey.ts +32 -0
- package/src/utils/equalsKeys.ts +50 -0
- package/src/utils/index.ts +16 -0
- package/src/utils/isAnyKey.ts +12 -0
- package/src/utils/isMouseKey.ts +27 -0
- package/src/utils/isNormalKey.ts +15 -0
- package/src/utils/isTriggerKey.ts +7 -0
- package/src/utils/isWheelKey.ts +18 -0
- package/src/utils/mapKeys.ts +21 -0
- package/src/utils/removeKeys.ts +16 -0
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
import type { Mutable } from "@alanscodelog/utils/types"
|
|
2
|
+
|
|
3
|
+
import type { KEY_SORT_POS, SHORTCUT_ERROR } from "./enums.js"
|
|
4
|
+
import type { PickManager } from "./general.js"
|
|
5
|
+
import type { Manager } from "./manager.js"
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
export type RawKey<T extends string = string> = Mutable<Pick<Key<T>, "id"> & Partial<Omit<Key<T>, "id">>>
|
|
9
|
+
|
|
10
|
+
export interface BaseKey<TId extends string = string> {
|
|
11
|
+
type: "key"
|
|
12
|
+
/**
|
|
13
|
+
* The id used to identify which key was pressed.
|
|
14
|
+
*
|
|
15
|
+
* For keyboard keys, this should be {@link https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code KeyboardEvent.code}.
|
|
16
|
+
*
|
|
17
|
+
* For mouse buttons, this should be {@link https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button MouseEvent.button}, i.e. `0-5`.
|
|
18
|
+
*
|
|
19
|
+
* For scroll wheel up/down, pass the custom `WheelUp` and `WheelDown`.
|
|
20
|
+
*
|
|
21
|
+
* For modifiers, see {@link Keys.variants}. for how to handle them as the same key, otherwise they are handled like different keys.
|
|
22
|
+
*
|
|
23
|
+
* For toggles, pass the key code like normal (e.g. `CapsLock`), see {@link Key.isToggle} for how to implement toggles.
|
|
24
|
+
*
|
|
25
|
+
* Changing the id is not supported. It's recommended you just create a new key if you happen to expose changing key options to users. You can then attempt to change all shortcuts to the new key (note you will have to find the toggles as well if they were created) and report back any errors to users (e.g. changing from/to a modifier can render shortcut chords invalid).
|
|
26
|
+
*/
|
|
27
|
+
readonly id: TId
|
|
28
|
+
/**
|
|
29
|
+
* Whether the key is enabled.
|
|
30
|
+
*
|
|
31
|
+
* If it's disabled, checking if "pressed" can be set to true will return an error. Also the manager will not manage the key's state so it will never be set to pressed (and therefore it will not trigger any shortcuts, even though shortcuts can use disabled keys).
|
|
32
|
+
*
|
|
33
|
+
* Useful for preventing the Meta/Super key from being pressed, but still showing a key.
|
|
34
|
+
*
|
|
35
|
+
* @RequiresSet @OnHookable @CanHookable
|
|
36
|
+
*/
|
|
37
|
+
// todo, shortcuts can have disabled keys but they won't work?
|
|
38
|
+
readonly enabled: boolean
|
|
39
|
+
/**
|
|
40
|
+
* The preferred human readable version of a key. Used when stringifying it.
|
|
41
|
+
*
|
|
42
|
+
* @RequiresSet @OnHookable @CanHookable
|
|
43
|
+
*/
|
|
44
|
+
readonly label: string
|
|
45
|
+
/**
|
|
46
|
+
* Variants are a list of fallback codes that will also trigger a key.
|
|
47
|
+
*
|
|
48
|
+
* For example, without variants, there's no way to have native modifier keys or have a shortcut like `[[Ctrl, A]]` where Ctrl can be either of the right/lefts Ctrl keys. One could create two shortcuts for both keys, but only one key would be considered triggered at a time on the layout.
|
|
49
|
+
*
|
|
50
|
+
* Variants can solve this by allowing us to create a key that's only labeled as `Ctrl`. The `id` can be set to an invalid key code (we still need an id for the {@link Keys} class), preferably one that indicates what's happening (e.g. `VirtualCtrl`). The variants can be set to `["ControlLeft", "ControlRight", "Control"]`. Now you can have shortcuts like `[[VirtualCtrl, A]]` and either control key will trigger them.
|
|
51
|
+
*
|
|
52
|
+
* ```ts
|
|
53
|
+
* const virtualCtrl = new Key("VirtualCtrl" {label: "Ctrl", variants: ["ControlLeft", "ControlRight", "Control"] })
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* If you still need the keys to be labeled or styled different, you can register multiple keys with different invalid ids but the same variants.
|
|
57
|
+
*
|
|
58
|
+
* ```ts
|
|
59
|
+
* // different labels for the layout
|
|
60
|
+
* const virtualCtrl = new Key("VirtualCtrl" {label: "Ctrl Left", variants: ["ControlLeft", "ControlRight", "Control"] })
|
|
61
|
+
* const virtualCtrl2 = new Key("VirtualCtrl2" {label: "Ctrl Right", variants: ["ControlLeft", "ControlRight", "Control"] })
|
|
62
|
+
*
|
|
63
|
+
* // same labels, different sizes
|
|
64
|
+
* const virtualCtrl = new Key("VirtualCtrl"
|
|
65
|
+
* { label: "Ctrl", variants: ["ControlLeft", "ControlRight", "Control"], layout: {width: 1.5} },
|
|
66
|
+
* )
|
|
67
|
+
* const virtualCtrl2 = new Key("VirtualCtrl2"
|
|
68
|
+
* { label: "Ctrl", variants: ["ControlLeft", "ControlRight", "Control"], layout: {width: 2}},
|
|
69
|
+
* )
|
|
70
|
+
*
|
|
71
|
+
* ```
|
|
72
|
+
*
|
|
73
|
+
* They can also be use to treat any set of keys as the exact same keys. This can be useful for allowing users to remap keys only within the application.
|
|
74
|
+
*
|
|
75
|
+
* For example, to use CapsLock as an extra Control key (e.g. `id: Ctrl, variants: ["ControlLeft", "ControlRight", "Control", "Capslock"]`). You could even "remap" it to multiple modifiers, just add "Capslock" to the variants list of those modifiers (e.g. Ctrl, Alt, Shift). This would cause all those keys to be considered pressed when Capslock is pressed.
|
|
76
|
+
*
|
|
77
|
+
*/
|
|
78
|
+
readonly variants: readonly string [] | string[] | undefined
|
|
79
|
+
/**
|
|
80
|
+
* Whether the key should be rendered. See {@link Keys.layout}.
|
|
81
|
+
*
|
|
82
|
+
* Toggle on/off keys are automatically created with this set to false.
|
|
83
|
+
*
|
|
84
|
+
* @RequiresSet @OnHookable @CanHookable
|
|
85
|
+
*/
|
|
86
|
+
readonly render: boolean
|
|
87
|
+
/**
|
|
88
|
+
* The width of the key. See {@link Keys.layout}.
|
|
89
|
+
*
|
|
90
|
+
* @RequiresSet @OnHookable @CanHookable
|
|
91
|
+
*/
|
|
92
|
+
readonly width: number
|
|
93
|
+
/**
|
|
94
|
+
* The height of the key. See {@link Keys.layout}.
|
|
95
|
+
*
|
|
96
|
+
* @RequiresSet @OnHookable @CanHookable
|
|
97
|
+
*/
|
|
98
|
+
readonly height: number
|
|
99
|
+
/**
|
|
100
|
+
* The x position of the key. See {@link Keys.layout}.
|
|
101
|
+
*
|
|
102
|
+
* @RequiresSet @OnHookable @CanHookable
|
|
103
|
+
*/
|
|
104
|
+
readonly x: number
|
|
105
|
+
/**
|
|
106
|
+
* The y position of the key. See {@link Keys.layout}.
|
|
107
|
+
*
|
|
108
|
+
* @RequiresSet @OnHookable @CanHookable
|
|
109
|
+
*/
|
|
110
|
+
readonly y: number
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* A list of css classes the key should be rendered with.
|
|
114
|
+
*
|
|
115
|
+
* @RequiresSet @OnHookable @CanHookable
|
|
116
|
+
*/
|
|
117
|
+
readonly classes: readonly string [] | string[]
|
|
118
|
+
/**
|
|
119
|
+
* Whether the key is a modifier. A modifier can be either `"native"` (event.getModifierState will always be used on all events to get it's true state) or `"emulated"`.
|
|
120
|
+
*
|
|
121
|
+
* For both, this determines in the manager when a chord is considered to have been pressed (when it contains a non-modifer key). See {@link Manager}.
|
|
122
|
+
*
|
|
123
|
+
* ### Notes
|
|
124
|
+
* - event.getModifierState does not check the validity of the key code, and will just return false for keys that don't exist.
|
|
125
|
+
* - You will probably need to specify {@link Key.variants key variants} because, for example, to get the state of the Control keys, you need to pass `Control` not `ControlLeft/Right`
|
|
126
|
+
* - If the modifier is native and the state is seen to change without a key press (i.e. when the element is not in focus), a key release is emulated.
|
|
127
|
+
*/
|
|
128
|
+
readonly isModifier: false | "emulated" | "native"
|
|
129
|
+
/**
|
|
130
|
+
* Wether the key is currently being *held* down.
|
|
131
|
+
*
|
|
132
|
+
* Keys presses can be emulated for testing using the {@link Emulator}.
|
|
133
|
+
*
|
|
134
|
+
* @RequiresSet @OnHookable @Managed
|
|
135
|
+
*/
|
|
136
|
+
readonly pressed: boolean
|
|
137
|
+
/**
|
|
138
|
+
* See {@link Manager.options.updateStateOnAllEvents}. Only has an effect if the key is a modifier or toggle key.
|
|
139
|
+
*/
|
|
140
|
+
updateStateOnAllEvents: boolean
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export interface NonToggleKey<TId extends string = string> extends BaseKey<TId> {
|
|
144
|
+
isToggle: false
|
|
145
|
+
toggleOnId: undefined | never
|
|
146
|
+
toggleOffId: undefined | never
|
|
147
|
+
toggleOnPressed: undefined | never
|
|
148
|
+
toggleOffPressed: undefined | never
|
|
149
|
+
}
|
|
150
|
+
export interface ToggleKey<TId extends string = string> extends BaseKey<TId> {
|
|
151
|
+
/**
|
|
152
|
+
* Whether it's a toggle key. A toggle can be either `"native"` (event.getModifierState will always be used on all events to get it's true state) or `"emulated"` (state starts off false and is toggled with every keydown registered).
|
|
153
|
+
*
|
|
154
|
+
* Toggle keys can be bound to shortcuts by their state. For example:
|
|
155
|
+
* ```ts
|
|
156
|
+
* let on = new Shortcut([keys.ScrollLock.toggle.on], commands.toggle_on_x)
|
|
157
|
+
* let off = new Shortcut([keys.ScrollLock.toggle.off], commands.toggle_off_x)
|
|
158
|
+
* ```
|
|
159
|
+
*
|
|
160
|
+
* ### State
|
|
161
|
+
*
|
|
162
|
+
* With either type of toggle key, we cannot know the initial state without a keypress. The {@link Manager} sets it the first time a key matches, but for native keys you can do this even earlier. **Note that event.getModifierState does not check the validity of the key code, and will just return false for keys that don't exist.**
|
|
163
|
+
*
|
|
164
|
+
* When the {@link Manager} handles a key's pressed state, for toggles, the root key indicates whether the user is actually pressing the key or not just like any other key, and the toggle property indicates the state of the toggles.
|
|
165
|
+
*
|
|
166
|
+
* Note that state is changed for the keys as follows:
|
|
167
|
+
*
|
|
168
|
+
* ```ts
|
|
169
|
+
* // user presses and holds key
|
|
170
|
+
* key.pressed = true
|
|
171
|
+
* key.toggleOnPressed = true
|
|
172
|
+
* key.toggleOffPressed = false
|
|
173
|
+
*
|
|
174
|
+
* // user releases key
|
|
175
|
+
* key.pressed = false
|
|
176
|
+
* key.toggleOnPressed = true
|
|
177
|
+
* key.toggleOffPressed = false
|
|
178
|
+
*
|
|
179
|
+
* // user presses and holds key again
|
|
180
|
+
* key.pressed = true
|
|
181
|
+
* key.toggleOnPressed = false
|
|
182
|
+
* key.toggleOffPressed = true
|
|
183
|
+
*
|
|
184
|
+
* // user releases key
|
|
185
|
+
* key.pressed = false
|
|
186
|
+
* key.toggleOnPressed = false
|
|
187
|
+
* key.toggleOffPressed = true
|
|
188
|
+
*
|
|
189
|
+
* ```
|
|
190
|
+
* ### Notes
|
|
191
|
+
* - If the toggle is native and the state is seen to change without a key press (i.e. when the element is not in focus), a key release is **NOT** emulated.
|
|
192
|
+
*/
|
|
193
|
+
readonly isToggle: "native" | "emulated"
|
|
194
|
+
|
|
195
|
+
readonly toggleOnId: string
|
|
196
|
+
readonly toggleOffId: string
|
|
197
|
+
/**
|
|
198
|
+
* @RequiresSet @OnHookable @Managed
|
|
199
|
+
*/
|
|
200
|
+
readonly toggleOnPressed: boolean
|
|
201
|
+
/**
|
|
202
|
+
* @RequiresSet @OnHookable @Managed
|
|
203
|
+
*/
|
|
204
|
+
readonly toggleOffPressed: boolean
|
|
205
|
+
}
|
|
206
|
+
export type Key<TId extends string = string> = (NonToggleKey<TId> | ToggleKey<TId>)
|
|
207
|
+
|
|
208
|
+
export type KeySortPos = typeof KEY_SORT_POS | Record<keyof typeof KEY_SORT_POS, number>
|
|
209
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
210
|
+
export type IKeysSorter = {
|
|
211
|
+
order: KeySortPos
|
|
212
|
+
sort(keyList: string[], keys: Keys): string[]
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
export interface Keys<
|
|
216
|
+
TEntries extends Record<string, Key> = Record<string, Key>
|
|
217
|
+
> {
|
|
218
|
+
type: "keys"
|
|
219
|
+
/**
|
|
220
|
+
* The key entries.
|
|
221
|
+
*
|
|
222
|
+
* To add/remove entries you should {@link addKey}/{@link removeKey} or {@link setKeysProp} with the synthetic `entries@add/remove` properties.
|
|
223
|
+
*
|
|
224
|
+
* The synthetic properties can be hooked into with {@link Manager.hooks}.
|
|
225
|
+
*
|
|
226
|
+
* @RequiresSet @OnHookable @Managed
|
|
227
|
+
*/
|
|
228
|
+
readonly entries: TEntries
|
|
229
|
+
/**
|
|
230
|
+
* If this is true, {@link calculateLayout} will be used to recalculate the size of the keyboard in key units when any related key properties are changed, or when keys are added or removed.
|
|
231
|
+
*/
|
|
232
|
+
autoManageLayout: boolean
|
|
233
|
+
/**
|
|
234
|
+
* The layout size in pixels. Useful for rendering the layout.
|
|
235
|
+
*
|
|
236
|
+
* {@link calculateLayout} can be used to calculate it. This is semi-automated if {@link Keys.autoManageLayout} is true. See it for details.
|
|
237
|
+
*/
|
|
238
|
+
layout: { y: number, x: number }
|
|
239
|
+
/**
|
|
240
|
+
* Is used to keep track of native toggle keys.
|
|
241
|
+
*
|
|
242
|
+
* @RequiresSet @OnHookable @Managed
|
|
243
|
+
*/
|
|
244
|
+
readonly nativeToggleKeys: string[]
|
|
245
|
+
/**
|
|
246
|
+
* Is used to keep track of native modifier keys.
|
|
247
|
+
*
|
|
248
|
+
* @RequiresSet @OnHookable @Managed
|
|
249
|
+
*/
|
|
250
|
+
readonly nativeModifierKeys: string[]
|
|
251
|
+
/**
|
|
252
|
+
* Is used to keep track of key variants in a record: `Record<VariantName, Key.id[]>`.
|
|
253
|
+
*
|
|
254
|
+
* @RequiresSet @OnHookable @Managed
|
|
255
|
+
*/
|
|
256
|
+
readonly variants: Record<string, Key["id"][]>
|
|
257
|
+
/**
|
|
258
|
+
* Is used to keep track of key toggles: `Record<toggleOn/OffId, Key.id>`.
|
|
259
|
+
*
|
|
260
|
+
* @RequiresSet @OnHookable @Managed
|
|
261
|
+
*/
|
|
262
|
+
readonly toggles: Record<string, Key>
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
type GetKeyHooks<T extends keyof KeySetEntries | keyof KeysSetEntries>
|
|
266
|
+
= T extends CanHookKeyProps
|
|
267
|
+
? Partial<Pick<NonNullable<Manager["hooks"]>, "canSetKeyProp" | "onSetKeyProp">>
|
|
268
|
+
: T extends OnHookKeyProps
|
|
269
|
+
? Partial<Pick<NonNullable<Manager["hooks"]>, "onSetKeyProp">>
|
|
270
|
+
: T extends CanHookKeysProps
|
|
271
|
+
? Partial<Pick<NonNullable<Manager["hooks"]>, "canSetKeysProp" | "onSetKeysProp">>
|
|
272
|
+
: Partial<Pick<NonNullable<Manager["hooks"]>, "onSetKeysProp">>
|
|
273
|
+
|
|
274
|
+
type BaseKeyManager = PickManager<"options", "stringifier"> & Record<any, any>
|
|
275
|
+
type Unmanaged<T extends keyof Key & keyof KeySetEntries> = {
|
|
276
|
+
val: Key[T]
|
|
277
|
+
manager: never
|
|
278
|
+
hooks: GetKeyHooks<T>
|
|
279
|
+
error: never
|
|
280
|
+
}
|
|
281
|
+
export type KeySetEntries = {
|
|
282
|
+
pressed: {
|
|
283
|
+
val: Key["pressed"]
|
|
284
|
+
manager: BaseKeyManager & Pick<Manager, "keys">
|
|
285
|
+
hooks: GetKeyHooks<"pressed">
|
|
286
|
+
error: typeof SHORTCUT_ERROR.CANNOT_SET_WHILE_DISABLED
|
|
287
|
+
}
|
|
288
|
+
toggleOnPressed: {
|
|
289
|
+
val: Key["toggleOnPressed"]
|
|
290
|
+
manager: BaseKeyManager & Pick<Manager, "keys">
|
|
291
|
+
hooks: GetKeyHooks<"toggleOnPressed">
|
|
292
|
+
error: typeof SHORTCUT_ERROR.CANNOT_SET_WHILE_DISABLED
|
|
293
|
+
}
|
|
294
|
+
toggleOffPressed: {
|
|
295
|
+
val: Key["toggleOffPressed"]
|
|
296
|
+
manager: BaseKeyManager & Pick<Manager, "keys">
|
|
297
|
+
hooks: GetKeyHooks<"toggleOffPressed">
|
|
298
|
+
error: typeof SHORTCUT_ERROR.CANNOT_SET_WHILE_DISABLED
|
|
299
|
+
}
|
|
300
|
+
enabled: {
|
|
301
|
+
val: Key["enabled"]
|
|
302
|
+
manager: BaseKeyManager
|
|
303
|
+
hooks: GetKeyHooks<"enabled">
|
|
304
|
+
error: typeof SHORTCUT_ERROR.KEY_IN_USE
|
|
305
|
+
}
|
|
306
|
+
x: Unmanaged<"x">
|
|
307
|
+
y: Unmanaged<"y">
|
|
308
|
+
width: Unmanaged<"width">
|
|
309
|
+
height: Unmanaged<"height">
|
|
310
|
+
render: Unmanaged<"render">
|
|
311
|
+
classes: Unmanaged<"classes">
|
|
312
|
+
label: Unmanaged<"label">
|
|
313
|
+
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
export type OnHookKeyProps
|
|
317
|
+
= | "pressed"
|
|
318
|
+
| "x"
|
|
319
|
+
| "y"
|
|
320
|
+
| "width"
|
|
321
|
+
| "height"
|
|
322
|
+
| "label"
|
|
323
|
+
| "toggleOnPressed"
|
|
324
|
+
| "toggleOffPressed"
|
|
325
|
+
| "enabled"
|
|
326
|
+
| "render"
|
|
327
|
+
| "classes"
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
export type CanHookKeyProps = Exclude<
|
|
331
|
+
OnHookKeyProps,
|
|
332
|
+
"pressed" | "toggleOffPressed" | "toggleOnPressed"
|
|
333
|
+
>
|
|
334
|
+
|
|
335
|
+
export type SyntheticOnHookKeysProps
|
|
336
|
+
= | `entries@${"add" | "remove"}`
|
|
337
|
+
| `variants@${"add" | "remove"}@${string}`
|
|
338
|
+
| `toggles@${"add" | "remove"}@${string}`
|
|
339
|
+
|
|
340
|
+
export type CanHookKeysProps = `entries@${"add" | "remove"}`
|
|
341
|
+
|
|
342
|
+
export type OnHookKeysProps = SyntheticOnHookKeysProps | CanHookKeysProps
|
|
343
|
+
|
|
344
|
+
type BaseKeysManager = PickManager<"options", "stringifier"> & Record<any, any>
|
|
345
|
+
& Pick<Manager, "keys">
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
export type KeysSetEntries
|
|
349
|
+
= Record<`entries@add`, {
|
|
350
|
+
val: Key
|
|
351
|
+
manager: BaseKeysManager
|
|
352
|
+
hooks: GetKeyHooks<`entries@add`>
|
|
353
|
+
error: typeof SHORTCUT_ERROR.DUPLICATE_KEY | typeof SHORTCUT_ERROR.INVALID_VARIANT_PAIR
|
|
354
|
+
}>
|
|
355
|
+
& Record<`entries@remove`, {
|
|
356
|
+
val: Key
|
|
357
|
+
manager: BaseKeysManager & Pick<Manager, "shortcuts" | "commands">
|
|
358
|
+
hooks: GetKeyHooks<`entries@remove`>
|
|
359
|
+
error: typeof SHORTCUT_ERROR.KEY_IN_USE | typeof SHORTCUT_ERROR.MISSING
|
|
360
|
+
}>
|
|
361
|
+
& Record<`toggles@${"add" | "remove"}@${string}`, {
|
|
362
|
+
val: Key
|
|
363
|
+
manager: BaseKeysManager
|
|
364
|
+
hooks: GetKeyHooks<`variants@${"add" | "remove"}@${string}`>
|
|
365
|
+
error: never
|
|
366
|
+
}>
|
|
367
|
+
& Record<`variants@${"add" | "remove"}@${string}`, {
|
|
368
|
+
manager: BaseKeysManager
|
|
369
|
+
val: string
|
|
370
|
+
hooks: GetKeyHooks<`variants@${"add" | "remove"}@${string}`>
|
|
371
|
+
error: never
|
|
372
|
+
}>
|
|
373
|
+
& Record<"nativeModifierKeys" | "nativeToggleKeys", {
|
|
374
|
+
manager: BaseKeysManager
|
|
375
|
+
val: string[]
|
|
376
|
+
hooks: GetKeyHooks<"nativeModifierKeys" | "nativeToggleKeys">
|
|
377
|
+
error: never
|
|
378
|
+
}>
|
|
379
|
+
& Record<"layout", {
|
|
380
|
+
manager: BaseKeysManager
|
|
381
|
+
val: Keys["layout"]
|
|
382
|
+
hooks: GetKeyHooks<"layout">
|
|
383
|
+
error: never
|
|
384
|
+
}>
|
|
385
|
+
|