backbone.keyboard

Keyboard event support for Backbone Views


License
MIT
Install
bower install backbone.keyboard

Documentation

 _______ _______ _______ _______ _______ _______ _______ _______
|\     /|\     /|\     /|\     /|\     /|\     /|\     /|\     /|
| +---+ | +---+ | +---+ | +---+ | +---+ | +---+ | +---+ | +---+ |
| |   | | |   | | |   | | |   | | |   | | |   | | |   | | |   | |
| |b  | | |a  | | |c  | | |k  | | |b  | | |o  | | |n  | | |e. | |
| +---+ | +---+ | +---+ | +---+ | +---+ | +---+ | +---+ | +---+ |
|/_____\|/_____\|/_____\|/_____\|/_____\|/_____\|/_____\|/_____\|
 _______ _______ _______ _______ _______ _______ _______ _______
|\     /|\     /|\     /|\     /|\     /|\     /|\     /|\     /|
| +---+ | +---+ | +---+ | +---+ | +---+ | +---+ | +---+ | +---+ |
| |   | | |   | | |   | | |   | | |   | | |   | | |   | | |   | |
| |k  | | |e  | | |y  | | |b  | | |o  | | |a  | | |r  | | |d  | |
| +---+ | +---+ | +---+ | +---+ | +---+ | +---+ | +---+ | +---+ |
|/_____\|/_____\|/_____\|/_____\|/_____\|/_____\|/_____\|/_____\|

Keyboard event support for Backbone Views

Based on backbone.keys © 2012 Raymond Julin, Keyteq AS.

This plugin differs from backbone.keys by allowing you to capture any combination of pressed keys and sequences, and associate multiple events with a method in a single line, i.e. ('a+b c, d e f', 'yourMethod'), as well as convenient key combination aliases for every letter and number ('letters' and 'numbers').

backbone.keyboard also differs from backbone.keys's implementation by allowing it to be used in your application's views selectively as a mixin, i.e. YourView = _.extend(Backbone.View, backbone.keyboard, ...)

Example

Backbone.View = _.extend(Backbone.View, backbone.keyboard);

KeyboardExampleView = Backbone.View.extend({
  keys: {
    'a b c': 'ABCpressed',
    'a+b+c': 'ABCheld',
    'a+b+c d': 'ABCheldThenD',
    'a+b+c, d+e': 'eitherABCorDE',
    'letters': 'letterPressed'
  },

  initialize: function() {
    ...
  },

  someMethod: function() {
    ...
  },

  ABCpressed: function() {
    // A, B and C keys were pressed one after the other
  },

  ABCheld: function() {
    // A, B and C keys were held down
  },

  ABCheldThenD: function() {
    // A, B and C keys were held down, then the D key was pressed
  },

  eitherABCorDE: function() {
    // either A, B and C were held down or D and E
  },

  letterPressed: function() {
    // a letter was pressed
  },

  bindFKey: function() {
    // bind a key to a method:
    this.keyOn('f', this.someMethod);
  },

  unbindFKey: function() {
    // unbind a key's methods:
    this.keyOff('f');

    // or unbind a key's specific method:
    this.keyOff('f', this.someMethod);
  }

  ...
});

This plugin was written for Lucas Sharp's website.
backbone.keyboard may be freely distributed under the MIT license.