Hotstuff
Small Javascript Library to add hotkeys to an element with no dependencies
Installation
npm install hotstuff
Usage
The preferred method of pulling HotStuff into your project is using NPM and a CommonJS module system.
const HotStuff = ;
You can then create a new HotStuff object using one of the methods below. Note: HotStuff attaches events using addEventListener on both the keydown and keyup events so the object you pass to HotStuff must support those events.
const element = document;const hotStuff = element; // You can also attach to the window const hotStuff = window;// by default HotStuff attaches itself to the window object if there is oneconst hotStuff = ;
From this point you can add hotkeys using the new hotStuff
object using the hotStuff.addListener
method. Both functions are optional (so if you want to just add a key up function, you can by passing null as the second argument)
hotStuff;// examplehotStuff;
As well as the original KeyboardEvent object, each function is also passed a persistant shared store (a basic object). This is shared between each pair of functions and persists between triggers. This allows data to be passed between functions without polluting other scopes. There's also a third argument that passes in the keys that were pressed
hotStuff;
You can attach multiple key bindings to the same functions (and stores) by passing an array of key presses as the first argument of addListener.
hotStuff;
There may be times where you don't want your keyup function to trigger, whilst this is possible to achieve with the store, you can also achieve this by making your keydown function return false.
// sidenote: the arrow keys can either be referred to by using// arrow<direction> or just <direction>hotStuff;
Finally, if you want to attach a key listener to all keys, you can by only passing the functions to the method
hotStuff
Examples
Debounce
// console.log will only be triggered once per keypresshotStuff; // example of a debounce with a 1 second time limit between keypresseshotStuff;