hotkeys.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /**
  2. * http://www.openjs.com/scripts/events/keyboard_shortcuts/
  3. * Version : 2.01.B
  4. * By Binny V A
  5. * License : BSD
  6. */
  7. shortcut = {
  8. all_shortcuts: {}, //All the shortcuts are stored in this array
  9. add: function (shortcut_combination, callback, opt) {
  10. //Provide a set of default options
  11. var default_options = {
  12. type: 'keydown',
  13. propagate: false,
  14. disable_in_input: false,
  15. target: document,
  16. keycode: false,
  17. };
  18. if (!opt) opt = default_options;
  19. else {
  20. for (var dfo in default_options) {
  21. if (typeof opt[dfo] == 'undefined') opt[dfo] = default_options[dfo];
  22. }
  23. }
  24. var ele = opt.target;
  25. if (typeof opt.target == 'string') ele = document.getElementById(opt.target);
  26. shortcut_combination = shortcut_combination.toLowerCase();
  27. //The function to be called at keypress
  28. var func = function (e) {
  29. e = e || window.event;
  30. if (opt['disable_in_input']) {
  31. //Don't enable shortcut keys in Input, Textarea fields
  32. var element;
  33. if (e.target) element = e.target;
  34. else if (e.srcElement) element = e.srcElement;
  35. if (element.nodeType == 3) element = element.parentNode;
  36. if (element.tagName == 'INPUT' || element.tagName == 'TEXTAREA') return;
  37. }
  38. //Find Which key is pressed
  39. if (e.keyCode) code = e.keyCode;
  40. else if (e.which) code = e.which;
  41. var character = String.fromCharCode(code).toLowerCase();
  42. if (code == 188) character = ','; //If the user presses , when the type is onkeydown
  43. if (code == 190) character = '.'; //If the user presses , when the type is onkeydown
  44. var keys = shortcut_combination.split('+');
  45. //Key Pressed - counts the number of valid keypresses - if it is same as the number of keys, the shortcut function is invoked
  46. var kp = 0;
  47. //Work around for stupid Shift key bug created by using lowercase - as a result the shift+num combination was broken
  48. var shift_nums = {
  49. '`': '~',
  50. 1: '!',
  51. 2: '@',
  52. 3: '#',
  53. 4: '$',
  54. 5: '%',
  55. 6: '^',
  56. 7: '&',
  57. 8: '*',
  58. 9: '(',
  59. 0: ')',
  60. '-': '_',
  61. '=': '+',
  62. ';': ':',
  63. "'": '"',
  64. ',': '<',
  65. '.': '>',
  66. '/': '?',
  67. '\\': '|',
  68. };
  69. //Special Keys - and their codes
  70. var special_keys = {
  71. esc: 27,
  72. escape: 27,
  73. tab: 9,
  74. space: 32,
  75. return: 13,
  76. enter: 13,
  77. backspace: 8,
  78. scrolllock: 145,
  79. scroll_lock: 145,
  80. scroll: 145,
  81. capslock: 20,
  82. caps_lock: 20,
  83. caps: 20,
  84. numlock: 144,
  85. num_lock: 144,
  86. num: 144,
  87. pause: 19,
  88. break: 19,
  89. insert: 45,
  90. home: 36,
  91. delete: 46,
  92. end: 35,
  93. pageup: 33,
  94. page_up: 33,
  95. pu: 33,
  96. pagedown: 34,
  97. page_down: 34,
  98. pd: 34,
  99. left: 37,
  100. up: 38,
  101. right: 39,
  102. down: 40,
  103. f1: 112,
  104. f2: 113,
  105. f3: 114,
  106. f4: 115,
  107. f5: 116,
  108. f6: 117,
  109. f7: 118,
  110. f8: 119,
  111. f9: 120,
  112. f10: 121,
  113. f11: 122,
  114. f12: 123,
  115. };
  116. var modifiers = {
  117. shift: { wanted: false, pressed: false },
  118. ctrl: { wanted: false, pressed: false },
  119. alt: { wanted: false, pressed: false },
  120. meta: { wanted: false, pressed: false }, //Meta is Mac specific
  121. };
  122. if (e.ctrlKey) modifiers.ctrl.pressed = true;
  123. if (e.shiftKey) modifiers.shift.pressed = true;
  124. if (e.altKey) modifiers.alt.pressed = true;
  125. if (e.metaKey) modifiers.meta.pressed = true;
  126. for (var i = 0; (k = keys[i]), i < keys.length; i++) {
  127. //Modifiers
  128. if (k == 'ctrl' || k == 'control') {
  129. kp++;
  130. modifiers.ctrl.wanted = true;
  131. } else if (k == 'shift') {
  132. kp++;
  133. modifiers.shift.wanted = true;
  134. } else if (k == 'alt') {
  135. kp++;
  136. modifiers.alt.wanted = true;
  137. } else if (k == 'meta') {
  138. kp++;
  139. modifiers.meta.wanted = true;
  140. } else if (k.length > 1) {
  141. //If it is a special key
  142. if (special_keys[k] == code) kp++;
  143. } else if (opt['keycode']) {
  144. if (opt['keycode'] == code) kp++;
  145. } else {
  146. //The special keys did not match
  147. if (character == k) kp++;
  148. else {
  149. if (shift_nums[character] && e.shiftKey) {
  150. //Stupid Shift key bug created by using lowercase
  151. character = shift_nums[character];
  152. if (character == k) kp++;
  153. }
  154. }
  155. }
  156. }
  157. if (
  158. kp == keys.length &&
  159. modifiers.ctrl.pressed == modifiers.ctrl.wanted &&
  160. modifiers.shift.pressed == modifiers.shift.wanted &&
  161. modifiers.alt.pressed == modifiers.alt.wanted &&
  162. modifiers.meta.pressed == modifiers.meta.wanted
  163. ) {
  164. callback(e);
  165. if (!opt['propagate']) {
  166. //Stop the event
  167. //e.cancelBubble is supported by IE - this will kill the bubbling process.
  168. e.cancelBubble = true;
  169. e.returnValue = false;
  170. //e.stopPropagation works in Firefox.
  171. if (e.stopPropagation) {
  172. e.stopPropagation();
  173. e.preventDefault();
  174. }
  175. return false;
  176. }
  177. }
  178. };
  179. this.all_shortcuts[shortcut_combination] = {
  180. callback: func,
  181. target: ele,
  182. event: opt['type'],
  183. };
  184. //Attach the function with the event
  185. if (ele.addEventListener) ele.addEventListener(opt['type'], func, false);
  186. else if (ele.attachEvent) ele.attachEvent('on' + opt['type'], func);
  187. else ele['on' + opt['type']] = func;
  188. },
  189. //Remove the shortcut - just specify the shortcut and I will remove the binding
  190. remove: function (shortcut_combination) {
  191. shortcut_combination = shortcut_combination.toLowerCase();
  192. var binding = this.all_shortcuts[shortcut_combination];
  193. delete this.all_shortcuts[shortcut_combination];
  194. if (!binding) return;
  195. var type = binding['event'];
  196. var ele = binding['target'];
  197. var callback = binding['callback'];
  198. if (ele.detachEvent) ele.detachEvent('on' + type, callback);
  199. else if (ele.removeEventListener) ele.removeEventListener(type, callback, false);
  200. else ele['on' + type] = false;
  201. },
  202. };