Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Keyboard as an object in the DOM
JavaScript
branch: gh-pages

Merge pull request #3 from ForbesLindesay/patch-1

Use `module.exports` when in CommonJS environments instead of `window` global
latest commit d6ce1f4020
@Benvie authored

README.md

Keyboard

An object that gives the keyboard a first class identity, normalizes keyboard events across browsers, and allows binding to multiple keys.

Example Usage

// create a new Keyboard object that is scoped to a specific DOM element or the window
var kb = new Keyboard(window);

// when both ctrl+a are pressed together
kb.on('ctrl+a', 'activate', function(e){ /***/ });

// when a, then be, then c are pressed in sequence and released
kb.on('a->b->c', 'release', function(e){ /***/ });

// when any key is first pressed (including modifiers such as shift/control/etc.)
kb.on('*', 'activate', function(e){ /***/ })

// when any key is activated, pressed, repeats, released
kb.on('*', function(e){
  console.log(e.action); // one of ['activate', 'repeat', 'release', 'press']
});

API

  • new Keyboard(view)
    view: a Window, Document, or Element which will be listened to

  • keyboard.on(keyCombo, [filter], callback)
    keyCombo: a string that can be any key name or a combination of key names separated by '+' or '->'. The '+' combinator requires the keys be held together, '->' requires they be pressed in a specific sequence.

    filter: optional and must be one

    • activate: when a key is first depressed
    • repeat: when a keydown due to the key being held down
    • release: fires on keyup
    • press: fires on keypress

    callback: called with the keyboardEvent object if the keyCombo was a single key, or an array of keyboardEvent objects if the keyCombo was a combinator.

  • keyboardEvent.action
    What type of action the event originated from. Same as the above filter types.

Something went wrong with that request. Please try again.