F1 Key blocker

Blocks F1 key default behaviour

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