Requires

Provides

Event.js

Some additional methods for keypress implementation that sniff key strokes.

License:
MIT-style license.
  1. 20
  2. 21
  3. 22
  4. 23
  5. 24
  6. 25
  7. 26
  8. 27
  9. 28
Event.Keys = { keyOf: function(code) { return Event.KeyNames[code]; } }; (function() {

borrowed from google closure TODO check with diferent browsers

  1. 32
  2. 33
  3. 34
  4. 35
  5. 36
  6. 37
  7. 38
  8. 39
  9. 40
  10. 41
  11. 42
  12. 43
  13. 44
  14. 45
  15. 46
  16. 47
  17. 48
  18. 49
  19. 50
  20. 51
  21. 52
  22. 53
  23. 54
  24. 55
  25. 56
  26. 57
  27. 58
  28. 59
  29. 60
  30. 61
  31. 62
  32. 63
  33. 64
  34. 65
  35. 66
  36. 67
  37. 68
  38. 69
  39. 70
  40. 71
  41. 72
  42. 73
  43. 74
  44. 75
  45. 76
  46. 77
  47. 78
  48. 79
  49. 80
  50. 81
  51. 82
  52. 83
  53. 84
  54. 85
  55. 86
  56. 87
  57. 88
  58. 89
  59. 90
  60. 91
  61. 92
  62. 93
  63. 94
  64. 95
  65. 96
  66. 97
  67. 98
  68. 99
  69. 100
  70. 101
  71. 102
  72. 103
  73. 104
  74. 105
  75. 106
  76. 107
  77. 108
  78. 109
  79. 110
  80. 111
  81. 112
  82. 113
  83. 114
  84. 115
  85. 116
  86. 117
  87. 118
  88. 119
  89. 120
  90. 121
  91. 122
  92. 123
  93. 124
  94. 125
  95. 126
  96. 127
  97. 128
  98. 129
  99. 130
  100. 131
  101. 132
  102. 133
  103. 134
  104. 135
  105. 136
  106. 137
  107. 138
  108. 139
  109. 140
  110. 141
  111. 142
  112. 143
  113. 144
  114. 145
  115. 146
  116. 147
  117. 148
  118. 149
  119. 150
  120. 151
  121. 152
  122. 153
  123. 154
  124. 155
  125. 156
  126. 157
  127. 158
  128. 159
  129. 160
  130. 161
  131. 162
  132. 163
  133. 164
  134. 165
  135. 166
  136. 167
  137. 168
  138. 169
  139. 170
  140. 171
  141. 172
  142. 173
  143. 174
  144. 175
  145. 176
  146. 177
  147. 178
  148. 179
  149. 180
  150. 181
  151. 182
  152. 183
  153. 184
  154. 185
  155. 186
  156. 187
  157. 188
  158. 189
  159. 190
  160. 191
  161. 192
  162. 193
  163. 194
  164. 195
  165. 196
  166. 197
  167. 198
  168. 199
  169. 200
  170. 201
  171. 202
  172. 203
  173. 204
  174. 205
  175. 206
  176. 207
  177. 208
  178. 209
  179. 210
  180. 211
  181. 212
  182. 213
  183. 214
  184. 215
  185. 216
  186. 217
  187. 218
  188. 219
  189. 220
  190. 221
  191. 222
  192. 223
  193. 224
Browser.Features.keydown = (Browser.ie || Browser.chrome || Browser.safari); Event.KeyNames = { 8: 'backspace', 9: 'tab', 13: 'enter', 16: 'shift', 17: 'control', 18: 'alt', 19: 'pause', 20: 'caps-lock', 27: 'esc', 32: 'space', 33: 'pg-up', 34: 'pg-down', 35: 'end', 36: 'home', 37: 'left', 38: 'up', 39: 'right', 40: 'down', 45: 'insert', 46: 'delete', 48: '0', 49: '1', 50: '2', 51: '3', 52: '4', 53: '5', 54: '6', 55: '7', 56: '8', 57: '9', 65: 'a', 66: 'b', 67: 'c', 68: 'd', 69: 'e', 70: 'f', 71: 'g', 72: 'h', 73: 'i', 74: 'j', 75: 'k', 76: 'l', 77: 'm', 78: 'n', 79: 'o', 80: 'p', 81: 'q', 82: 'r', 83: 's', 84: 't', 85: 'u', 86: 'v', 87: 'w', 88: 'x', 89: 'y', 90: 'z', 93: 'context', 107: 'num-plus', 109: 'num-minus', 112: 'f1', 113: 'f2', 114: 'f3', 115: 'f4', 116: 'f5', 117: 'f6', 118: 'f7', 119: 'f8', 120: 'f9', 121: 'f10', 122: 'f11', 123: 'f12', 187: 'equals', 188: ',', 190: '.', 191: '/', 220: '\\', 224: 'meta' }; Event.Codes = { MAC_ENTER: 3, BACKSPACE: 8, TAB: 9, NUM_CENTER: 12, ENTER: 13, SHIFT: 16, CTRL: 17, ALT: 18, PAUSE: 19, CAPS_LOCK: 20, ESC: 27, SPACE: 32, PAGE_UP: 33, // also NUM_NORTH_EAST PAGE_DOWN: 34, // also NUM_SOUTH_EAST END: 35, // also NUM_SOUTH_WEST HOME: 36, // also NUM_NORTH_WEST LEFT: 37, // also NUM_WEST UP: 38, // also NUM_NORTH RIGHT: 39, // also NUM_EAST DOWN: 40, // also NUM_SOUTH PRINT_SCREEN: 44, INSERT: 45, // also NUM_INSERT DELETE: 46, // also NUM_DELETE ZERO: 48, ONE: 49, TWO: 50, THREE: 51, FOUR: 52, FIVE: 53, SIX: 54, SEVEN: 55, EIGHT: 56, NINE: 57, QUESTION_MARK: 63, // needs localization A: 65, B: 66, C: 67, D: 68, E: 69, F: 70, G: 71, H: 72, I: 73, J: 74, K: 75, L: 76, M: 77, N: 78, O: 79, P: 80, Q: 81, R: 82, S: 83, T: 84, U: 85, V: 86, W: 87, X: 88, Y: 89, Z: 90, META: 91, CONTEXT_MENU: 93, NUM_ZERO: 96, NUM_ONE: 97, NUM_TWO: 98, NUM_THREE: 99, NUM_FOUR: 100, NUM_FIVE: 101, NUM_SIX: 102, NUM_SEVEN: 103, NUM_EIGHT: 104, NUM_NINE: 105, NUM_MULTIPLY: 106, NUM_PLUS: 107, NUM_MINUS: 109, NUM_PERIOD: 110, NUM_DIVISION: 111, F1: 112, F2: 113, F3: 114, F4: 115, F5: 116, F6: 117, F7: 118, F8: 119, F9: 120, F10: 121, F11: 122, F12: 123, NUMLOCK: 144, SEMICOLON: 186, DASH: 189, EQUALS: 187, COMMA: 188, PERIOD: 190, SLASH: 191, APOSTROPHE: 192, SINGLE_QUOTE: 222, OPEN_SQUARE_BRACKET: 219, BACKSLASH: 220, CLOSE_SQUARE_BRACKET: 221, META_KEY: 224, MAC_FF_META: 224, // Firefox (Gecko) fires this for the meta key instead of 91 WIN_IME: 229 }; Event.implement({ isTextModifyingKeyEvent: function(e) { if (this.alt && this.control || this.meta ||

Function keys don’t generate text

  1. 226
  2. 227
  3. 228
  4. 229
this.code >= Event.Codes.F1 && this.code <= Event.Codes.F12) { return false; }

The following keys are quite harmless, even in combination with CTRL, ALT or SHIFT.

  1. 233
  2. 234
  3. 235
  4. 236
  5. 237
  6. 238
  7. 239
  8. 240
  9. 241
  10. 242
  11. 243
  12. 244
  13. 245
  14. 246
  15. 247
  16. 248
  17. 249
  18. 250
  19. 251
  20. 252
  21. 253
  22. 254
  23. 255
  24. 256
  25. 257
  26. 258
  27. 259
  28. 260
  29. 261
  30. 262
  31. 263
  32. 264
  33. 265
switch (this.code) { case Event.Codes.ALT: case Event.Codes.SHIFT: case Event.Codes.CTRL: case Event.Codes.PAUSE: case Event.Codes.CAPS_LOCK: case Event.Codes.ESC: case Event.Codes.PAGE_UP: case Event.Codes.PAGE_DOWN: case Event.Codes.HOME: case Event.Codes.END: case Event.Codes.LEFT: case Event.Codes.RIGHT: case Event.Codes.UP: case Event.Codes.DOWN: case Event.Codes.INSERT: case Event.Codes.NUMLOCK: case Event.Codes.CONTEXT_MENU: case Event.Codes.PRINT_SCREEN: return false; default: return true; } }, firesKeyPressEvent: function(held) { if (!Browser.Features.keydown) { return true; } if (Browser.Platform.mac && this.alt) { return Event.Codes.isCharacterKey(this.code); }

Alt but not AltGr which is represented as Alt+Ctrl.

  1. 268
  2. 269
  3. 270
if (this.alt && !this.control) { return false; }

Saves Ctrl or Alt + key for IE7, which won’t fire keypress.

  1. 273
  2. 274
  3. 275
  4. 276
  5. 277
  6. 278
if (Browser.Engine.trident && !this.shift && (held == Event.Codes.CTRL || held == Event.Codes.ALT)) { return false; }

When Ctrl+ is held in IE, it only fires a keypress once, but it continues to fire keydown events as the event repeats.

  1. 282
  2. 283
  3. 284
  4. 285
  5. 286
  6. 287
  7. 288
  8. 289
  9. 290
  10. 291
  11. 292
  12. 293
  13. 294
  14. 295
  15. 296
  16. 297
  17. 298
  18. 299
  19. 300
  20. 301
  21. 302
  22. 303
  23. 304
  24. 305
  25. 306
  26. 307
  27. 308
  28. 309
  29. 310
  30. 311
  31. 312
  32. 313
  33. 314
  34. 315
  35. 316
  36. 317
  37. 318
  38. 319
  39. 320
  40. 321
  41. 322
  42. 323
  43. 324
  44. 325
  45. 326
  46. 327
  47. 328
  48. 329
  49. 330
  50. 331
  51. 332
  52. 333
  53. 334
  54. 335
  55. 336
if (Browser.Engine.trient && this.control && held == this.code) { return false; } switch (this.code) { case Event.Codes.ENTER: return true; case Event.Codes.ESC: return !Browser.Engine.webkit; } return this.isCharacterKey(); }, isCharacterKey: function(code) { if (this.code >= Event.Codes.ZERO && this.code <= Event.Codes.NINE) { return true; } if (this.code >= Event.Codes.NUM_ZERO && this.code <= Event.Codes.NUM_MULTIPLY) { return true; } if (this.code >= Event.Codes.A && this.code <= Event.Codes.Z) { return true; } switch (this.code) { case Event.Codes.SPACE: case Event.Codes.QUESTION_MARK: case Event.Codes.NUM_PLUS: case Event.Codes.NUM_MINUS: case Event.Codes.NUM_PERIOD: case Event.Codes.NUM_DIVISION: case Event.Codes.SEMICOLON: case Event.Codes.DASH: case Event.Codes.EQUALS: case Event.Codes.COMMA: case Event.Codes.PERIOD: case Event.Codes.SLASH: case Event.Codes.APOSTROPHE: case Event.Codes.SINGLE_QUOTE: case Event.Codes.OPEN_SQUARE_BRACKET: case Event.Codes.BACKSLASH: case Event.Codes.CLOSE_SQUARE_BRACKET: return true; default: return false; } } }); })();