🔥 Litkey makes keyboard shortcuts simple and enjoyable.
# Using npm
npm install litkey
# Using yarn
yarn add litkey
import litkey from 'litkey';
// Add a global keyboard shortcut
litkey('mod+k', () => {
// do something
});
// Add a keyboard shortcut to a specific element
litkey('mod+k', () => {
// do something
}, myElement);
import { useShortcut } from 'litkey';
let Component = () => {
let [clicked, setClicked] = useState(false);
useShortcut('mod+a', () => {
setClicked(true);
});
// You can also specify hook dependencies which will
// get passed on to the underlying useEffect
useShortcut('mod+k', () => {
setClicked(true);
}, [/* react hook dependency list */]);
// Using the fourth parameter, you can specify a
// specific DOM element, in which the keyboard
// shortcut will be fired
useShortcut('mod+k', () => {
setClicked(true);
}, [], myElement);
return (
<p>{ clicked ? 'clicked' : 'not clicked' }</p>
);
};
The litkey
function is the default export of litkey.
shortcut
is a string
or an array of strings
, which specify the key combinations which will fire the callback.
The handler
is a callback function which will be called if the keyboard shortcut is pressed.
It receives the KeyboardEvent
as its first parameter.
The context is optional and can be used to specify the HTMLElement
, in which litkey will listen for keyboard shortcuts.
shortcut
is a string
or an array of strings
, which specify the key combinations which will fire the callback.
The handler
is a callback function which will be called if the keyboard shortcut is pressed.
It receives the KeyboardEvent
as its first parameter.
dependencies
is an optional array, which will be passed on directly to useEffect
to serve as React hook dependencies.
context
is optional and can be used to specify the HTMLElement
, in which litkey will listen for keyboard shortcuts.
MIT © Tobias Herber