View on GitHub

TappaJS

A Tiny JavaScript Library for Capturing Keyboard Input

Download this project as a .zip file Download this project as a tar.gz file

TappaJS provides a simple way to capture keyboard input, and act accordingly. Instead of capturing key events and placing your functions inside conditions that match event.keyCode to a corresponding integer, you can use TappaJS to pass readable key names to return their corresponding key code and attach a callback function to them.

For instance:

tappa.map('left');
// returns 37

The on() method accepts as many keys as you wish, as a comma-delimited string and attaches the given callback to all of them. For instance:

tappa.on('left, right, up, down', function() {
    console.log('It works!')
});

The state() method accepts an object map of key combination to callbacks and replaces all current listeners with that state. This is helpful for grouping functionality within your application and switching functions on and off at will.

var mystate = {
    'left' : function() {
        console.log('left');
    },
    'right' : function() {
        console.log('right');
    }
}

tappa.state(mystate);

You can clear a specific listener by using the clear() method, or you can clear all listeners by calling this without an argument.

tappa.clear('left'); // listener for left is cleared

tappa.clear(); // all listeners are cleared

You can also map callbacks to "chained" keys using the + sign.

tappa.on('ctrl + z', function() {
    console.log('Undo!');
});
Accepted Key Names

backspace, tab, enter, shift, ctrl, alt, capslock, escape, pageup, pagedown, end, home, left, up, right, down, insert, delete, numlock, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z, leftwindow, rightwindow, leftcommand, rightcommand, num0, num1, num2, num3, num4, num5, num6, num7, num8, num9, multiply, add, subtract, decimal, divide, f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12, semicolon, equals, comma, dash, period, slash, grave, openbracket, backslash, closebracket, quote


Copyright 2012, Gordon Hall - Released under the MIT, BSD, and GPL Licenses.