hotkeys.js 7.5 KB

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