hot keys

jquery.hotkeys

目前为 2014-09-21 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/5178/18278/hot%20keys.js

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

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址