2012-09-12 14:01:47 +04:00
|
|
|
(function () {
|
2012-09-16 15:27:00 +04:00
|
|
|
var ns = $.namespace("pskl.service");
|
2012-09-12 14:01:47 +04:00
|
|
|
|
2012-09-16 15:19:31 +04:00
|
|
|
ns.KeyboardEventService = function () {};
|
2012-09-12 14:01:47 +04:00
|
|
|
|
2012-09-16 15:11:10 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2012-09-16 15:19:31 +04:00
|
|
|
ns.KeyboardEventService.prototype.KeyboardActions_ = {
|
2012-09-12 14:01:47 +04:00
|
|
|
|
|
|
|
"ctrl" : {
|
|
|
|
"z" : Events.UNDO,
|
|
|
|
"y" : Events.REDO,
|
|
|
|
"x" : Events.CUT,
|
|
|
|
"c" : Events.COPY,
|
|
|
|
"v" : Events.PASTE
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-09-16 15:11:10 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2012-09-16 15:19:31 +04:00
|
|
|
ns.KeyboardEventService.prototype.CharCodeToKeyCodeMap_ = {
|
2012-09-16 15:11:10 +04:00
|
|
|
|
|
|
|
90 : "z",
|
|
|
|
89 : "y",
|
|
|
|
88 : "x",
|
|
|
|
67 : "c",
|
|
|
|
86 : "v"
|
|
|
|
};
|
2012-09-12 14:01:47 +04:00
|
|
|
|
2012-09-16 15:11:10 +04:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2012-09-16 15:19:31 +04:00
|
|
|
ns.KeyboardEventService.prototype.onKeyUp_ = function(evt) {
|
2012-09-12 23:32:18 +04:00
|
|
|
var isMac = false;
|
|
|
|
if (navigator.appVersion.indexOf("Mac")!=-1) {
|
2012-09-12 14:01:47 +04:00
|
|
|
// Welcome in mac world where vowels are consons and meta used instead of ctrl:
|
2012-09-12 23:32:18 +04:00
|
|
|
isMac = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isMac ? evt.metaKey : evt.ctrlKey) {
|
2012-09-12 14:01:47 +04:00
|
|
|
// Get key pressed:
|
2012-09-16 15:11:10 +04:00
|
|
|
var letter = this.CharCodeToKeyCodeMap_[evt.which];
|
2012-09-12 14:01:47 +04:00
|
|
|
if(letter) {
|
2012-09-16 15:11:10 +04:00
|
|
|
var eventToTrigger = this.KeyboardActions_.ctrl[letter];
|
2012-09-12 14:01:47 +04:00
|
|
|
if(eventToTrigger) {
|
|
|
|
$.publish(eventToTrigger);
|
2012-09-16 15:11:26 +04:00
|
|
|
|
|
|
|
evt.preventDefault();
|
|
|
|
return false;
|
2012-09-12 14:01:47 +04:00
|
|
|
}
|
2012-09-14 22:12:21 +04:00
|
|
|
}
|
2012-09-12 14:01:47 +04:00
|
|
|
}
|
|
|
|
};
|
2012-09-16 15:19:31 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
*/
|
|
|
|
ns.KeyboardEventService.prototype.init = function() {
|
|
|
|
$(document.body).keydown($.proxy(this.onKeyUp_, this));
|
|
|
|
};
|
2012-09-15 04:24:06 +04:00
|
|
|
|
2012-09-12 14:01:47 +04:00
|
|
|
})();
|