jquery.hotkeys.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * jQuery Hotkeys Plugin
  3. * Copyright 2010, John Resig
  4. * Dual licensed under the MIT or GPL Version 2 licenses.
  5. *
  6. * Based upon the plugin by Tzury Bar Yochay:
  7. * http://github.com/tzuryby/hotkeys
  8. *
  9. * Original idea by:
  10. * Binny V A, http://www.openjs.com/scripts/events/keyboard_shortcuts/
  11. */
  12. (function(jQuery){
  13. jQuery.hotkeys = {
  14. version: "0.8+",
  15. specialKeys: {
  16. 8: "backspace", 9: "tab", 13: "return", 16: "shift", 17: "ctrl", 18: "alt", 19: "pause",
  17. 20: "capslock", 27: "esc", 32: "space", 33: "pageup", 34: "pagedown", 35: "end", 36: "home",
  18. 37: "left", 38: "up", 39: "right", 40: "down", 45: "insert", 46: "del",
  19. 96: "0", 97: "1", 98: "2", 99: "3", 100: "4", 101: "5", 102: "6", 103: "7",
  20. 104: "8", 105: "9", 106: "*", 107: "+", 109: "-", 110: ".", 111 : "/",
  21. 112: "f1", 113: "f2", 114: "f3", 115: "f4", 116: "f5", 117: "f6", 118: "f7", 119: "f8",
  22. 120: "f9", 121: "f10", 122: "f11", 123: "f12", 144: "numlock", 145: "scroll", 188: ",", 190: ".",
  23. 191: "/", 224: "meta"
  24. },
  25. shiftNums: {
  26. "`": "~", "1": "!", "2": "@", "3": "#", "4": "$", "5": "%", "6": "^", "7": "&",
  27. "8": "*", "9": "(", "0": ")", "-": "_", "=": "+", ";": ": ", "'": "\"", ",": "<",
  28. ".": ">", "/": "?", "\\": "|"
  29. }
  30. };
  31. function keyHandler( handleObj ) {
  32. var origHandler = handleObj.handler,
  33. //use namespace as keys so it works with event delegation as well
  34. //will also allow removing listeners of a specific key combination
  35. //and support data objects
  36. keys = (handleObj.namespace || "").toLowerCase().split(" ");
  37. keys = jQuery.map(keys, function(key) { return key.split("."); });
  38. //no need to modify handler if no keys specified
  39. //Added keys[0].substring(0, 12) to work with jQuery ui 1.9.0
  40. //Added accordion, tabs and menu, then jquery ui can use keys.
  41. if (keys.length === 1 && (keys[0] === "" ||
  42. keys[0].substring(0, 12) === "autocomplete" ||
  43. keys[0].substring(0, 9) === "accordion" ||
  44. keys[0].substring(0, 4) === "tabs" ||
  45. keys[0].substring(0, 4) === "menu")) {
  46. return;
  47. }
  48. handleObj.handler = function( event ) {
  49. // Don't fire in text-accepting inputs that we didn't directly bind to
  50. // important to note that $.fn.prop is only available on jquery 1.6+
  51. if ( this !== event.target && (/textarea|select/i.test( event.target.nodeName ) ||
  52. event.target.type === "text" || $(event.target).prop('contenteditable') == 'true' )) {
  53. return;
  54. }
  55. // Keypress represents characters, not special keys
  56. var special = event.type !== "keypress" && jQuery.hotkeys.specialKeys[ event.which ],
  57. character = String.fromCharCode( event.which ).toLowerCase(),
  58. key, modif = "", possible = {};
  59. // check combinations (alt|ctrl|shift+anything)
  60. if ( event.altKey && special !== "alt" ) {
  61. modif += "alt_";
  62. }
  63. if ( event.ctrlKey && special !== "ctrl" ) {
  64. modif += "ctrl_";
  65. }
  66. // TODO: Need to make sure this works consistently across platforms
  67. if ( event.metaKey && !event.ctrlKey && special !== "meta" ) {
  68. modif += "meta_";
  69. }
  70. if ( event.shiftKey && special !== "shift" ) {
  71. modif += "shift_";
  72. }
  73. if ( special ) {
  74. possible[ modif + special ] = true;
  75. } else {
  76. possible[ modif + character ] = true;
  77. possible[ modif + jQuery.hotkeys.shiftNums[ character ] ] = true;
  78. // "$" can be triggered as "Shift+4" or "Shift+$" or just "$"
  79. if ( modif === "shift_" ) {
  80. possible[ jQuery.hotkeys.shiftNums[ character ] ] = true;
  81. }
  82. }
  83. for ( var i = 0, l = keys.length; i < l; i++ ) {
  84. if ( possible[ keys[i] ] ) {
  85. return origHandler.apply( this, arguments );
  86. }
  87. }
  88. };
  89. }
  90. jQuery.each([ "keydown", "keyup", "keypress" ], function() {
  91. jQuery.event.special[ this ] = { add: keyHandler };
  92. });
  93. })( jQuery );