Daniel@0: /*! Daniel@0: * jQuery Hotkeys Plugin Daniel@0: * Copyright 2010, John Resig Daniel@0: * Dual licensed under the MIT or GPL Version 2 licenses. Daniel@0: * Daniel@0: * Based upon the plugin by Tzury Bar Yochay: Daniel@0: * http://github.com/tzuryby/hotkeys Daniel@0: * Daniel@0: * Original idea by: Daniel@0: * Binny V A, http://www.openjs.com/scripts/events/keyboard_shortcuts/ Daniel@0: */ Daniel@0: Daniel@0: /* Daniel@0: * One small change is: now keys are passed by object { keys: '...' } Daniel@0: * Might be useful, when you want to pass some other data to your handler Daniel@0: */ Daniel@0: Daniel@0: (function(jQuery) { Daniel@0: Daniel@0: jQuery.hotkeys = { Daniel@0: version: "0.8", Daniel@0: Daniel@0: specialKeys: { Daniel@0: 8: "backspace", Daniel@0: 9: "tab", Daniel@0: 10: "return", Daniel@0: 13: "return", Daniel@0: 16: "shift", Daniel@0: 17: "ctrl", Daniel@0: 18: "alt", Daniel@0: 19: "pause", Daniel@0: 20: "capslock", Daniel@0: 27: "esc", Daniel@0: 32: "space", Daniel@0: 33: "pageup", Daniel@0: 34: "pagedown", Daniel@0: 35: "end", Daniel@0: 36: "home", Daniel@0: 37: "left", Daniel@0: 38: "up", Daniel@0: 39: "right", Daniel@0: 40: "down", Daniel@0: 45: "insert", Daniel@0: 46: "del", Daniel@0: 59: ";", Daniel@0: 61: "=", Daniel@0: 96: "0", Daniel@0: 97: "1", Daniel@0: 98: "2", Daniel@0: 99: "3", Daniel@0: 100: "4", Daniel@0: 101: "5", Daniel@0: 102: "6", Daniel@0: 103: "7", Daniel@0: 104: "8", Daniel@0: 105: "9", Daniel@0: 106: "*", Daniel@0: 107: "+", Daniel@0: 109: "-", Daniel@0: 110: ".", Daniel@0: 111: "/", Daniel@0: 112: "f1", Daniel@0: 113: "f2", Daniel@0: 114: "f3", Daniel@0: 115: "f4", Daniel@0: 116: "f5", Daniel@0: 117: "f6", Daniel@0: 118: "f7", Daniel@0: 119: "f8", Daniel@0: 120: "f9", Daniel@0: 121: "f10", Daniel@0: 122: "f11", Daniel@0: 123: "f12", Daniel@0: 144: "numlock", Daniel@0: 145: "scroll", Daniel@0: 173: "-", Daniel@0: 186: ";", Daniel@0: 187: "=", Daniel@0: 188: ",", Daniel@0: 189: "-", Daniel@0: 190: ".", Daniel@0: 191: "/", Daniel@0: 192: "`", Daniel@0: 219: "[", Daniel@0: 220: "\\", Daniel@0: 221: "]", Daniel@0: 222: "'" Daniel@0: }, Daniel@0: Daniel@0: shiftNums: { Daniel@0: "`": "~", Daniel@0: "1": "!", Daniel@0: "2": "@", Daniel@0: "3": "#", Daniel@0: "4": "$", Daniel@0: "5": "%", Daniel@0: "6": "^", Daniel@0: "7": "&", Daniel@0: "8": "*", Daniel@0: "9": "(", Daniel@0: "0": ")", Daniel@0: "-": "_", Daniel@0: "=": "+", Daniel@0: ";": ": ", Daniel@0: "'": "\"", Daniel@0: ",": "<", Daniel@0: ".": ">", Daniel@0: "/": "?", Daniel@0: "\\": "|" Daniel@0: }, Daniel@0: Daniel@0: // excludes: button, checkbox, file, hidden, image, password, radio, reset, search, submit, url Daniel@0: textAcceptingInputTypes: [ Daniel@0: "text", "password", "number", "email", "url", "range", "date", "month", "week", "time", "datetime", Daniel@0: "datetime-local", "search", "color", "tel"], Daniel@0: Daniel@0: options: { Daniel@0: filterTextInputs: true Daniel@0: } Daniel@0: }; Daniel@0: Daniel@0: function keyHandler(handleObj) { Daniel@0: if (typeof handleObj.data === "string") { Daniel@0: handleObj.data = { Daniel@0: keys: handleObj.data Daniel@0: }; Daniel@0: } Daniel@0: Daniel@0: // Only care when a possible input has been specified Daniel@0: if (!handleObj.data || !handleObj.data.keys || typeof handleObj.data.keys !== "string") { Daniel@0: return; Daniel@0: } Daniel@0: Daniel@0: var origHandler = handleObj.handler, Daniel@0: keys = handleObj.data.keys.toLowerCase().split(" "); Daniel@0: Daniel@0: handleObj.handler = function(event) { Daniel@0: // Don't fire in text-accepting inputs that we didn't directly bind to Daniel@0: if (this !== event.target && (/textarea|select/i.test(event.target.nodeName) || Daniel@0: (jQuery.hotkeys.options.filterTextInputs && Daniel@0: jQuery.inArray(event.target.type, jQuery.hotkeys.textAcceptingInputTypes) > -1))) { Daniel@0: return; Daniel@0: } Daniel@0: Daniel@0: var special = event.type !== "keypress" && jQuery.hotkeys.specialKeys[event.which], Daniel@0: character = String.fromCharCode(event.which).toLowerCase(), Daniel@0: modif = "", Daniel@0: possible = {}; Daniel@0: Daniel@0: jQuery.each(["alt", "ctrl", "shift"], function(index, specialKey) { Daniel@0: Daniel@0: if (event[specialKey + 'Key'] && special !== specialKey) { Daniel@0: modif += specialKey + '+'; Daniel@0: } Daniel@0: }); Daniel@0: Daniel@0: // metaKey is triggered off ctrlKey erronously Daniel@0: if (event.metaKey && !event.ctrlKey && special !== "meta") { Daniel@0: modif += "meta+"; Daniel@0: } Daniel@0: Daniel@0: if (event.metaKey && special !== "meta" && modif.indexOf("alt+ctrl+shift+") > -1) { Daniel@0: modif = modif.replace("alt+ctrl+shift+", "hyper+"); Daniel@0: } Daniel@0: Daniel@0: if (special) { Daniel@0: possible[modif + special] = true; Daniel@0: } Daniel@0: else { Daniel@0: possible[modif + character] = true; Daniel@0: possible[modif + jQuery.hotkeys.shiftNums[character]] = true; Daniel@0: Daniel@0: // "$" can be triggered as "Shift+4" or "Shift+$" or just "$" Daniel@0: if (modif === "shift+") { Daniel@0: possible[jQuery.hotkeys.shiftNums[character]] = true; Daniel@0: } Daniel@0: } Daniel@0: Daniel@0: for (var i = 0, l = keys.length; i < l; i++) { Daniel@0: if (possible[keys[i]]) { Daniel@0: return origHandler.apply(this, arguments); Daniel@0: } Daniel@0: } Daniel@0: }; Daniel@0: } Daniel@0: Daniel@0: jQuery.each(["keydown", "keyup", "keypress"], function() { Daniel@0: jQuery.event.special[this] = { Daniel@0: add: keyHandler Daniel@0: }; Daniel@0: }); Daniel@0: Daniel@0: })(jQuery || this.jQuery || window.jQuery);