NxTIC/public_html/js/lib/min/shortcut-manager.js

4 lines
1.0 KiB
JavaScript
Executable File

function strToKeyCode(a){var b=a.toUpperCase().charCodeAt(0);if(1==a.length&&(65<=b&&90>=b||49<=b&&57>=b))return b;switch(a){case "ctrl":return 17;case "maj":return 16;case "alt":return 18;case "tab":return 9;case "left":return 37;case "up":return 38;case "right":return 39;case "down":return 40}return null}function ShortcutManager(){}
ShortcutManager.prototype={pressed:[],shortcuts:[],handlers:[],lastKeyCode:[],append:function(a,b){for(var c=a.toLowerCase().split("+"),d=0;d<c.length;d++)if(c[d]=strToKeyCode(c[d]),null==c[d])return null;c=this.shortcuts.push(c)-1;this.handlers[c]=b;this.lastKeyCode[c]=null},listen:function(){var a=this;window.addEventListener("keydown",function(b){0>a.pressed.indexOf(b.keyCode)&&a.pressed.push(b.keyCode);for(var c=0;c<a.shortcuts.length;c++){for(var d=a.shortcuts[c].length,e=0;e<a.pressed.length;e++)-1<
a.shortcuts[c].indexOf(a.pressed[e])&&--d;if(0==d)a.handlers[c](b)}},!1);window.addEventListener("keyup",function(b){b=a.pressed.indexOf(b.keyCode);a.pressed=-1<b?a.pressed.slice(0,b).concat(a.pressed.slice(b+1)):[]},!1)}};