diff --git a/js/service/KeyboardEventService.js b/js/service/KeyboardEventService.js index 2a79d6d1..b1f96160 100644 --- a/js/service/KeyboardEventService.js +++ b/js/service/KeyboardEventService.js @@ -1,64 +1,53 @@ (function () { var ns = $.namespace("pskl.service"); - ns.KeyboardEventService = function () {}; - - /** - * @private - */ - ns.KeyboardEventService.prototype.KeyboardActions_ = { - - "ctrl" : { - "z" : Events.UNDO, - "y" : Events.REDO, - "x" : Events.CUT, - "c" : Events.COPY, - "v" : Events.PASTE - } - }; - - /** - * @private - */ - ns.KeyboardEventService.prototype.CharCodeToKeyCodeMap_ = { - - 90 : "z", - 89 : "y", - 88 : "x", - 67 : "c", - 86 : "v" - }; - - /** - * @private - */ - ns.KeyboardEventService.prototype.onKeyUp_ = function(evt) { - var isMac = false; - if (navigator.appVersion.indexOf("Mac")!=-1) { - // Welcome in mac world where vowels are consons and meta used instead of ctrl: - isMac = true; - } - - if (isMac ? evt.metaKey : evt.ctrlKey) { - // Get key pressed: - var letter = this.CharCodeToKeyCodeMap_[evt.which]; - if(letter) { - var eventToTrigger = this.KeyboardActions_.ctrl[letter]; - if(eventToTrigger) { - $.publish(eventToTrigger); - - evt.preventDefault(); - return false; - } + ns.KeyboardEventService = function () { + this.keyboardActions_ = { + "ctrl" : { + "z" : Events.UNDO, + "y" : Events.REDO, + "x" : Events.CUT, + "c" : Events.COPY, + "v" : Events.PASTE } - } + }; }; + /** * @public */ ns.KeyboardEventService.prototype.init = function() { $(document.body).keydown($.proxy(this.onKeyUp_, this)); }; - -})(); \ No newline at end of file + + /** + * @private + */ + ns.KeyboardEventService.prototype.onKeyUp_ = function(evt) { + var eventToTrigger; + + // jquery names FTW ... + var keycode = evt.which; + var charkey = pskl.service.keyboard.KeycodeTranslator.toChar(keycode); + + if(charkey) { + if (this.isCtrlKeyPressed_(evt)) { + eventToTrigger = this.keyboardActions_.ctrl[charkey]; + } + } + + if(eventToTrigger) { + $.publish(eventToTrigger); + evt.preventDefault(); + } + }; + + ns.KeyboardEventService.prototype.isCtrlKeyPressed_ = function (evt) { + return this.isMac_() ? evt.metaKey : evt.ctrlKey; + }; + + ns.KeyboardEventService.prototype.isMac_ = function () { + return navigator.appVersion.indexOf("Mac") != -1; + }; +})(); diff --git a/js/service/keyboard/KeyboardEvent.js b/js/service/keyboard/KeyboardEvent.js new file mode 100644 index 00000000..6110f3c0 --- /dev/null +++ b/js/service/keyboard/KeyboardEvent.js @@ -0,0 +1,17 @@ +(function () { + var ns = $.namespace('service.keyboard'); + + ns.KeyboardEvent = function (eventName, args, description) { + this.eventName = eventName; + this.args = args; + this.description = description; + }; + + ns.KeyboardEvent.prototype.fire = function () { + $.publish(this.eventName, this.args); + }; + + ns.KeyboardEvent.prototype.getDescription = function () { + return this.description; + }; +})(); diff --git a/js/service/keyboard/KeycodeTranslator.js b/js/service/keyboard/KeycodeTranslator.js new file mode 100644 index 00000000..e43df52b --- /dev/null +++ b/js/service/keyboard/KeycodeTranslator.js @@ -0,0 +1,19 @@ +(function () { + var specialKeys = {}; + + var ns = $.namespace('service.keyboard'); + + ns.KeycodeTranslator= { + toChar : function (keycode) { + if (keycode >= 48 && keycode <= 57) { + // key is 0-9 + return (keycode - 48) + ""; + } else if (keycode >= 65 && keycode <= 90) { + // key is a-z, we'll use base 36 to get the string representation + return (keycode - 65 + 10).toString(36); + } else { + return specialKeys[keycode]; + } + } + }; +})(); \ No newline at end of file diff --git a/piskel-script-list.js b/piskel-script-list.js index deeb069e..81170315 100644 --- a/piskel-script-list.js +++ b/piskel-script-list.js @@ -62,6 +62,7 @@ exports.scripts = [ // Services "js/service/LocalStorageService.js", "js/service/HistoryService.js", + "js/service/keyboard/KeycodeTranslator.js", "js/service/KeyboardEventService.js", "js/service/ImageUploadService.js",