dom-event-key

1.0.2 • Public • Published

dom-event-key

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.

Install

npm install dom-event-key

Usage

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');
  }
});

API

createEventKeys(state[, options]) => string[]

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']
});

parseEventKey(key[, options]) => state

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', ... }

equalEventKey(keyOrState, otherKeyOrState) => boolean

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
});

Readme

Keywords

none

Package Sidebar

Install

npm i dom-event-key

Weekly Downloads

16

Version

1.0.2

License

MIT

Unpacked Size

21.9 kB

Total Files

6

Last publish

Collaborators

  • ofk