2 * http://www.openjs.com/scripts/events/keyboard_shortcuts/
8 'all_shortcuts':{},//All the shortcuts are stored in this array
9 'add': function(shortcut_combination,callback,opt) {
10 //Provide a set of default options
11 var default_options = {
14 'disable_in_input':false,
18 if(!opt) opt = default_options;
20 for(var dfo in default_options) {
21 if(typeof opt[dfo] == 'undefined') opt[dfo] = default_options[dfo];
26 if(typeof opt.target == 'string') ele = document.getElementById(opt.target);
28 shortcut_combination = shortcut_combination.toLowerCase();
30 //The function to be called at keypress
31 var func = function(e) {
32 e = e || window.event;
34 if(opt['disable_in_input']) { //Don't enable shortcut keys in Input, Textarea fields
36 if(e.target) element=e.target;
37 else if(e.srcElement) element=e.srcElement;
38 if(element.nodeType==3) element=element.parentNode;
40 if(element.tagName == 'INPUT' || element.tagName == 'TEXTAREA') return;
43 //Find Which key is pressed
44 if (e.keyCode) code = e.keyCode;
45 else if (e.which) code = e.which;
46 var character = String.fromCharCode(code).toLowerCase();
48 if(code == 188) character=","; //If the user presses , when the type is onkeydown
49 if(code == 190) character="."; //If the user presses , when the type is onkeydown
51 var keys = shortcut_combination.split("+");
52 //Key Pressed - counts the number of valid keypresses - if it is same as the number of keys, the shortcut function is invoked
55 //Work around for stupid Shift key bug created by using lowercase - as a result the shift+num combination was broken
77 //Special Keys - and their codes
133 shift: { wanted:false, pressed:false},
134 ctrl : { wanted:false, pressed:false},
135 alt : { wanted:false, pressed:false},
136 meta : { wanted:false, pressed:false} //Meta is Mac specific
139 if(e.ctrlKey) modifiers.ctrl.pressed = true;
140 if(e.shiftKey) modifiers.shift.pressed = true;
141 if(e.altKey) modifiers.alt.pressed = true;
142 if(e.metaKey) modifiers.meta.pressed = true;
144 for(var i=0; k=keys[i],i<keys.length; i++) {
146 if(k == 'ctrl' || k == 'control') {
148 modifiers.ctrl.wanted = true;
150 } else if(k == 'shift') {
152 modifiers.shift.wanted = true;
154 } else if(k == 'alt') {
156 modifiers.alt.wanted = true;
157 } else if(k == 'meta') {
159 modifiers.meta.wanted = true;
160 } else if(k.length > 1) { //If it is a special key
161 if(special_keys[k] == code) kp++;
163 } else if(opt['keycode']) {
164 if(opt['keycode'] == code) kp++;
166 } else { //The special keys did not match
167 if(character == k) kp++;
169 if(shift_nums[character] && e.shiftKey) { //Stupid Shift key bug created by using lowercase
170 character = shift_nums[character];
171 if(character == k) kp++;
177 if(kp == keys.length &&
178 modifiers.ctrl.pressed == modifiers.ctrl.wanted &&
179 modifiers.shift.pressed == modifiers.shift.wanted &&
180 modifiers.alt.pressed == modifiers.alt.wanted &&
181 modifiers.meta.pressed == modifiers.meta.wanted) {
184 if(!opt['propagate']) { //Stop the event
185 //e.cancelBubble is supported by IE - this will kill the bubbling process.
186 e.cancelBubble = true;
187 e.returnValue = false;
189 //e.stopPropagation works in Firefox.
190 if (e.stopPropagation) {
198 this.all_shortcuts[shortcut_combination] = {
203 //Attach the function with the event
204 if(ele.addEventListener) ele.addEventListener(opt['type'], func, false);
205 else if(ele.attachEvent) ele.attachEvent('on'+opt['type'], func);
206 else ele['on'+opt['type']] = func;
209 //Remove the shortcut - just specify the shortcut and I will remove the binding
210 'remove':function(shortcut_combination) {
211 shortcut_combination = shortcut_combination.toLowerCase();
212 var binding = this.all_shortcuts[shortcut_combination];
213 delete(this.all_shortcuts[shortcut_combination])
215 var type = binding['event'];
216 var ele = binding['target'];
217 var callback = binding['callback'];
219 if(ele.detachEvent) ele.detachEvent('on'+type, callback);
220 else if(ele.removeEventListener) ele.removeEventListener(type, callback, false);
221 else ele['on'+type] = false;