/AppKit/Platform/DOM/CPPlatformWindow+DOMKeys.j

http://github.com/cacaodev/cappuccino · Unknown · 232 lines · 216 code · 16 blank · 0 comment · 0 complexity · 2ccf73537c2bbf0c75f636b49dd304e6 MD5 · raw file

  1. /*
  2. * CPPlatformWindow+DOMKeys.j
  3. * AppKit
  4. *
  5. * Created by Ross Boucher.
  6. * Copyright 2009, 280 North, Inc.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. */
  22. // Keycodes taken and modified from Google Closure, available under Apache 2 License
  23. CPKeyCodes = {
  24. BACKSPACE: 8,
  25. TAB: 9,
  26. NUM_CENTER: 12,
  27. ENTER: 13,
  28. SHIFT: 16,
  29. CTRL: 17,
  30. ALT: 18,
  31. PAUSE: 19,
  32. CAPS_LOCK: 20,
  33. ESC: 27,
  34. SPACE: 32,
  35. PAGE_UP: 33, // also NUM_NORTH_EAST
  36. PAGE_DOWN: 34, // also NUM_SOUTH_EAST
  37. END: 35, // also NUM_SOUTH_WEST
  38. HOME: 36, // also NUM_NORTH_WEST
  39. LEFT: 37, // also NUM_WEST
  40. UP: 38, // also NUM_NORTH
  41. RIGHT: 39, // also NUM_EAST
  42. DOWN: 40, // also NUM_SOUTH
  43. PRINT_SCREEN: 44,
  44. INSERT: 45, // also NUM_INSERT
  45. DELETE: 46, // also NUM_DELETE
  46. ZERO: 48,
  47. ONE: 49,
  48. TWO: 50,
  49. THREE: 51,
  50. FOUR: 52,
  51. FIVE: 53,
  52. SIX: 54,
  53. SEVEN: 55,
  54. EIGHT: 56,
  55. NINE: 57,
  56. QUESTION_MARK: 63, // needs localization
  57. A: 65,
  58. B: 66,
  59. C: 67,
  60. D: 68,
  61. E: 69,
  62. F: 70,
  63. G: 71,
  64. H: 72,
  65. I: 73,
  66. J: 74,
  67. K: 75,
  68. L: 76,
  69. M: 77,
  70. N: 78,
  71. O: 79,
  72. P: 80,
  73. Q: 81,
  74. R: 82,
  75. S: 83,
  76. T: 84,
  77. U: 85,
  78. V: 86,
  79. W: 87,
  80. X: 88,
  81. Y: 89,
  82. Z: 90,
  83. META: 91,
  84. WEBKIT_RIGHT_META: 93, // WebKit (on Mac at least) fires this for the right Command key
  85. CONTEXT_MENU: 93,
  86. NUM_ZERO: 96,
  87. NUM_ONE: 97,
  88. NUM_TWO: 98,
  89. NUM_THREE: 99,
  90. NUM_FOUR: 100,
  91. NUM_FIVE: 101,
  92. NUM_SIX: 102,
  93. NUM_SEVEN: 103,
  94. NUM_EIGHT: 104,
  95. NUM_NINE: 105,
  96. NUM_MULTIPLY: 106,
  97. NUM_PLUS: 107,
  98. NUM_MINUS: 109,
  99. NUM_PERIOD: 110,
  100. NUM_DIVISION: 111,
  101. F1: 112,
  102. F2: 113,
  103. F3: 114,
  104. F4: 115,
  105. F5: 116,
  106. F6: 117,
  107. F7: 118,
  108. F8: 119,
  109. F9: 120,
  110. F10: 121,
  111. F11: 122,
  112. F12: 123,
  113. NUMLOCK: 144,
  114. SEMICOLON: 186, // needs localization
  115. DASH: 189, // needs localization
  116. EQUALS: 187, // needs localization
  117. COMMA: 188, // needs localization
  118. PERIOD: 190, // needs localization
  119. SLASH: 191, // needs localization
  120. APOSTROPHE: 192, // needs localization
  121. SINGLE_QUOTE: 222, // needs localization
  122. OPEN_SQUARE_BRACKET: 219, // needs localization
  123. BACKSLASH: 220, // needs localization
  124. CLOSE_SQUARE_BRACKET: 221, // needs localization
  125. WIN_KEY: 224,
  126. MAC_FF_META: 224, // Firefox (Gecko) fires this for the meta key instead of 91
  127. WIN_IME: 229
  128. };
  129. /*!
  130. * Returns true if the key fires a keypress event in the current browser.
  131. *
  132. * Accoridng to MSDN [1] IE only fires keypress events for the following keys:
  133. * - Letters: A - Z (uppercase and lowercase)
  134. * - Numerals: 0 - 9
  135. * - Symbols: ! @ # $ % ^ & * ( ) _ - + = < [ ] { } , . / ? \ | ' ` " ~
  136. * - System: ESC, SPACEBAR, ENTER
  137. *
  138. * That's not entirely correct though, for instance there's no distinction
  139. * between upper and lower case letters.
  140. *
  141. * [1] http://msdn2.microsoft.com/en-us/library/ms536939(VS.85).aspx)
  142. *
  143. * Safari is similar to IE, but does not fire keypress for ESC.
  144. *
  145. * Additionally, IE6 does not fire keydown or keypress events for letters when
  146. * the control or alt keys are held down and the shift key is not. IE7 does
  147. * fire keydown in these cases, though, but not keypress.
  148. *
  149. * @param keyCode A key code.
  150. * @param opt_heldKeyCode Key code of a currently-held key.
  151. * @param opt_shiftKey Whether the shift key is held down.
  152. * @param opt_ctrlKey Whether the control key is held down.
  153. * @param opt_altKey Whether the alt key is held down.
  154. * @return Returns YES if it's a key that fires a keypress event.
  155. */
  156. CPKeyCodes.firesKeyPressEvent = function(keyCode, opt_heldKeyCode, opt_shiftKey, opt_ctrlKey, opt_altKey)
  157. {
  158. if (!CPFeatureIsCompatible(CPJavaScriptRemedialKeySupport))
  159. return true;
  160. if (CPBrowserIsOperatingSystem(CPMacOperatingSystem) && opt_altKey)
  161. return CPKeyCodes.isCharacterKey(keyCode);
  162. // Alt but not AltGr which is represented as Alt+Ctrl.
  163. if (opt_altKey && !opt_ctrlKey)
  164. return false;
  165. // Saves Ctrl or Alt + key for IE7, which won't fire keypress.
  166. if (CPBrowserIsEngine(CPInternetExplorerBrowserEngine) && !opt_shiftKey && (opt_ctrlKey || opt_altKey))
  167. return false;
  168. // When Ctrl+<somekey> is held in IE, it only fires a keypress once, but it
  169. // continues to fire keydown events as the event repeats.
  170. if (CPBrowserIsEngine(CPInternetExplorerBrowserEngine) && opt_ctrlKey && opt_heldKeyCode == keyCode)
  171. return false;
  172. switch (keyCode)
  173. {
  174. case CPKeyCodes.ENTER: return true;
  175. case CPKeyCodes.ESC: return !CPBrowserIsEngine(CPWebKitBrowserEngine);
  176. }
  177. return CPKeyCodes.isCharacterKey(keyCode);
  178. };
  179. /*!
  180. * Test for whether or not a given keyCode represents a character key.
  181. *
  182. * @param keyCode A key code.
  183. * @return Returns YES if the keyCode is a character key.
  184. */
  185. CPKeyCodes.isCharacterKey = function(keyCode)
  186. {
  187. if (keyCode >= CPKeyCodes.ZERO && keyCode <= CPKeyCodes.NINE)
  188. return true;
  189. if (keyCode >= CPKeyCodes.NUM_ZERO && keyCode <= CPKeyCodes.NUM_MULTIPLY)
  190. return true;
  191. if (keyCode >= CPKeyCodes.A && keyCode <= CPKeyCodes.Z)
  192. return true;
  193. switch (keyCode)
  194. {
  195. case CPKeyCodes.SPACE:
  196. case CPKeyCodes.QUESTION_MARK:
  197. case CPKeyCodes.NUM_PLUS:
  198. case CPKeyCodes.NUM_MINUS:
  199. case CPKeyCodes.NUM_PERIOD:
  200. case CPKeyCodes.NUM_DIVISION:
  201. case CPKeyCodes.SEMICOLON:
  202. case CPKeyCodes.DASH:
  203. case CPKeyCodes.EQUALS:
  204. case CPKeyCodes.COMMA:
  205. case CPKeyCodes.PERIOD:
  206. case CPKeyCodes.SLASH:
  207. case CPKeyCodes.APOSTROPHE:
  208. case CPKeyCodes.SINGLE_QUOTE:
  209. case CPKeyCodes.OPEN_SQUARE_BRACKET:
  210. case CPKeyCodes.BACKSLASH:
  211. case CPKeyCodes.CLOSE_SQUARE_BRACKET:
  212. return true;
  213. default:
  214. return false;
  215. }
  216. };