shortcut.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. var ths = this;
  27. shortcut_combination = shortcut_combination.toLowerCase();
  28. //The function to be called at keypress
  29. var func = function(e) {
  30. e = e || window.event;
  31. if(opt['disable_in_input']) { //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) { //If it is a special key
  141. if(special_keys[k] == code) kp++;
  142. } else if(opt['keycode']) {
  143. if(opt['keycode'] == code) kp++;
  144. } else { //The special keys did not match
  145. if(character == k) kp++;
  146. else {
  147. if(shift_nums[character] && e.shiftKey) { //Stupid Shift key bug created by using lowercase
  148. character = shift_nums[character];
  149. if(character == k) kp++;
  150. }
  151. }
  152. }
  153. }
  154. if(kp == keys.length &&
  155. modifiers.ctrl.pressed == modifiers.ctrl.wanted &&
  156. modifiers.shift.pressed == modifiers.shift.wanted &&
  157. modifiers.alt.pressed == modifiers.alt.wanted &&
  158. modifiers.meta.pressed == modifiers.meta.wanted) {
  159. callback(e);
  160. if(!opt['propagate']) { //Stop the event
  161. //e.cancelBubble is supported by IE - this will kill the bubbling process.
  162. e.cancelBubble = true;
  163. e.returnValue = false;
  164. //e.stopPropagation works in Firefox.
  165. if (e.stopPropagation) {
  166. e.stopPropagation();
  167. e.preventDefault();
  168. }
  169. return false;
  170. }
  171. }
  172. }
  173. this.all_shortcuts[shortcut_combination] = {
  174. 'callback':func,
  175. 'target':ele,
  176. 'event': opt['type']
  177. };
  178. //Attach the function with the event
  179. if(ele.addEventListener) ele.addEventListener(opt['type'], func, false);
  180. else if(ele.attachEvent) ele.attachEvent('on'+opt['type'], func);
  181. else ele['on'+opt['type']] = func;
  182. },
  183. //Remove the shortcut - just specify the shortcut and I will remove the binding
  184. 'remove':function(shortcut_combination) {
  185. shortcut_combination = shortcut_combination.toLowerCase();
  186. var binding = this.all_shortcuts[shortcut_combination];
  187. delete(this.all_shortcuts[shortcut_combination])
  188. if(!binding) return;
  189. var type = binding['event'];
  190. var ele = binding['target'];
  191. var callback = binding['callback'];
  192. if(ele.detachEvent) ele.detachEvent('on'+type, callback);
  193. else if(ele.removeEventListener) ele.removeEventListener(type, callback, false);
  194. else ele['on'+type] = false;
  195. }
  196. }