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