jquery.hotkeys.js 5.5 KB

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