modern-hotkeys is a library that makes it easy to create hotkeys for your applications. It uses modern APIs and supports multiple combinations of keyboard layouts. Furthermore, its customizable event filter and scope system allows you to easily add hotkeys to any part of your application. You can create a smooth and reliable hotkey experience for your users. It has no dependencies.
yarn add modern-hotkeys
or
npm i modern-hotkeys
import { hotkeys } from 'modern-hotkeys';
hotkeys('shift + a', (event, handler) => {
// it always prevents default
console.log('You pressed shift + a!');
});
- Always prevent default all events
- Set an order in your shortcuts
- Stop propagation if you have multiple shorcuts with the same keys
- Use modern APIs
- Prepared to work with different keyboard layouts such as Spanish (by default it's enUS)
- Create your own instance
Modern HotKeys understands the following modifiers: ⇧
, shift
, option
, ⌥
, alt
, ctrl
, control
, ⌃
, command
, and ⌘
.
To see all supported keys check out the layouts:
If you need another layout you can create using the interface KeyboardLayout
.
import { KeyboardLayout, hotkeys } from 'modern-hotkeys';
const MyLayout: KeyboardLayout = {
Backspace: { value: 'backspace' },
Tab: { value: 'tab' },
NumLock: { value: 'NumLock' },
...
};
hotkeys.setKeyboardLayout(MyLayout);
We export a global instance but you can create your own one.
Using the global instance
import { hotkeys } from 'modern-hotkeys';
hotkeys('command + s, ctrl + s', () => {
alert('saving!');
});
Creating own instance
import { createHotkeys } from 'modern-hotkeys';
const instance = createHotkeys({ element: document.body });
instance.hotkeys('command + s, ctrl + s', () => {
alert('saving!');
});
Creates a new instance of hotkeys
with the specified options.
element
(HTMLElement
): Element to bind events to.keyboard
(KeyboardLayout
): Keyboard layout to use. Default isLayouts['en-us']
.autoWatchKeys
(boolean
): Whether to automatically watch for keys. Default istrue
.watchCaps
(boolean
): Whether to watch for theCapsLock
key. Default isfalse
.autoPreventDefault
(boolean
): Whether to automatically prevent default. Default istrue
.
Scopes allow you to isolate your shortcuts and only execute depending on the scope that your app is running.
import { hotkeys } from 'modern-hotkeys';
hotkeys('command + s, ctrl + s', 'file', () => {
alert('saving file! you are in the scope ' + hotkeys.getScope());
});
<button onClick={() => hotkeys.setScope('file')}>set file scope</button>;
// or if you have your instance
import { createHotkeys } from 'modern-hotkeys';
const instance = createHotkeys({ element: document.body });
instance.hotkeys('command + s, ctr + s', 'file', () => {
alert('saving file! you are in the scope ' + instance.getScope());
});
<button onClick={() => instance.setScope('file')}>set file scope</button>;
Returns a Set
with all keys down.
import { hotkeys } from 'modern-hotkeys';
hotkeys('a, b, c', () => {
console.log(hotkeys.getKeysDown().has('a'));
console.log(hotkeys.getKeysDown().has('b'));
console.log(hotkeys.getKeysDown().has('c'));
});
Return a boolean to know if a key is pressed
import { hotkeys } from 'modern-hotkeys';
hotkeys('a, b, c', () => {
console.log(hotkeys.isPressed('a'));
console.log(hotkeys.isPressed('b'));
console.log(hotkeys.isPressed('c'));
});
You can get the default layouts exported by the library with Layouts
or create your own one. To change the layout use the setKeyboardLayout
.
import { hotkeys, Layouts } from 'modern-hotkeys';
hotkeys(...);
hotkeys.setKeyboardLayout(Layouts['es-la']);
// or if you have your instance
import { createHotkeys, Layouts } from 'modern-hotkeys';
const instance = createHotkeys({ element: document.body });
instance.hotkeys(...);
instance.setKeyboardLayout(Layouts['es-la']);
Unbind a shortcut or all shortcuts.
import { hotkeys } from 'modern-hotkeys';
// unbind defined key
hotkeys.unbind('ctrl + s');
// unbind defined key and scope
hotkeys.unbind('command + s', 'my-scope-files');
// unbind all
hotkeys.unbind();
// or if you have your instance
import { createHotkeys } from 'modern-hotkeys';
const instance = createHotkeys({ element: document.body });
// unbind defined key
instance.unbind('ctrl + s');
// unbind defined key and scope
instance.unbind('command + s', 'my-scope-files');
// unbind all
instance.unbind();
Useful to filter input events and not fire shortcut when the user is writing on an input.
import { hotkeys } from 'modern-hotkeys';
// filter inputs
hotkeys.setEventFilter((e) => e.target?.tagName !== 'INPUT');
// allow all events
hotkeys.setEventFilter(() => true);
// or if you have your instance
import { createHotkeys } from 'modern-hotkeys';
const instance = createHotkeys({ element: document.body });
// filter inputs
instance.setEventFilter((e) => e.target?.tagName !== 'INPUT');
// allow all events
instance.setEventFilter(() => true);
Show debug logs
import { createHotkeys } from 'modern-hotkeys';
const instance = createHotkeys({ element: document.body });
instance.setVerbose(true);
instance.hotkeys('shift + c', () => {
...
});
import { hotkeys } from 'modern-hotkeys';
hotkeys(
'escape',
(e, h) => {
h.stopPropagation();
alert('cancelling 1!');
},
{ order: 0 },
);
hotkeys(
'escape',
() => {
// it won't be triggered
alert('cancelling 2!');
},
{ order: 1 },
);
The third argument of hotkeys
could be an object with options.
You can scale the priority of a shortcut using negative numbers.
import { hotkeys } from 'modern-hotkeys';
hotkeys('escape', () => {
alert('cancelling 2!');
});
hotkeys(
'escape',
(e, h) => {
alert('cancelling 1!');
},
{ order: -1000 },
);
You can define what event will trigger your shortcut
import { hotkeys } from 'modern-hotkeys';
hotkeys(
'control + s',
() => {
console.log('triggered on keydown');
},
{ event: 'keydown' },
);
hotkeys(
'control + s',
() => {
console.log('triggered on keyup');
},
{ event: 'keyup' },
);
Define the scope of the shortcut
import { hotkeys } from 'modern-hotkeys';
hotkeys(
'control + s',
() => {
console.log('triggered on scope myscope');
},
{ scope: 'myscope' },
);
MIT