KeyboardShortcut.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (C) 2009 Apple Inc. All rights reserved.
  3. * Copyright (C) 2009 Google Inc. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
  15. * its contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. /**
  30. * @constructor
  31. */
  32. WebInspector.KeyboardShortcut = function()
  33. {
  34. }
  35. /**
  36. * Constants for encoding modifier key set as a bit mask.
  37. * @see #_makeKeyFromCodeAndModifiers
  38. */
  39. WebInspector.KeyboardShortcut.Modifiers = {
  40. None: 0, // Constant for empty modifiers set.
  41. Shift: 1,
  42. Ctrl: 2,
  43. Alt: 4,
  44. Meta: 8, // Command key on Mac, Win key on other platforms.
  45. get CtrlOrMeta()
  46. {
  47. // "default" command/ctrl key for platform, Command on Mac, Ctrl on other platforms
  48. return WebInspector.isMac() ? this.Meta : this.Ctrl;
  49. }
  50. };
  51. /** @typedef {{code: number, name: (string|Object.<string, string>)}} */
  52. WebInspector.KeyboardShortcut.Key;
  53. /** @type {!Object.<string, !WebInspector.KeyboardShortcut.Key>} */
  54. WebInspector.KeyboardShortcut.Keys = {
  55. Backspace: { code: 8, name: "\u21a4" },
  56. Tab: { code: 9, name: { mac: "\u21e5", other: "<Tab>" } },
  57. Enter: { code: 13, name: { mac: "\u21a9", other: "<Enter>" } },
  58. Esc: { code: 27, name: { mac: "\u238b", other: "<Esc>" } },
  59. Space: { code: 32, name: "<Space>" },
  60. PageUp: { code: 33, name: { mac: "\u21de", other: "<PageUp>" } }, // also NUM_NORTH_EAST
  61. PageDown: { code: 34, name: { mac: "\u21df", other: "<PageDown>" } }, // also NUM_SOUTH_EAST
  62. End: { code: 35, name: { mac: "\u2197", other: "<End>" } }, // also NUM_SOUTH_WEST
  63. Home: { code: 36, name: { mac: "\u2196", other: "<Home>" } }, // also NUM_NORTH_WEST
  64. Left: { code: 37, name: "<Left>" }, // also NUM_WEST
  65. Up: { code: 38, name: "<Up>" }, // also NUM_NORTH
  66. Right: { code: 39, name: "<Right>" }, // also NUM_EAST
  67. Down: { code: 40, name: "<Down>" }, // also NUM_SOUTH
  68. Delete: { code: 46, name: "<Del>" },
  69. Zero: { code: 48, name: "0" },
  70. F1: { code: 112, name: "F1" },
  71. F2: { code: 113, name: "F2" },
  72. F3: { code: 114, name: "F3" },
  73. F4: { code: 115, name: "F4" },
  74. F5: { code: 116, name: "F5" },
  75. F6: { code: 117, name: "F6" },
  76. F7: { code: 118, name: "F7" },
  77. F8: { code: 119, name: "F8" },
  78. F9: { code: 120, name: "F9" },
  79. F10: { code: 121, name: "F10" },
  80. F11: { code: 122, name: "F11" },
  81. F12: { code: 123, name: "F12" },
  82. Semicolon: { code: 186, name: ";" },
  83. Plus: { code: 187, name: "+" },
  84. Comma: { code: 188, name: "," },
  85. Minus: { code: 189, name: "-" },
  86. Period: { code: 190, name: "." },
  87. Slash: { code: 191, name: "/" },
  88. Apostrophe: { code: 192, name: "`" },
  89. SingleQuote: { code: 222, name: "\'" },
  90. H: { code: 72, name: "H" }
  91. };
  92. /**
  93. * Creates a number encoding keyCode in the lower 8 bits and modifiers mask in the higher 8 bits.
  94. * It is useful for matching pressed keys.
  95. *
  96. * @param {number|string} keyCode The Code of the key, or a character "a-z" which is converted to a keyCode value.
  97. * @param {number=} modifiers Optional list of modifiers passed as additional paramerters.
  98. * @return {number}
  99. */
  100. WebInspector.KeyboardShortcut.makeKey = function(keyCode, modifiers)
  101. {
  102. if (typeof keyCode === "string")
  103. keyCode = keyCode.charCodeAt(0) - 32;
  104. modifiers = modifiers || WebInspector.KeyboardShortcut.Modifiers.None;
  105. return WebInspector.KeyboardShortcut._makeKeyFromCodeAndModifiers(keyCode, modifiers);
  106. }
  107. /**
  108. * @param {KeyboardEvent} keyboardEvent
  109. * @return {number}
  110. */
  111. WebInspector.KeyboardShortcut.makeKeyFromEvent = function(keyboardEvent)
  112. {
  113. var modifiers = WebInspector.KeyboardShortcut.Modifiers.None;
  114. if (keyboardEvent.shiftKey)
  115. modifiers |= WebInspector.KeyboardShortcut.Modifiers.Shift;
  116. if (keyboardEvent.ctrlKey)
  117. modifiers |= WebInspector.KeyboardShortcut.Modifiers.Ctrl;
  118. if (keyboardEvent.altKey)
  119. modifiers |= WebInspector.KeyboardShortcut.Modifiers.Alt;
  120. if (keyboardEvent.metaKey)
  121. modifiers |= WebInspector.KeyboardShortcut.Modifiers.Meta;
  122. return WebInspector.KeyboardShortcut._makeKeyFromCodeAndModifiers(keyboardEvent.keyCode, modifiers);
  123. }
  124. /**
  125. * @param {KeyboardEvent} event
  126. * @return {boolean}
  127. */
  128. WebInspector.KeyboardShortcut.eventHasCtrlOrMeta = function(event)
  129. {
  130. return WebInspector.isMac() ? event.metaKey && !event.ctrlKey : event.ctrlKey && !event.metaKey;
  131. }
  132. /**
  133. * @param {KeyboardEvent} event
  134. * @return {boolean}
  135. */
  136. WebInspector.KeyboardShortcut.hasNoModifiers = function(event)
  137. {
  138. return !event.ctrlKey && !event.shiftKey && !event.altKey && !event.metaKey;
  139. }
  140. /** @typedef {{key: number, name: string}} */
  141. WebInspector.KeyboardShortcut.Descriptor;
  142. /**
  143. * @param {string|WebInspector.KeyboardShortcut.Key} key
  144. * @param {number=} modifiers
  145. * @return {WebInspector.KeyboardShortcut.Descriptor}
  146. */
  147. WebInspector.KeyboardShortcut.makeDescriptor = function(key, modifiers)
  148. {
  149. return {
  150. key: WebInspector.KeyboardShortcut.makeKey(typeof key === "string" ? key : key.code, modifiers),
  151. name: WebInspector.KeyboardShortcut.shortcutToString(key, modifiers)
  152. };
  153. }
  154. /**
  155. * @param {string|WebInspector.KeyboardShortcut.Key} key
  156. * @param {number=} modifiers
  157. * @return {string}
  158. */
  159. WebInspector.KeyboardShortcut.shortcutToString = function(key, modifiers)
  160. {
  161. return WebInspector.KeyboardShortcut._modifiersToString(modifiers) + WebInspector.KeyboardShortcut._keyName(key);
  162. }
  163. /**
  164. * @param {string|WebInspector.KeyboardShortcut.Key} key
  165. * @return {string}
  166. */
  167. WebInspector.KeyboardShortcut._keyName = function(key)
  168. {
  169. if (typeof key === "string")
  170. return key.toUpperCase();
  171. if (typeof key.name === "string")
  172. return key.name;
  173. return key.name[WebInspector.platform()] || key.name.other || '';
  174. }
  175. /**
  176. * @param {number} keyCode
  177. * @param {?number} modifiers
  178. * @return {number}
  179. */
  180. WebInspector.KeyboardShortcut._makeKeyFromCodeAndModifiers = function(keyCode, modifiers)
  181. {
  182. return (keyCode & 255) | (modifiers << 8);
  183. };
  184. /**
  185. * @param {number|undefined} modifiers
  186. * @return {string}
  187. */
  188. WebInspector.KeyboardShortcut._modifiersToString = function(modifiers)
  189. {
  190. const cmdKey = "\u2318";
  191. const optKey = "\u2325";
  192. const shiftKey = "\u21e7";
  193. const ctrlKey = "\u2303";
  194. var isMac = WebInspector.isMac();
  195. var res = "";
  196. if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Ctrl)
  197. res += isMac ? ctrlKey : "<Ctrl> + ";
  198. if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Alt)
  199. res += isMac ? optKey : "<Alt> + ";
  200. if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Shift)
  201. res += isMac ? shiftKey : "<Shift> + ";
  202. if (modifiers & WebInspector.KeyboardShortcut.Modifiers.Meta)
  203. res += isMac ? cmdKey : "<Win> + ";
  204. return res;
  205. };
  206. WebInspector.KeyboardShortcut.SelectAll = WebInspector.KeyboardShortcut.makeKey("a", WebInspector.KeyboardShortcut.Modifiers.CtrlOrMeta);