sc-keycodes

0.1.9 • Public • Published

sc-keycodes

A simple package to make working with browser key events a little more convenient.

API

dispatch(evt, callbackTable)

A convenience API for mapping key combinations to callback functions. Takes an event object and a table of key combinations to callback functions.

const keycodes = require('sc-keycodes');

const MyReactComponent = React.createClass({
    render : function() {
        ...
    },
    _handleKeyPress: function (evt) {
        keycodes.dispatch(evt, {
            "SHIFT+UP":         this._moveUp(),
            "SHIFT+DOWN":       this._moveDown(),
            "CTRL+S":           this._saveData(),
            "TAB, SHIFT+RIGHT": this._indent(),
        });
    }
});

KeyCodes

A map of keys by string name to keyCode numeric values.

var keycodes = require('sc-keycodes');

$('#my_div').on('keydown', function (evt) {
    if (evt.keyCode === keycodes.KeyCodes["ENTER"]) {
        doSomething();
    }
});

This is just a simple associative array so key combinations are not supported.

Package Sidebar

Install

npm i sc-keycodes

Weekly Downloads

5

Version

0.1.9

License

MIT

Last publish

Collaborators

  • bcronin