0
|
1 |
/****************************************************************************************************************************** |
|
2 |
|
5
|
3 |
* @ Original idea by by Binny V A, Original version: 2.00.A |
0
|
4 |
* @ http://www.openjs.com/scripts/events/keyboard_shortcuts/ |
|
5 |
* @ Original License : BSD |
5
|
6 |
|
|
7 |
* @ jQuery Plugin by Tzury Bar Yochay |
0
|
8 |
mail: tzury.by@gmail.com |
|
9 |
blog: evalinux.wordpress.com |
|
10 |
face: facebook.com/profile.php?id=513676303 |
5
|
11 |
|
0
|
12 |
(c) Copyrights 2007 |
5
|
13 |
|
0
|
14 |
* @ jQuery Plugin version Beta (0.0.2) |
|
15 |
* @ License: jQuery-License. |
5
|
16 |
|
0
|
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?');});> |
5
|
24 |
$.hotkeys.remove('Ctrl+c'); |
|
25 |
$.hotkeys.remove('Ctrl+c', {target:'div#editor', type:'keypress'}); |
|
26 |
|
0
|
27 |
******************************************************************************************************************************/ |
|
28 |
(function (jQuery){ |
|
29 |
this.version = '(beta)(0.0.3)'; |
|
30 |
this.all = {}; |
|
31 |
this.special_keys = { |
5
|
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', |
0
|
35 |
115:'f4', 116:'f5', 117:'f6', 118:'f7', 119:'f8', 120:'f9', 121:'f10', 122:'f11', 123:'f12'}; |
5
|
36 |
|
|
37 |
this.shift_nums = { "`":"~", "1":"!", "2":"@", "3":"#", "4":"$", "5":"%", "6":"^", "7":"&", |
|
38 |
"8":"*", "9":"(", "0":")", "-":"_", "=":"+", ";":":", "'":"\"", ",":"<", |
0
|
39 |
".":">", "/":"?", "\\":"|" }; |
5
|
40 |
|
0
|
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 || {} ); |
5
|
50 |
combi = combi.toLowerCase(); |
|
51 |
|
0
|
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; |
5
|
60 |
|
|
61 |
if ( opt['disableInInput'] ) { // Disable shortcut keys in Input, Textarea fields |
0
|
62 |
var target = jQuery(element); |
5
|
63 |
|
|
64 |
if ( ( target.is('input') || target.is('textarea') ) && |
|
65 |
( ! opt.noDisable || ! target.is( opt.noDisable ) ) ) { |
|
66 |
|
|
67 |
return; |
0
|
68 |
} |
|
69 |
} |
|
70 |
var code = event.which, |
|
71 |
type = event.type, |
|
72 |
character = String.fromCharCode(code).toLowerCase(), |
|
73 |
special = that.special_keys[code], |
|
74 |
shift = event.shiftKey, |
|
75 |
ctrl = event.ctrlKey, |
|
76 |
alt= event.altKey, |
|
77 |
meta = event.metaKey, |
|
78 |
propagate = true, // default behaivour |
|
79 |
mapPoint = null; |
5
|
80 |
|
0
|
81 |
// in opera + safari, the event.target is unpredictable. |
5
|
82 |
// for example: 'keydown' might be associated with HtmlBodyElement |
0
|
83 |
// or the element where you last clicked with your mouse. |
5
|
84 |
// WP: needed for all browsers |
0
|
85 |
// if (jQuery.browser.opera || jQuery.browser.safari){ |
|
86 |
while (!that.all[element] && element.parentNode){ |
|
87 |
element = element.parentNode; |
|
88 |
} |
|
89 |
// } |
|
90 |
var cbMap = that.all[element].events[type].callbackMap; |
|
91 |
if(!shift && !ctrl && !alt && !meta) { // No Modifiers |
|
92 |
mapPoint = cbMap[special] || cbMap[character] |
|
93 |
} |
|
94 |
// deals with combinaitons (alt|ctrl|shift+anything) |
|
95 |
else{ |
|
96 |
var modif = ''; |
|
97 |
if(alt) modif +='alt+'; |
|
98 |
if(ctrl) modif+= 'ctrl+'; |
|
99 |
if(shift) modif += 'shift+'; |
|
100 |
if(meta) modif += 'meta+'; |
|
101 |
// modifiers + special keys or modifiers + characters or modifiers + shift characters |
|
102 |
mapPoint = cbMap[modif+special] || cbMap[modif+character] || cbMap[modif+that.shift_nums[character]] |
|
103 |
} |
|
104 |
if (mapPoint){ |
|
105 |
mapPoint.cb(event); |
|
106 |
if(!mapPoint.propagate) { |
|
107 |
event.stopPropagation(); |
|
108 |
event.preventDefault(); |
|
109 |
return false; |
|
110 |
} |
|
111 |
} |
5
|
112 |
}; |
0
|
113 |
// first hook for this element |
|
114 |
if (!this.all[opt.target]){ |
|
115 |
this.all[opt.target] = {events:{}}; |
|
116 |
} |
|
117 |
if (!this.all[opt.target].events[opt.type]){ |
|
118 |
this.all[opt.target].events[opt.type] = {callbackMap: {}} |
|
119 |
jQuery.event.add(opt.target, opt.type, inspector); |
5
|
120 |
} |
|
121 |
this.all[opt.target].events[opt.type].callbackMap[combi] = {cb: callback, propagate:opt.propagate}; |
0
|
122 |
return jQuery; |
5
|
123 |
}; |
0
|
124 |
this.remove = function(exp, opt) { |
|
125 |
opt = opt || {}; |
|
126 |
target = opt.target || jQuery('html')[0]; |
|
127 |
type = opt.type || 'keydown'; |
5
|
128 |
exp = exp.toLowerCase(); |
|
129 |
delete this.all[target].events[type].callbackMap[exp] |
0
|
130 |
return jQuery; |
|
131 |
}; |
|
132 |
jQuery.hotkeys = this; |
5
|
133 |
return jQuery; |
|
134 |
})(jQuery); |