openrat-cms

# OpenRat Content Management System
git clone http://git.code.weiherhei.de/openrat-cms.git
Log | Files | Refs

jquery.hotkeys.js (5991B)


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