is-hotkey.umd.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. (function (global, factory) {
  2. if (typeof define === "function" && define.amd) {
  3. define(['exports'], factory);
  4. } else if (typeof exports !== "undefined") {
  5. factory(exports);
  6. } else {
  7. var mod = {
  8. exports: {}
  9. };
  10. factory(mod.exports);
  11. global.index = mod.exports;
  12. }
  13. })(this, function (exports) {
  14. 'use strict';
  15. Object.defineProperty(exports, "__esModule", {
  16. value: true
  17. });
  18. /**
  19. * Constants.
  20. */
  21. var IS_MAC = typeof window != 'undefined' && /Mac|iPod|iPhone|iPad/.test(window.navigator.platform);
  22. var MODIFIERS = {
  23. alt: 'altKey',
  24. control: 'ctrlKey',
  25. meta: 'metaKey',
  26. shift: 'shiftKey'
  27. };
  28. var ALIASES = {
  29. add: '+',
  30. break: 'pause',
  31. cmd: 'meta',
  32. command: 'meta',
  33. ctl: 'control',
  34. ctrl: 'control',
  35. del: 'delete',
  36. down: 'arrowdown',
  37. esc: 'escape',
  38. ins: 'insert',
  39. left: 'arrowleft',
  40. mod: IS_MAC ? 'meta' : 'control',
  41. opt: 'alt',
  42. option: 'alt',
  43. return: 'enter',
  44. right: 'arrowright',
  45. space: ' ',
  46. spacebar: ' ',
  47. up: 'arrowup',
  48. win: 'meta',
  49. windows: 'meta'
  50. };
  51. var CODES = {
  52. backspace: 8,
  53. tab: 9,
  54. enter: 13,
  55. shift: 16,
  56. control: 17,
  57. alt: 18,
  58. pause: 19,
  59. capslock: 20,
  60. escape: 27,
  61. ' ': 32,
  62. pageup: 33,
  63. pagedown: 34,
  64. end: 35,
  65. home: 36,
  66. arrowleft: 37,
  67. arrowup: 38,
  68. arrowright: 39,
  69. arrowdown: 40,
  70. insert: 45,
  71. delete: 46,
  72. meta: 91,
  73. numlock: 144,
  74. scrolllock: 145,
  75. ';': 186,
  76. '=': 187,
  77. ',': 188,
  78. '-': 189,
  79. '.': 190,
  80. '/': 191,
  81. '`': 192,
  82. '[': 219,
  83. '\\': 220,
  84. ']': 221,
  85. '\'': 222
  86. };
  87. for (var f = 1; f < 20; f++) {
  88. CODES['f' + f] = 111 + f;
  89. }
  90. /**
  91. * Is hotkey?
  92. */
  93. function isHotkey(hotkey, options, event) {
  94. if (options && !('byKey' in options)) {
  95. event = options;
  96. options = null;
  97. }
  98. if (!Array.isArray(hotkey)) {
  99. hotkey = [hotkey];
  100. }
  101. var array = hotkey.map(function (string) {
  102. return parseHotkey(string, options);
  103. });
  104. var check = function check(e) {
  105. return array.some(function (object) {
  106. return compareHotkey(object, e);
  107. });
  108. };
  109. var ret = event == null ? check : check(event);
  110. return ret;
  111. }
  112. function isCodeHotkey(hotkey, event) {
  113. return isHotkey(hotkey, event);
  114. }
  115. function isKeyHotkey(hotkey, event) {
  116. return isHotkey(hotkey, { byKey: true }, event);
  117. }
  118. /**
  119. * Parse.
  120. */
  121. function parseHotkey(hotkey, options) {
  122. var byKey = options && options.byKey;
  123. var ret = {};
  124. // Special case to handle the `+` key since we use it as a separator.
  125. hotkey = hotkey.replace('++', '+add');
  126. var values = hotkey.split('+');
  127. var length = values.length;
  128. // Ensure that all the modifiers are set to false unless the hotkey has them.
  129. for (var k in MODIFIERS) {
  130. ret[MODIFIERS[k]] = false;
  131. }
  132. var _iteratorNormalCompletion = true;
  133. var _didIteratorError = false;
  134. var _iteratorError = undefined;
  135. try {
  136. for (var _iterator = values[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
  137. var value = _step.value;
  138. var optional = value.endsWith('?') && value.length > 1;
  139. if (optional) {
  140. value = value.slice(0, -1);
  141. }
  142. var name = toKeyName(value);
  143. var modifier = MODIFIERS[name];
  144. if (value.length > 1 && !modifier && !ALIASES[value] && !CODES[name]) {
  145. throw new TypeError('Unknown modifier: "' + value + '"');
  146. }
  147. if (length === 1 || !modifier) {
  148. if (byKey) {
  149. ret.key = name;
  150. } else {
  151. ret.which = toKeyCode(value);
  152. }
  153. }
  154. if (modifier) {
  155. ret[modifier] = optional ? null : true;
  156. }
  157. }
  158. } catch (err) {
  159. _didIteratorError = true;
  160. _iteratorError = err;
  161. } finally {
  162. try {
  163. if (!_iteratorNormalCompletion && _iterator.return) {
  164. _iterator.return();
  165. }
  166. } finally {
  167. if (_didIteratorError) {
  168. throw _iteratorError;
  169. }
  170. }
  171. }
  172. return ret;
  173. }
  174. /**
  175. * Compare.
  176. */
  177. function compareHotkey(object, event) {
  178. for (var key in object) {
  179. var expected = object[key];
  180. var actual = void 0;
  181. if (expected == null) {
  182. continue;
  183. }
  184. if (key === 'key' && event.key != null) {
  185. actual = event.key.toLowerCase();
  186. } else if (key === 'which') {
  187. actual = expected === 91 && event.which === 93 ? 91 : event.which;
  188. } else {
  189. actual = event[key];
  190. }
  191. if (actual == null && expected === false) {
  192. continue;
  193. }
  194. if (actual !== expected) {
  195. return false;
  196. }
  197. }
  198. return true;
  199. }
  200. /**
  201. * Utils.
  202. */
  203. function toKeyCode(name) {
  204. name = toKeyName(name);
  205. var code = CODES[name] || name.toUpperCase().charCodeAt(0);
  206. return code;
  207. }
  208. function toKeyName(name) {
  209. name = name.toLowerCase();
  210. name = ALIASES[name] || name;
  211. return name;
  212. }
  213. /**
  214. * Export.
  215. */
  216. exports.default = isHotkey;
  217. exports.isHotkey = isHotkey;
  218. exports.isCodeHotkey = isCodeHotkey;
  219. exports.isKeyHotkey = isKeyHotkey;
  220. exports.parseHotkey = parseHotkey;
  221. exports.compareHotkey = compareHotkey;
  222. exports.toKeyCode = toKeyCode;
  223. exports.toKeyName = toKeyName;
  224. });