shortcut.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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':{},
  9. 'add': function(shortcut_combination,callback,opt) {
  10. var default_options = {
  11. 'type':'keydown',
  12. 'propagate':false,
  13. 'disable_in_input':false,
  14. 'target':document,
  15. 'keycode':false
  16. }
  17. if(!opt) opt = default_options;
  18. else {
  19. for(var dfo in default_options) {
  20. if(typeof opt[dfo] == 'undefined') opt[dfo] = default_options[dfo];
  21. }
  22. }
  23. var ele = opt.target;
  24. if(typeof opt.target == 'string') ele = document.getElementById(opt.target);
  25. var ths = this;
  26. shortcut_combination = shortcut_combination.toLowerCase();
  27. var func = function(e) {
  28. e = e || window.event;
  29. if(opt['disable_in_input']) {
  30. var element;
  31. if(e.target) element=e.target;
  32. else if(e.srcElement) element=e.srcElement;
  33. if(element.nodeType==3) element=element.parentNode;
  34. if(element.tagName == 'INPUT' || element.tagName == 'TEXTAREA') return;
  35. }
  36. if (e.keyCode) code = e.keyCode;
  37. else if (e.which) code = e.which;
  38. var character = String.fromCharCode(code).toLowerCase();
  39. if(code == 188) character=",";
  40. if(code == 190) character=".";
  41. var keys = shortcut_combination.split("+");
  42. var kp = 0;
  43. var shift_nums = {
  44. "`":"~",
  45. "1":"!",
  46. "2":"@",
  47. "3":"#",
  48. "4":"$",
  49. "5":"%",
  50. "6":"^",
  51. "7":"&",
  52. "8":"*",
  53. "9":"(",
  54. "0":")",
  55. "-":"_",
  56. "=":"+",
  57. ";":":",
  58. "'":"\"",
  59. ",":"<",
  60. ".":">",
  61. "/":"?",
  62. "\\":"|"
  63. }
  64. var special_keys = {
  65. 'esc':27,
  66. 'escape':27,
  67. 'tab':9,
  68. 'space':32,
  69. 'return':13,
  70. 'enter':13,
  71. 'backspace':8,
  72. 'scrolllock':145,
  73. 'scroll_lock':145,
  74. 'scroll':145,
  75. 'capslock':20,
  76. 'caps_lock':20,
  77. 'caps':20,
  78. 'numlock':144,
  79. 'num_lock':144,
  80. 'num':144,
  81. 'pause':19,
  82. 'break':19,
  83. 'insert':45,
  84. 'home':36,
  85. 'delete':46,
  86. 'end':35,
  87. 'pageup':33,
  88. 'page_up':33,
  89. 'pu':33,
  90. 'pagedown':34,
  91. 'page_down':34,
  92. 'pd':34,
  93. 'left':37,
  94. 'up':38,
  95. 'right':39,
  96. 'down':40,
  97. 'f1':112,
  98. 'f2':113,
  99. 'f3':114,
  100. 'f4':115,
  101. 'f5':116,
  102. 'f6':117,
  103. 'f7':118,
  104. 'f8':119,
  105. 'f9':120,
  106. 'f10':121,
  107. 'f11':122,
  108. 'f12':123
  109. }
  110. var modifiers = {
  111. shift: { wanted:false, pressed:false},
  112. ctrl : { wanted:false, pressed:false},
  113. alt : { wanted:false, pressed:false},
  114. meta : { wanted:false, pressed:false}
  115. };
  116. if(e.ctrlKey)modifiers.ctrl.pressed = true;
  117. if(e.shiftKey)modifiers.shift.pressed = true;
  118. if(e.altKey)modifiers.alt.pressed = true;
  119. if(e.metaKey) modifiers.meta.pressed = true;
  120. for(var i=0; k=keys[i],i<keys.length; i++) {
  121. //Modifiers
  122. if(k == 'ctrl' || k == 'control') {
  123. kp++;
  124. modifiers.ctrl.wanted = true;
  125. } else if(k == 'shift') {
  126. kp++;
  127. modifiers.shift.wanted = true;
  128. } else if(k == 'alt') {
  129. kp++;
  130. modifiers.alt.wanted = true;
  131. } else if(k == 'meta') {
  132. kp++;
  133. modifiers.meta.wanted = true;
  134. } else if(k.length > 1) {
  135. if(special_keys[k] == code) kp++;
  136. } else if(opt['keycode']) {
  137. if(opt['keycode'] == code) kp++;
  138. } else {
  139. if(character == k) kp++;
  140. else {
  141. if(shift_nums[character] && e.shiftKey) {
  142. character = shift_nums[character];
  143. if(character == k) kp++;
  144. }
  145. }
  146. }
  147. }
  148. if(kp == keys.length &&
  149. modifiers.ctrl.pressed == modifiers.ctrl.wanted &&
  150. modifiers.shift.pressed == modifiers.shift.wanted &&
  151. modifiers.alt.pressed == modifiers.alt.wanted &&
  152. modifiers.meta.pressed == modifiers.meta.wanted) {
  153. callback(e);
  154. if(!opt['propagate']) {
  155. e.cancelBubble = true;
  156. e.returnValue = false;
  157. if (e.stopPropagation) {
  158. e.stopPropagation();
  159. e.preventDefault();
  160. }
  161. return false;
  162. }
  163. }
  164. }
  165. this.all_shortcuts[shortcut_combination] = {
  166. 'callback':func,
  167. 'target':ele,
  168. 'event': opt['type']
  169. };
  170. if(ele.addEventListener) ele.addEventListener(opt['type'], func, false);
  171. else if(ele.attachEvent) ele.attachEvent('on'+opt['type'], func);
  172. else ele['on'+opt['type']] = func;
  173. },
  174. 'remove':function(shortcut_combination) {
  175. shortcut_combination = shortcut_combination.toLowerCase();
  176. var binding = this.all_shortcuts[shortcut_combination];
  177. delete(this.all_shortcuts[shortcut_combination])
  178. if(!binding) return;
  179. var type = binding['event'];
  180. var ele = binding['target'];
  181. var callback = binding['callback'];
  182. if(ele.detachEvent) ele.detachEvent('on'+type, callback);
  183. else if(ele.removeEventListener) ele.removeEventListener(type, callback, false);
  184. else ele['on'+type] = false;
  185. }
  186. }