mirror of
https://github.com/pcottle/learnGitBranching.git
synced 2025-06-28 00:40:07 +02:00
keyboard events legit
This commit is contained in:
parent
d6a7a11337
commit
93f6f3c6ca
3 changed files with 193 additions and 21 deletions
45
src/js/util/keyboard.js
Normal file
45
src/js/util/keyboard.js
Normal file
|
@ -0,0 +1,45 @@
|
|||
var _ = require('underscore');
|
||||
var Backbone = require('backbone');
|
||||
|
||||
function KeyboardListener(options) {
|
||||
this.events = options.events || _.clone(Backbone.Events);
|
||||
this.aliasMap = options.aliasMap || {};
|
||||
|
||||
this.keyMap = {
|
||||
37: 'left',
|
||||
38: 'up',
|
||||
39: 'right',
|
||||
40: 'down',
|
||||
27: 'esc',
|
||||
13: 'enter'
|
||||
};
|
||||
this.keydownListener = _.bind(this.keydown, this);
|
||||
|
||||
this.listen();
|
||||
}
|
||||
|
||||
KeyboardListener.prototype.listen = function() {
|
||||
$(document).bind('keydown', this.keydownListener);
|
||||
};
|
||||
|
||||
KeyboardListener.prototype.mute = function() {
|
||||
$(document).unbind('keydown', this.keydownListener);
|
||||
};
|
||||
|
||||
KeyboardListener.prototype.keydown = function(e) {
|
||||
var which = e.which;
|
||||
console.log('key which', which);
|
||||
|
||||
if (this.keyMap[which] === undefined) {
|
||||
return;
|
||||
}
|
||||
this.fireEvent(this.keyMap[which]);
|
||||
};
|
||||
|
||||
KeyboardListener.prototype.fireEvent = function(eventName) {
|
||||
eventName = this.aliasMap[eventName] || eventName;
|
||||
this.events.trigger(eventName);
|
||||
};
|
||||
|
||||
exports.KeyboardListener = KeyboardListener;
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue