JAVASCRIPT   28
hotkeys js
Guest on 2nd February 2023 02:22:13 PM


  1. (function (root, factory) {
  2.   if (typeof define === 'function' && define.amd) {
  3.     // AMD. Register as an anonymous module unless amdModuleId is set
  4.     define('simple-hotkeys', ["jquery","simple-module"], function ($, SimpleModule) {
  5.       return (root['hotkeys'] = factory($, SimpleModule));
  6.     });
  7.   } else if (typeof exports === 'object') {
  8.     // Node. Does not work with strict CommonJS, but
  9.     // only CommonJS-like environments that support module.exports,
  10.     // like Node.
  11.     module.exports = factory(require("jquery"),require("simple-module"));
  12.   } else {
  13.     root.simple = root.simple || {};
  14.     root.simple['hotkeys'] = factory(jQuery,SimpleModule);
  15.   }
  16. }(this, function ($, SimpleModule) {
  17.  
  18. var Hotkeys, hotkeys,
  19.   extend = function(child, parent) { for (var key in parent) { if (hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; },
  20.   hasProp = {}.hasOwnProperty;
  21.  
  22. Hotkeys = (function(superClass) {
  23.   extend(Hotkeys, superClass);
  24.  
  25.   function Hotkeys() {
  26.     return Hotkeys.__super__.constructor.apply(this, arguments);
  27.   }
  28.  
  29.   Hotkeys.count = 0;
  30.  
  31.   Hotkeys.keyNameMap = {
  32.     8: "Backspace",
  33.     9: "Tab",
  34.     13: "Enter",
  35.     16: "Shift",
  36.     17: "Control",
  37.     18: "Alt",
  38.     19: "Pause",
  39.     20: "CapsLock",
  40.     27: "Esc",
  41.     32: "Spacebar",
  42.     33: "PageUp",
  43.     34: "PageDown",
  44.     35: "End",
  45.     36: "Home",
  46.     37: "Left",
  47.     38: "Up",
  48.     39: "Right",
  49.     40: "Down",
  50.     45: "Insert",
  51.     46: "Del",
  52.     91: "Meta",
  53.     93: "Meta",
  54.     48: "0",
  55.     49: "1",
  56.     50: "2",
  57.     51: "3",
  58.     52: "4",
  59.     53: "5",
  60.     54: "6",
  61.     55: "7",
  62.     56: "8",
  63.     57: "9",
  64.     65: "A",
  65.     66: "B",
  66.     67: "C",
  67.     68: "D",
  68.     69: "E",
  69.     70: "F",
  70.     71: "G",
  71.     72: "H",
  72.     73: "I",
  73.     74: "J",
  74.     75: "K",
  75.     76: "L",
  76.     77: "M",
  77.     78: "N",
  78.     79: "O",
  79.     80: "P",
  80.     81: "Q",
  81.     82: "R",
  82.     83: "S",
  83.     84: "T",
  84.     85: "U",
  85.     86: "V",
  86.     87: "W",
  87.     88: "X",
  88.     89: "Y",
  89.     90: "Z",
  90.     96: "0",
  91.     97: "1",
  92.     98: "2",
  93.     99: "3",
  94.     100: "4",
  95.     101: "5",
  96.     102: "6",
  97.     103: "7",
  98.     104: "8",
  99.     105: "9",
  100.     106: "Multiply",
  101.     107: "Add",
  102.     109: "Subtract",
  103.     110: "Decimal",
  104.     111: "Divide",
  105.     112: "F1",
  106.     113: "F2",
  107.     114: "F3",
  108.     115: "F4",
  109.     116: "F5",
  110.     117: "F6",
  111.     118: "F7",
  112.     119: "F8",
  113.     120: "F9",
  114.     121: "F10",
  115.     122: "F11",
  116.     123: "F12",
  117.     124: "F13",
  118.     125: "F14",
  119.     126: "F15",
  120.     127: "F16",
  121.     128: "F17",
  122.     129: "F18",
  123.     130: "F19",
  124.     131: "F20",
  125.     132: "F21",
  126.     133: "F22",
  127.     134: "F23",
  128.     135: "F24",
  129.     59: ";",
  130.     61: "=",
  131.     186: ";",
  132.     187: "=",
  133.     188: ",",
  134.     190: ".",
  135.     191: "/",
  136.     192: "`",
  137.     219: "[",
  138.     220: "\\",
  139.     221: "]",
  140.     222: "'"
  141.   };
  142.  
  143.   Hotkeys.aliases = {
  144.     "escape": "esc",
  145.     "delete": "del",
  146.     "return": "enter",
  147.     "ctrl": "control",
  148.     "space": "spacebar",
  149.     "ins": "insert",
  150.     "cmd": "meta",
  151.     "command": "meta",
  152.     "wins": "meta",
  153.     "windows": "meta"
  154.   };
  155.  
  156.   Hotkeys.normalize = function(shortcut) {
  157.     var i, j, key, keyname, keys, len;
  158.     keys = shortcut.toLowerCase().replace(/\s+/gi, "").split("+");
  159.     for (i = j = 0, len = keys.length; j < len; i = ++j) {
  160.       key = keys[i];
  161.       keys[i] = this.aliases[key] || key;
  162.     }
  163.     keyname = keys.pop();
  164.     keys.sort().push(keyname);
  165.     return keys.join("_");
  166.   };
  167.  
  168.   Hotkeys.prototype.opts = {
  169.     el: document
  170.   };
  171.  
  172.   Hotkeys.prototype._init = function() {
  173.     this.id = ++this.constructor.count;
  174.     this._map = {};
  175.     this._delegate = typeof this.opts.el === "string" ? document : this.opts.el;
  176.     return $(this._delegate).on("keydown.simple-hotkeys-" + this.id, this.opts.el, (function(_this) {
  177.       return function(e) {
  178.         var ref;
  179.         return (ref = _this._getHander(e)) != null ? ref.call(_this, e) : void 0;
  180.       };
  181.     })(this));
  182.   };
  183.  
  184.   Hotkeys.prototype._getHander = function(e) {
  185.     var keyname, shortcut;
  186.     if (!(keyname = this.constructor.keyNameMap[e.which])) {
  187.       return;
  188.     }
  189.     shortcut = "";
  190.     if (e.altKey) {
  191.       shortcut += "alt_";
  192.     }
  193.     if (e.ctrlKey) {
  194.       shortcut += "control_";
  195.     }
  196.     if (e.metaKey) {
  197.       shortcut += "meta_";
  198.     }
  199.     if (e.shiftKey) {
  200.       shortcut += "shift_";
  201.     }
  202.     shortcut += keyname.toLowerCase();
  203.     return this._map[shortcut];
  204.   };
  205.  
  206.   Hotkeys.prototype.respondTo = function(subject) {
  207.     if (typeof subject === 'string') {
  208.       return this._map[this.constructor.normalize(subject)] != null;
  209.     } else {
  210.       return this._getHander(subject) != null;
  211.     }
  212.   };
  213.  
  214.   Hotkeys.prototype.add = function(shortcut, handler) {
  215.     this._map[this.constructor.normalize(shortcut)] = handler;
  216.     return this;
  217.   };
  218.  
  219.   Hotkeys.prototype.remove = function(shortcut) {
  220.     delete this._map[this.constructor.normalize(shortcut)];
  221.     return this;
  222.   };
  223.  
  224.   Hotkeys.prototype.destroy = function() {
  225.     $(this._delegate).off(".simple-hotkeys-" + this.id);
  226.     this._map = {};
  227.     return this;
  228.   };
  229.  
  230.   return Hotkeys;
  231.  
  232. })(SimpleModule);
  233.  
  234. hotkeys = function(opts) {
  235.   return new Hotkeys(opts);
  236. };
  237.  
  238. return hotkeys;
  239.  
  240. }));

Raw Paste

Login or Register to edit or fork this paste. It's free.