shortcut.js 6.0 KB

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