hotkeys.js 5.1 KB

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