dom-event-key simplifies handling browser keyboard events by providing a convenient way to check if a given key event matches a specified shortcut key.
npm install dom-event-key
import { equalEventKey } from 'dom-event-key';
// Detect shortcut key from keyboard events
window.addEventListener('keydown', (e) => {
if (equalEventKey('Ctrl+k', e)) {
alert('Ctrl+k pressed');
}
});
Generates an array of string representations of keyboard event keys based on the provided state object.
window.addEventListener('keydown', (e) => {
console.log(createEventKeys(e)); // => ex. ['Modifier+k', 'Control+k']
});
Parses the given string representation of a keyboard event key and returns the corresponding state object.
console.log(parseEventKey('Ctrl+k')); // => { ctrlKey: true, key: 'k', ... }
Compares two keyboard event keys or state objects and returns true if they are equal, false otherwise.
window.addEventListener('keydown', (e) => {
console.log(equalEventKey('Ctrl+k', e)); // => true OR false
});