1.0.1 • Published 5 years ago

keypress-handler v1.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

keypress-handler

Handler for javascript keyboard events

NPM JavaScript Style Guide

Info

Handler for keyboard events in JS. Wich DOESN'T have debounce time when triggered.

Legacy event handling

document.addEventListener('keydown', handler);

Legacy event handling

kh.onKey(35, handler);

kh event handling

kh.onKey(35, handler, { hold: true });

kh event handling

Install

npm install --save keypress-handler

Usage

Works with either JS or TS (typings included).

Example below provided in React but you can ues any other framework

kh.run(interval);
  • runs the watcher
    • interval (number, optional, 100 - default) - value in ms stands for interval how often en event is fired.
kh.onKey(keyCode, handler, options);
  • creates watcher for certain key
    • keyCode (number|string, required)- js keycode for a key https://keycode.info/
    • handler (function, required) - handler function for an event
    • options (object, optional) - configuration options for a watcher
      • hold (boolean) - true if needed even firing on hold, false - ONLY 1 event is fired
kh.stop();
  • stops handling events

Example

import * as React from 'react';

import kh from 'keypress-handler';

class MyComponent extends React.Component {
  public componentDidMount() {
    kh.run(75);
    kh.onKey(32, () => console.log('Keypress-handler works!'), {
      hold: true,
    });
  }

  public componentWillUnmount() {
    kh.stop();
  }

  render() {
    return <div />;
  }
}

License

MIT © SergeOlabin