jquery.hotkeys.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*jslint browser: true*/
  2. /*jslint jquery: true*/
  3. /*
  4. * jQuery Hotkeys Plugin
  5. * Copyright 2010, John Resig
  6. * Dual licensed under the MIT or GPL Version 2 licenses.
  7. *
  8. * Based upon the plugin by Tzury Bar Yochay:
  9. * https://github.com/tzuryby/jquery.hotkeys
  10. *
  11. * Original idea by:
  12. * Binny V A, http://www.openjs.com/scripts/events/keyboard_shortcuts/
  13. */
  14. /*
  15. * One small change is: now keys are passed by object { keys: '...' }
  16. * Might be useful, when you want to pass some other data to your handler
  17. */
  18. (function(jQuery) {
  19. jQuery.hotkeys = {
  20. version: "0.2.0",
  21. specialKeys: {
  22. 8: "backspace",
  23. 9: "tab",
  24. 10: "return",
  25. 13: "return",
  26. 16: "shift",
  27. 17: "ctrl",
  28. 18: "alt",
  29. 19: "pause",
  30. 20: "capslock",
  31. 27: "esc",
  32. 32: "space",
  33. 33: "pageup",
  34. 34: "pagedown",
  35. 35: "end",
  36. 36: "home",
  37. 37: "left",
  38. 38: "up",
  39. 39: "right",
  40. 40: "down",
  41. 45: "insert",
  42. 46: "del",
  43. 59: ";",
  44. 61: "=",
  45. 96: "0",
  46. 97: "1",
  47. 98: "2",
  48. 99: "3",
  49. 100: "4",
  50. 101: "5",
  51. 102: "6",
  52. 103: "7",
  53. 104: "8",
  54. 105: "9",
  55. 106: "*",
  56. 107: "+",
  57. 109: "-",
  58. 110: ".",
  59. 111: "/",
  60. 112: "f1",
  61. 113: "f2",
  62. 114: "f3",
  63. 115: "f4",
  64. 116: "f5",
  65. 117: "f6",
  66. 118: "f7",
  67. 119: "f8",
  68. 120: "f9",
  69. 121: "f10",
  70. 122: "f11",
  71. 123: "f12",
  72. 144: "numlock",
  73. 145: "scroll",
  74. 173: "-",
  75. 186: ";",
  76. 187: "=",
  77. 188: ",",
  78. 189: "-",
  79. 190: ".",
  80. 191: "/",
  81. 192: "`",
  82. 219: "[",
  83. 220: "\\",
  84. 221: "]",
  85. 222: "'"
  86. },
  87. shiftNums: {
  88. "`": "~",
  89. "1": "!",
  90. "2": "@",
  91. "3": "#",
  92. "4": "$",
  93. "5": "%",
  94. "6": "^",
  95. "7": "&",
  96. "8": "*",
  97. "9": "(",
  98. "0": ")",
  99. "-": "_",
  100. "=": "+",
  101. ";": ": ",
  102. "'": "\"",
  103. ",": "<",
  104. ".": ">",
  105. "/": "?",
  106. "\\": "|"
  107. },
  108. // excludes: button, checkbox, file, hidden, image, password, radio, reset, search, submit, url
  109. textAcceptingInputTypes: [
  110. "text", "password", "number", "email", "url", "range", "date", "month", "week", "time", "datetime",
  111. "datetime-local", "search", "color", "tel"],
  112. // default input types not to bind to unless bound directly
  113. textInputTypes: /textarea|input|select/i,
  114. options: {
  115. filterInputAcceptingElements: true,
  116. filterTextInputs: true,
  117. filterContentEditable: true
  118. }
  119. };
  120. function keyHandler(handleObj) {
  121. if (typeof handleObj.data === "string") {
  122. handleObj.data = {
  123. keys: handleObj.data
  124. };
  125. }
  126. // Only care when a possible input has been specified
  127. if (!handleObj.data || !handleObj.data.keys || typeof handleObj.data.keys !== "string") {
  128. return;
  129. }
  130. var origHandler = handleObj.handler,
  131. keys = handleObj.data.keys.toLowerCase().split(" ");
  132. handleObj.handler = function(event) {
  133. // Don't fire in text-accepting inputs that we didn't directly bind to
  134. if (this !== event.target &&
  135. (jQuery.hotkeys.options.filterInputAcceptingElements &&
  136. jQuery.hotkeys.textInputTypes.test(event.target.nodeName) ||
  137. (jQuery.hotkeys.options.filterContentEditable && jQuery(event.target).attr('contenteditable')) ||
  138. (jQuery.hotkeys.options.filterTextInputs &&
  139. jQuery.inArray(event.target.type, jQuery.hotkeys.textAcceptingInputTypes) > -1))) {
  140. return;
  141. }
  142. var special = event.type !== "keypress" && jQuery.hotkeys.specialKeys[event.which],
  143. character = String.fromCharCode(event.which).toLowerCase(),
  144. modif = "",
  145. possible = {};
  146. jQuery.each(["alt", "ctrl", "shift"], function(index, specialKey) {
  147. if (event[specialKey + 'Key'] && special !== specialKey) {
  148. modif += specialKey + '+';
  149. }
  150. });
  151. // metaKey is triggered off ctrlKey erronously
  152. if (event.metaKey && !event.ctrlKey && special !== "meta") {
  153. modif += "meta+";
  154. }
  155. if (event.metaKey && special !== "meta" && modif.indexOf("alt+ctrl+shift+") > -1) {
  156. modif = modif.replace("alt+ctrl+shift+", "hyper+");
  157. }
  158. if (special) {
  159. possible[modif + special] = true;
  160. }
  161. else {
  162. possible[modif + character] = true;
  163. possible[modif + jQuery.hotkeys.shiftNums[character]] = true;
  164. // "$" can be triggered as "Shift+4" or "Shift+$" or just "$"
  165. if (modif === "shift+") {
  166. possible[jQuery.hotkeys.shiftNums[character]] = true;
  167. }
  168. }
  169. for (var i = 0, l = keys.length; i < l; i++) {
  170. if (possible[keys[i]]) {
  171. return origHandler.apply(this, arguments);
  172. }
  173. }
  174. };
  175. }
  176. jQuery.each(["keydown", "keyup", "keypress"], function() {
  177. jQuery.event.special[this] = {
  178. add: keyHandler
  179. };
  180. });
  181. })(jQuery || this.jQuery || window.jQuery);