1 (function(a){this.version="(beta)(0.0.3)";this.all={};this.special_keys={27:"esc",9:"tab",32:"space",13:"return",8:"backspace",145:"scroll",20:"capslock",144:"numlock",19:"pause",45:"insert",36:"home",46:"del",35:"end",33:"pageup",34:"pagedown",37:"left",38:"up",39:"right",40:"down",112:"f1",113:"f2",114:"f3",115:"f4",116:"f5",117:"f6",118:"f7",119:"f8",120:"f9",121:"f10",122:"f11",123:"f12"};this.shift_nums={"`":"~","1":"!","2":"@","3":"#","4":"$","5":"%","6":"^","7":"&","8":"*","9":"(","0":")","-":"_","=":"+",";":":","'":'"',",":"<",".":">","/":"?","\\":"|"};this.add=function(c,b,h){if(a.isFunction(b)){h=b;b={}}var d={},f={type:"keydown",propagate:false,disableInInput:false,target:a("html")[0]},e=this;d=a.extend(d,f,b||{});c=c.toLowerCase();var g=function(j){var o=j.target;if(d.disableInInput){var s=a(o);if(s.is("input")||s.is("textarea")){return}}var l=j.which,u=j.type,r=String.fromCharCode(l).toLowerCase(),t=e.special_keys[l],m=j.shiftKey,i=j.ctrlKey,p=j.altKey,w=j.metaKey,q=true,k=null;while(!e.all[o]&&o.parentNode){o=o.parentNode}var v=e.all[o].events[u].callbackMap;if(!m&&!i&&!p&&!w){k=v[t]||v[r]}else{var n="";if(p){n+="alt+"}if(i){n+="ctrl+"}if(m){n+="shift+"}if(w){n+="meta+"}k=v[n+t]||v[n+r]||v[n+e.shift_nums[r]]}if(k){k.cb(j);if(!k.propagate){j.stopPropagation();j.preventDefault();return false}}};if(!this.all[d.target]){this.all[d.target]={events:{}}}if(!this.all[d.target].events[d.type]){this.all[d.target].events[d.type]={callbackMap:{}};a.event.add(d.target,d.type,g)}this.all[d.target].events[d.type].callbackMap[c]={cb:h,propagate:d.propagate};return a};this.remove=function(c,b){b=b||{};target=b.target||a("html")[0];type=b.type||"keydown";c=c.toLowerCase();delete this.all[target].events[type].callbackMap[c];return a};a.hotkeys=this;return a})(jQuery); |
1 /****************************************************************************************************************************** |
|
2 |
|
3 * @ Original idea by by Binny V A, Original version: 2.00.A |
|
4 * @ http://www.openjs.com/scripts/events/keyboard_shortcuts/ |
|
5 * @ Original License : BSD |
|
6 |
|
7 * @ jQuery Plugin by Tzury Bar Yochay |
|
8 mail: tzury.by@gmail.com |
|
9 blog: evalinux.wordpress.com |
|
10 face: facebook.com/profile.php?id=513676303 |
|
11 |
|
12 (c) Copyrights 2007 |
|
13 |
|
14 * @ jQuery Plugin version Beta (0.0.2) |
|
15 * @ License: jQuery-License. |
|
16 |
|
17 TODO: |
|
18 add queue support (as in gmail) e.g. 'x' then 'y', etc. |
|
19 add mouse + mouse wheel events. |
|
20 |
|
21 USAGE: |
|
22 $.hotkeys.add('Ctrl+c', function(){ alert('copy anyone?');}); |
|
23 $.hotkeys.add('Ctrl+c', {target:'div#editor', type:'keyup', propagate: true},function(){ alert('copy anyone?');});> |
|
24 $.hotkeys.remove('Ctrl+c'); |
|
25 $.hotkeys.remove('Ctrl+c', {target:'div#editor', type:'keypress'}); |
|
26 |
|
27 ******************************************************************************************************************************/ |
|
28 (function (jQuery){ |
|
29 this.version = '(beta)(0.0.3)'; |
|
30 this.all = {}; |
|
31 this.special_keys = { |
|
32 27: 'esc', 9: 'tab', 32:'space', 13: 'return', 8:'backspace', 145: 'scroll', 20: 'capslock', |
|
33 144: 'numlock', 19:'pause', 45:'insert', 36:'home', 46:'del',35:'end', 33: 'pageup', |
|
34 34:'pagedown', 37:'left', 38:'up', 39:'right',40:'down', 112:'f1',113:'f2', 114:'f3', |
|
35 115:'f4', 116:'f5', 117:'f6', 118:'f7', 119:'f8', 120:'f9', 121:'f10', 122:'f11', 123:'f12'}; |
|
36 |
|
37 this.shift_nums = { "`":"~", "1":"!", "2":"@", "3":"#", "4":"$", "5":"%", "6":"^", "7":"&", |
|
38 "8":"*", "9":"(", "0":")", "-":"_", "=":"+", ";":":", "'":"\"", ",":"<", |
|
39 ".":">", "/":"?", "\\":"|" }; |
|
40 |
|
41 this.add = function(combi, options, callback) { |
|
42 if (jQuery.isFunction(options)){ |
|
43 callback = options; |
|
44 options = {}; |
|
45 } |
|
46 var opt = {}, |
|
47 defaults = {type: 'keydown', propagate: false, disableInInput: false, target: jQuery('html')[0]}, |
|
48 that = this; |
|
49 opt = jQuery.extend( opt , defaults, options || {} ); |
|
50 combi = combi.toLowerCase(); |
|
51 |
|
52 // inspect if keystroke matches |
|
53 var inspector = function(event) { |
|
54 // WP: not needed with newer jQuery |
|
55 // event = jQuery.event.fix(event); // jQuery event normalization. |
|
56 var element = event.target; |
|
57 // @ TextNode -> nodeType == 3 |
|
58 // WP: not needed with newer jQuery |
|
59 // element = (element.nodeType==3) ? element.parentNode : element; |
|
60 |
|
61 if(opt['disableInInput']) { // Disable shortcut keys in Input, Textarea fields |
|
62 var target = jQuery(element); |
|
63 if( target.is("input") || target.is("textarea")){ |
|
64 return; |
|
65 } |
|
66 } |
|
67 var code = event.which, |
|
68 type = event.type, |
|
69 character = String.fromCharCode(code).toLowerCase(), |
|
70 special = that.special_keys[code], |
|
71 shift = event.shiftKey, |
|
72 ctrl = event.ctrlKey, |
|
73 alt= event.altKey, |
|
74 meta = event.metaKey, |
|
75 propagate = true, // default behaivour |
|
76 mapPoint = null; |
|
77 |
|
78 // in opera + safari, the event.target is unpredictable. |
|
79 // for example: 'keydown' might be associated with HtmlBodyElement |
|
80 // or the element where you last clicked with your mouse. |
|
81 // WP: needed for all browsers |
|
82 // if (jQuery.browser.opera || jQuery.browser.safari){ |
|
83 while (!that.all[element] && element.parentNode){ |
|
84 element = element.parentNode; |
|
85 } |
|
86 // } |
|
87 var cbMap = that.all[element].events[type].callbackMap; |
|
88 if(!shift && !ctrl && !alt && !meta) { // No Modifiers |
|
89 mapPoint = cbMap[special] || cbMap[character] |
|
90 } |
|
91 // deals with combinaitons (alt|ctrl|shift+anything) |
|
92 else{ |
|
93 var modif = ''; |
|
94 if(alt) modif +='alt+'; |
|
95 if(ctrl) modif+= 'ctrl+'; |
|
96 if(shift) modif += 'shift+'; |
|
97 if(meta) modif += 'meta+'; |
|
98 // modifiers + special keys or modifiers + characters or modifiers + shift characters |
|
99 mapPoint = cbMap[modif+special] || cbMap[modif+character] || cbMap[modif+that.shift_nums[character]] |
|
100 } |
|
101 if (mapPoint){ |
|
102 mapPoint.cb(event); |
|
103 if(!mapPoint.propagate) { |
|
104 event.stopPropagation(); |
|
105 event.preventDefault(); |
|
106 return false; |
|
107 } |
|
108 } |
|
109 }; |
|
110 // first hook for this element |
|
111 if (!this.all[opt.target]){ |
|
112 this.all[opt.target] = {events:{}}; |
|
113 } |
|
114 if (!this.all[opt.target].events[opt.type]){ |
|
115 this.all[opt.target].events[opt.type] = {callbackMap: {}} |
|
116 jQuery.event.add(opt.target, opt.type, inspector); |
|
117 } |
|
118 this.all[opt.target].events[opt.type].callbackMap[combi] = {cb: callback, propagate:opt.propagate}; |
|
119 return jQuery; |
|
120 }; |
|
121 this.remove = function(exp, opt) { |
|
122 opt = opt || {}; |
|
123 target = opt.target || jQuery('html')[0]; |
|
124 type = opt.type || 'keydown'; |
|
125 exp = exp.toLowerCase(); |
|
126 delete this.all[target].events[type].callbackMap[exp] |
|
127 return jQuery; |
|
128 }; |
|
129 jQuery.hotkeys = this; |
|
130 return jQuery; |
|
131 })(jQuery); |