Create a manager which can track key states, layouts, and trigger shortcuts. Basically the brains of the operation.
First, all the elements of a manager need to be created.
Then you can create the listeners for the manager with createManagerEventListeners.
Then these listeners can be attached/removed from elements (or the Emulator)
You could make several managers for different areas of your application or use single manager and different contexts to differ between them.
It is easiest to build a manager like so, but the manager also takes in full Keys/Commands/Shortcuts instances, though it's a bit harder to build those first then pass to the manager:
constmanager = createManager( { keys: [ createKey("a", ), // or using a raw key {id:"b"} ], commands: [ createCommand("a"), {name:"b", execute() {....}} ], shortcuts: [ { chain: [["a"]], command:"a" } ] context: createContext({...}), options: { // required evaluateCondition() { ... } } }, { // if constructing from raw entries like above, createKeys/Shortcuts can take additional options ..., keys: {...}, shortcuts:{...} }).unwrap()
constlisteners = createManagerEventListeners(manager) attach(document, listeners) // to remove: dettach(document, listeners)
You will then not need to do much else. It will take care of setting the pressed state of keys, the state of the current chord chain (see Manager._chain), finding a matching shortcut if it exists, and triggering it's command.
The way this works is that when a key is pressed it's added to the current chord in the chain. If the chain matches a shortcut's chain that can be triggered (it must be enabled, it must have a command and a function to execute, and it's condition and it's command's condition must evaluate to true), the command's execute function is triggered with isKeydown = true, see Command.execute.
If there are no shortcuts to trigger but there are "potential shortcuts" that start with the current chain and could trigger and current chord contains any non-modifier keys (see Manager.pressedNonModifierKeys), an empty chord will be added to the chain on the next key pressed and the process repeats.
If there are no shortcuts and no potential shortcuts, the callback will be called with SHORTCUT_ERROR.NO_MATCHING_SHORTCUT and you will probably want to clear the chain. When the chain is cleared (see Manager._chain), the manager will not add/remove keys from the chain until all non-modifier keys are unpressed. Pressed state is still set/tracked though.
The moment a key is released after a shortcut is triggered, the command's execute function is fired again with isKeydown = false.
If Manager.options.updateStateOnAllEvents is not disabled and a mouseeneter listener was created, the manager is able to keep the most accurate state of the modifier keys. See Manager.options.updateStateOnAllEvents for more info.
For a detailed usage example, see the demo.
Other
If you need to emulate keypresses for testing see Emulator.
Create a manager which can track key states, layouts, and trigger shortcuts. Basically the brains of the operation.
First, all the elements of a manager need to be created. Then you can create the listeners for the manager with createManagerEventListeners.
Then these listeners can be attached/removed from elements (or the Emulator)
You could make several managers for different areas of your application or use single manager and different contexts to differ between them.
It is easiest to build a manager like so, but the manager also takes in full Keys/Commands/Shortcuts instances, though it's a bit harder to build those first then pass to the manager:
You will then not need to do much else. It will take care of setting the pressed state of keys, the state of the current chord chain (see Manager._chain), finding a matching shortcut if it exists, and triggering it's command.
The way this works is that when a key is pressed it's added to the current chord in the chain. If the chain matches a shortcut's chain that can be triggered (it must be enabled, it must have a command and a function to execute, and it's condition and it's command's condition must evaluate to true), the command's execute function is triggered with
isKeydown = true
, see Command.execute.If there are no shortcuts to trigger but there are "potential shortcuts" that start with the current chain and could trigger and current chord contains any non-modifier keys (see Manager.pressedNonModifierKeys), an empty chord will be added to the chain on the next key pressed and the process repeats.
If there are no shortcuts and no potential shortcuts, the callback will be called with SHORTCUT_ERROR.NO_MATCHING_SHORTCUT and you will probably want to clear the chain. When the chain is cleared (see Manager._chain), the manager will not add/remove keys from the chain until all non-modifier keys are unpressed. Pressed state is still set/tracked though.
The moment a key is released after a shortcut is triggered, the command's execute function is fired again with
isKeydown = false
.If Manager.options.updateStateOnAllEvents is not disabled and a mouseeneter listener was created, the manager is able to keep the most accurate state of the modifier keys. See Manager.options.updateStateOnAllEvents for more info.
For a detailed usage example, see the demo.
Other