A library to handle hotkeys in browser
https://github.com/doouding/keybinding.git
# โ ๏ธ This project is no longer being maintained.
A typescript library to handle hotkeys in browser.
$ yarn add keybinding
import Keybinding from 'keybinding';
const keybinding = new Keybinding();
keybinding.on('ctrl + a', () => {
console.log('ctrl + a pressed!');
});
Keybinding support function keys, common modifier keys and some special keys. You can use their handy name to bind event.
f1 - f12shiftmeta, windows, command, cmd control, ctrlalt, optionspaceup, down, left, rightbackspace, backentera - z and A - Z0 - 9 `,-, +, [, ], \, ;, ', ,, ., /
### API
**construct options**
%%CODEBLOCK2%%
**on**
Add a hotkey handler.
%%CODEBLOCK3%%
**off**
Remove a hotkey handler.
%%CODEBLOCK4%%
> Scopes are used to control whether handler should be called when the bound hotkey was pressed. See more detail in disable/enable method.
**disable/enable**
disable method will disable the given scope's handlers. All handlers under the given scope will not be called even if the hotkeys they bound was pressed until the enable` method called. /**
* Disable given scope's hotkeys.
* @param scope scope to disable, use 'all' by default
*/
keybinding.disable(scope?: string);
/**
* Enable given scope's hotkeys
* @param scope Scope to enable, use 'all' by default
*/
keybinding.enable(scope?: string);
// After pressing Control + A, handlerA won't be called but handlerB will still be called.
keybind.on('ctrl + a', handlerA);
keybind.on('ctrl + a', handlerB, 'scope2');
keybind.disable('default');
// You can choose to pass no parameter to disable all handler.
// In the following example, both handlerC and handlerD won't be called.
keybind.on('ctrl + a', handlerC, 'scope');
keybind.on('ctrl + a', handlerD, 'scope2');
keybind.disable(); // equal to keybind.disable('all')
// use enable to restore given disabled scope
keybind.enable('scope1') // enable 'scope1'
keybind.enable() // enable all scope, equal to keybind.disable('all')
destroy
Destroy instance.
keybind.destroy();