piskel/js/KeyManager.js

50 lines
1,015 B
JavaScript
Raw Normal View History

(function () {
var ns = $.namespace("pskl");
ns.KeyManager = function () {
2012-09-12 19:32:18 +00:00
$(document.body).keydown($.proxy(this.onKeyUp_, this));
};
// Kind of object that make you want to stop front-end _engineering_:
ns.KeyManager.prototype.CharCodeToKeyCodeMap = {
90 : "z",
89 : "y",
88 : "x",
67 : "c",
86 : "v"
};
ns.KeyManager.prototype.KeyboardActions = {
"ctrl" : {
"z" : Events.UNDO,
"y" : Events.REDO,
"x" : Events.CUT,
"c" : Events.COPY,
"v" : Events.PASTE
}
};
ns.KeyManager.prototype.onKeyUp_ = function(evt) {
2012-09-12 19:32:18 +00:00
var isMac = false;
if (navigator.appVersion.indexOf("Mac")!=-1) {
// Welcome in mac world where vowels are consons and meta used instead of ctrl:
2012-09-12 19:32:18 +00:00
isMac = true;
}
if (isMac ? evt.metaKey : evt.ctrlKey) {
// Get key pressed:
2012-09-14 18:12:21 +00:00
var letter = this.CharCodeToKeyCodeMap[evt.which];
if(letter) {
2012-09-14 18:12:21 +00:00
var eventToTrigger = this.KeyboardActions.ctrl[letter];
if(eventToTrigger) {
$.publish(eventToTrigger);
}
2012-09-14 18:12:21 +00:00
}
}
};
2012-09-15 00:24:06 +00:00
})();