Shortcut is a little library to help make shortcut handling easier and more flexible.
Normally, to handle shortcuts, the go-to method is to just check event
like so:
document.addEventListener("keydown", event => {
if (event.ctrlKey && event.key == "k") {
// Code goes here
}
})
But this method has some flaws:
- You can't make a combination of normal keys, like F2 + K, or / + 1.
- You can accidentally allow extra keys, like Shift or Alt, if you don't make sure that they're false.
Using Shortcut is easy once you understand what values you can pass to it's prompt object:
alt
, control
, meta
, shift
: These are the special keys in a normal KeyboardEvent.
true
: This key must be pressed for the function to fire.null
orundefined
: It doesn't matter if this key is pressed down or not.- When
implicit
is not set totrue
,undefined
is regarded asfalse
.
- When
false
: This key must NOT be pressed for the callback function to fire.
keys
: An array of normal keys that are required. Like event.key
, except case-insensitive.
implicit
: If true, special keys that are undefined
will be allowed.
repeat
: When true
, the shortcut can repeat when held down.
Here's an example of a Shortcut that handles Ctrl + K:
new Shortcut({ control: true, keys: ["k"] }, () => {
// Code goes here
})
Another example that fires when A is pressed once:
new Shortcut({ keys: ["a"], implicit: true }, event => {
// Code goes here
})
One more example for F2 + E:
new Shortcut({ keys: ["F2", "E"] }, event => {
// Code goes here
})
You can easily kill a Shortcut by calling it's kill()
method.
This library was made by Aetinx for Latapals.