jquery.hotkeys.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. /*
  13. * One small change is: now keys are passed by object { keys: '...' }
  14. * Might be useful, when you want to pass some other data to your handler
  15. */
  16. (function(jQuery){
  17. jQuery.hotkeys = {
  18. version: "0.8",
  19. specialKeys: {
  20. 8: "backspace", 9: "tab", 10: "return", 13: "return", 16: "shift", 17: "ctrl", 18: "alt", 19: "pause",
  21. 20: "capslock", 27: "esc", 32: "space", 33: "pageup", 34: "pagedown", 35: "end", 36: "home",
  22. 37: "left", 38: "up", 39: "right", 40: "down", 45: "insert", 46: "del", 59: ";", 61: "=",
  23. 96: "0", 97: "1", 98: "2", 99: "3", 100: "4", 101: "5", 102: "6", 103: "7",
  24. 104: "8", 105: "9", 106: "*", 107: "+", 109: "-", 110: ".", 111 : "/",
  25. 112: "f1", 113: "f2", 114: "f3", 115: "f4", 116: "f5", 117: "f6", 118: "f7", 119: "f8",
  26. 120: "f9", 121: "f10", 122: "f11", 123: "f12", 144: "numlock", 145: "scroll", 173: "-", 186: ";", 187: "=",
  27. 188: ",", 189: "-", 190: ".", 191: "/", 192: "`", 219: "[", 220: "\\", 221: "]", 222: "'"
  28. },
  29. shiftNums: {
  30. "`": "~", "1": "!", "2": "@", "3": "#", "4": "$", "5": "%", "6": "^", "7": "&",
  31. "8": "*", "9": "(", "0": ")", "-": "_", "=": "+", ";": ": ", "'": "\"", ",": "<",
  32. ".": ">", "/": "?", "\\": "|"
  33. },
  34. // excludes: button, checkbox, file, hidden, image, password, radio, reset, search, submit, url
  35. textAcceptingInputTypes: [
  36. "text", "password", "number", "email", "url", "range", "date", "month", "week", "time", "datetime",
  37. "datetime-local", "search", "color", "tel"],
  38. options: {
  39. filterTextInputs: true
  40. }
  41. };
  42. function keyHandler( handleObj ) {
  43. if ( typeof handleObj.data === "string" ) {
  44. handleObj.data = { keys: handleObj.data };
  45. }
  46. // Only care when a possible input has been specified
  47. if ( !handleObj.data || !handleObj.data.keys || typeof handleObj.data.keys !== "string" ) {
  48. return;
  49. }
  50. var origHandler = handleObj.handler,
  51. keys = handleObj.data.keys.toLowerCase().split(" ");
  52. handleObj.handler = function( event ) {
  53. // Don't fire in text-accepting inputs that we didn't directly bind to
  54. if ( this !== event.target && (/textarea|select/i.test( event.target.nodeName ) ||
  55. ( jQuery.hotkeys.options.filterTextInputs &&
  56. jQuery.inArray(event.target.type, jQuery.hotkeys.textAcceptingInputTypes) > -1 ) ) ) {
  57. return;
  58. }
  59. var special = jQuery.hotkeys.specialKeys[ event.keyCode ],
  60. character = String.fromCharCode( event.which ).toLowerCase(),
  61. modif = "", possible = {};
  62. jQuery.each([ "alt", "ctrl", "meta", "shift" ], function(index, specialKey) {
  63. if (event[specialKey + 'Key'] && special !== specialKey) {
  64. modif += specialKey + '+';
  65. }
  66. });
  67. modif = modif.replace('alt+ctrl+meta+shift', 'hyper');
  68. if ( special ) {
  69. possible[ modif + special ] = true;
  70. }
  71. if ( character ) {
  72. possible[ modif + character ] = true;
  73. possible[ modif + jQuery.hotkeys.shiftNums[ character ] ] = true;
  74. // "$" can be triggered as "Shift+4" or "Shift+$" or just "$"
  75. if ( modif === "shift+" ) {
  76. possible[ jQuery.hotkeys.shiftNums[ character ] ] = true;
  77. }
  78. }
  79. for ( var i = 0, l = keys.length; i < l; i++ ) {
  80. if ( possible[ keys[i] ] ) {
  81. return origHandler.apply( this, arguments );
  82. }
  83. }
  84. };
  85. }
  86. jQuery.each([ "keydown", "keyup", "keypress" ], function() {
  87. jQuery.event.special[ this ] = { add: keyHandler };
  88. });
  89. })( this.jQuery );