hotkeys.js 5.1 KB

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