Advertisement
Guest User

aakeylib.js

a guest
May 14th, 2013
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @include    http://prohardver.hu/*
  3. // @include    http://mobilarena.hu/*
  4. // @include    http://itcafe.hu/*
  5. // @include    http://logout.hu/*
  6. // ==/UserScript==
  7.  
  8. function keylib_initialize(obj, hotkeys)
  9. {
  10.     obj.hotkey = null;
  11.     if (obj == null)
  12.         return;
  13.    
  14.     var onhotkeycancel = function (e)
  15.     {
  16.         e.returnValue = false;
  17.         if (e.stopPropagation)
  18.             e.stopPropagation();
  19.         if (e.preventDefault)
  20.             e.preventDefault();
  21.         return false;
  22.     };
  23.    
  24.     var onhotkeypress = function (e)
  25.     {
  26.         if (obj.hotkey == null)
  27.             return true;
  28.  
  29.         var hotkey = obj.hotkey;
  30.         if (typeof(hotkey) == "function")
  31.             hotkey(e);
  32.         else if (typeof(hotkey) == "string" && typeof(obj) == "object" && obj.tagName == "TEXTAREA")
  33.             obj.value = obj.value.substr(0, obj.selectionStart) + hotkey + obj.value.substr(obj.selectionEnd);
  34.         else if (typeof(hotkey) == "object" && (hotkey.tagName == "INPUT" || hotkey.tagName == "A"))// && window.opera == null)
  35.             hotkey.click();
  36.  
  37.         return onhotkeycancel(e);
  38.     }
  39.  
  40.     var onhotkeydown = function (e)
  41.     {
  42.         var code = e.keyCode ? e.keyCode : e.which;
  43.         if (code == null)
  44.             return true;
  45.        
  46.         var named_keys =
  47.         {
  48.             8: "backspace", 9: "tab", 13: "enter", 32: "space", 27: "esc",
  49.             33: "pageup", 34: "pagedown", 35: "end", 36: "home",
  50.             37: "left", 38: "up", 39 : "right", 40: "down",
  51.             45: "insert", 46: "delete",
  52.             112: "f1", 113 : "f2", 114 : "f3", 115 : "f4", 116 : "f5", 117 : "f6",
  53.             118: "f7", 119 : "f8", 120 : "f9", 121 : "f10", 122 : "f11", 123 : "f12"
  54.         };
  55.  
  56.         var key =
  57.             (e.ctrlKey ? "ctrl-" : "") + (e.altKey ? "alt-" : "") + (e.shiftKey ? "shift-" : "");
  58.         if ((code >= 65 && code <= 90) || (code >= 48 && code <= 57))
  59.             key += String.fromCharCode(code).toLowerCase();
  60.         else if (named_keys[code] == null)
  61.             return true;
  62.         else
  63.             key += named_keys[code];
  64.        
  65.         obj.hotkey = hotkeys[key];
  66.         if (obj.hotkey == null)
  67.             return true;
  68.        
  69.         return onhotkeypress(e);
  70.     };
  71.  
  72.     var onhotkeyup = function (e)
  73.     {
  74.         if (obj.hotkey == null)
  75.             return true;
  76.        
  77.         obj.hotkey = null;
  78.         return onhotkeycancel(e);
  79.     }
  80.  
  81.     obj.addEventListener("keydown", onhotkeydown, false);
  82.     obj.addEventListener("keyup", onhotkeyup, false);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement