jquery.hotkeys.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 (factory) {
  13. if (typeof define === 'function' && define.amd) {
  14. define(["jquery"], factory);
  15. } else {
  16. factory(jQuery);
  17. }
  18. }(function (jQuery) {
  19. jQuery.hotkeys = {
  20. version: "0.8",
  21. specialKeys: {
  22. 8: "backspace", 9: "tab", 13: "return", 16: "shift", 17: "ctrl", 18: "alt", 19: "pause",
  23. 20: "capslock", 27: "esc", 32: "space", 33: "pageup", 34: "pagedown", 35: "end", 36: "home",
  24. 37: "left", 38: "up", 39: "right", 40: "down", 45: "insert", 46: "del",
  25. 96: "0", 97: "1", 98: "2", 99: "3", 100: "4", 101: "5", 102: "6", 103: "7",
  26. 104: "8", 105: "9", 106: "*", 107: "+", 109: "-", 110: ".", 111 : "/",
  27. 112: "f1", 113: "f2", 114: "f3", 115: "f4", 116: "f5", 117: "f6", 118: "f7", 119: "f8",
  28. 120: "f9", 121: "f10", 122: "f11", 123: "f12", 144: "numlock", 145: "scroll", 191: "/", 224: "meta"
  29. },
  30. shiftNums: {
  31. "`": "~", "1": "!", "2": "@", "3": "#", "4": "$", "5": "%", "6": "^", "7": "&",
  32. "8": "*", "9": "(", "0": ")", "-": "_", "=": "+", ";": ": ", "'": "\"", ",": "<",
  33. ".": ">", "/": "?", "\\": "|"
  34. }
  35. };
  36. function keyHandler( handleObj ) {
  37. // Only care when a possible input has been specified
  38. if ( typeof handleObj.data !== "string" ) {
  39. return;
  40. }
  41. var origHandler = handleObj.handler,
  42. keys = handleObj.data.toLowerCase().split(" ");
  43. handleObj.handler = function( event ) {
  44. // Don't fire in text-accepting inputs that we didn't directly bind to
  45. /**
  46. * Modified by Magento to ensure it doesn't fire on any content editable areas. This library is no
  47. * longer maintained by its author however we require content editable to behave as expected.
  48. */
  49. if ( this !== event.target && (/textarea|select/i.test( event.target.nodeName ) ||
  50. event.target.type === "text" || jQuery(event.target).attr('contenteditable')) ) {
  51. return;
  52. }
  53. // Keypress represents characters, not special keys
  54. var special = event.type !== "keypress" && jQuery.hotkeys.specialKeys[ event.which ],
  55. character = String.fromCharCode( event.which ).toLowerCase(),
  56. key, modif = "", possible = {};
  57. // check combinations (alt|ctrl|shift+anything)
  58. if ( event.altKey && special !== "alt" ) {
  59. modif += "alt+";
  60. }
  61. if ( event.ctrlKey && special !== "ctrl" ) {
  62. modif += "ctrl+";
  63. }
  64. // TODO: Need to make sure this works consistently across platforms
  65. if ( event.metaKey && !event.ctrlKey && special !== "meta" ) {
  66. modif += "meta+";
  67. }
  68. if ( event.shiftKey && special !== "shift" ) {
  69. modif += "shift+";
  70. }
  71. if ( special ) {
  72. possible[ modif + special ] = true;
  73. } else {
  74. possible[ modif + character ] = true;
  75. possible[ modif + jQuery.hotkeys.shiftNums[ character ] ] = true;
  76. // "$" can be triggered as "Shift+4" or "Shift+$" or just "$"
  77. if ( modif === "shift+" ) {
  78. possible[ jQuery.hotkeys.shiftNums[ character ] ] = true;
  79. }
  80. }
  81. for ( var i = 0, l = keys.length; i < l; i++ ) {
  82. if ( possible[ keys[i] ] ) {
  83. return origHandler.apply( this, arguments );
  84. }
  85. }
  86. };
  87. }
  88. jQuery.each([ "keydown", "keyup", "keypress" ], function() {
  89. jQuery.event.special[ this ] = { add: keyHandler };
  90. });
  91. }));