Greasy Fork is available in English.

jQuery Hotkeys Plugin

Originally from github jeresig/jquery.hotkeys

Tento skript by neměl být instalován přímo. Jedná se o knihovnu, kterou by měly jiné skripty využívat pomocí meta příkazu // @require https://update.greatest.deepsurf.us/scripts/8984/44373/jQuery%20Hotkeys%20Plugin.js

  1. // ==UserScript==
  2. // @name jQuery Hotkeys Plugin
  3. // @namespace JQHP
  4. // @author drhouse
  5. // @version 1.0
  6. // ==/UserScript==
  7.  
  8. /*
  9. * jQuery Hotkeys Plugin
  10. * Copyright 2010, John Resig
  11. * Dual licensed under the MIT or GPL Version 2 licenses.
  12. *
  13. * Based upon the plugin by Tzury Bar Yochay:
  14. * http://github.com/tzuryby/hotkeys
  15. *
  16. * Original idea by:
  17. * Binny V A, http://www.openjs.com/scripts/events/keyboard_shortcuts/
  18. */
  19.  
  20. /*
  21. * One small change is: now keys are passed by object { keys: '...' }
  22. * Might be useful, when you want to pass some other data to your handler
  23. */
  24.  
  25. (function(jQuery){
  26. jQuery.hotkeys = {
  27. version: "0.8",
  28.  
  29. specialKeys: {
  30. 8: "backspace", 9: "tab", 10: "return", 13: "return", 16: "shift", 17: "ctrl", 18: "alt", 19: "pause",
  31. 20: "capslock", 27: "esc", 32: "space", 33: "pageup", 34: "pagedown", 35: "end", 36: "home",
  32. 37: "left", 38: "up", 39: "right", 40: "down", 45: "insert", 46: "del",
  33. 96: "0", 97: "1", 98: "2", 99: "3", 100: "4", 101: "5", 102: "6", 103: "7",
  34. 104: "8", 105: "9", 106: "*", 107: "+", 109: "-", 110: ".", 111 : "/",
  35. 112: "f1", 113: "f2", 114: "f3", 115: "f4", 116: "f5", 117: "f6", 118: "f7", 119: "f8",
  36. 120: "f9", 121: "f10", 122: "f11", 123: "f12", 144: "numlock", 145: "scroll", 186: ";", 191: "/",
  37. 220: "\\", 222: "'", 224: "meta"
  38. },
  39. shiftNums: {
  40. "`": "~", "1": "!", "2": "@", "3": "#", "4": "$", "5": "%", "6": "^", "7": "&",
  41. "8": "*", "9": "(", "0": ")", "-": "_", "=": "+", ";": ": ", "'": "\"", ",": "<",
  42. ".": ">", "/": "?", "\\": "|"
  43. }
  44. };
  45.  
  46. function keyHandler( handleObj ) {
  47. if ( typeof handleObj.data === "string" ) {
  48. handleObj.data = { keys: handleObj.data };
  49. }
  50.  
  51. // Only care when a possible input has been specified
  52. if ( !handleObj.data.keys || typeof handleObj.data.keys !== "string" ) {
  53. return;
  54. }
  55.  
  56. var origHandler = handleObj.handler,
  57. keys = handleObj.data.keys.toLowerCase().split(" "),
  58. textAcceptingInputTypes = ["text", "password", "number", "email", "url", "range", "date", "month", "week", "time", "datetime", "datetime-local", "search", "color"];
  59. handleObj.handler = function( event ) {
  60. // Don't fire in text-accepting inputs that we didn't directly bind to
  61. if ( this !== event.target && (/textarea|select/i.test( event.target.nodeName ) ||
  62. jQuery.inArray(event.target.type, textAcceptingInputTypes) > -1 ) ) {
  63. return;
  64. }
  65. // Keypress represents characters, not special keys
  66. var special = event.type !== "keypress" && jQuery.hotkeys.specialKeys[ event.which ],
  67. character = String.fromCharCode( event.which ).toLowerCase(),
  68. key, modif = "", possible = {};
  69.  
  70. // check combinations (alt|ctrl|shift+anything)
  71. if ( event.altKey && special !== "alt" ) {
  72. modif += "alt+";
  73. }
  74.  
  75. if ( event.ctrlKey && special !== "ctrl" ) {
  76. modif += "ctrl+";
  77. }
  78. // TODO: Need to make sure this works consistently across platforms
  79. if ( event.metaKey && !event.ctrlKey && special !== "meta" ) {
  80. modif += "meta+";
  81. }
  82.  
  83. if ( event.shiftKey && special !== "shift" ) {
  84. modif += "shift+";
  85. }
  86.  
  87. if ( special ) {
  88. possible[ modif + special ] = true;
  89.  
  90. } else {
  91. possible[ modif + character ] = true;
  92. possible[ modif + jQuery.hotkeys.shiftNums[ character ] ] = true;
  93.  
  94. // "$" can be triggered as "Shift+4" or "Shift+$" or just "$"
  95. if ( modif === "shift+" ) {
  96. possible[ jQuery.hotkeys.shiftNums[ character ] ] = true;
  97. }
  98. }
  99.  
  100. for ( var i = 0, l = keys.length; i < l; i++ ) {
  101. if ( possible[ keys[i] ] ) {
  102. return origHandler.apply( this, arguments );
  103. }
  104. }
  105. };
  106. }
  107.  
  108. jQuery.each([ "keydown", "keyup", "keypress" ], function() {
  109. jQuery.event.special[ this ] = { add: keyHandler };
  110. });
  111.  
  112. })( jQuery );