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