character_editor 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a5ad19831f0795d9dd9793f4190793310bf16afa
4
- data.tar.gz: 86b9249162fbe1d41a923182e0d59b91bfb27fcc
3
+ metadata.gz: 9662d1c745ffafb4bb1cd74235e347aa6e834cbc
4
+ data.tar.gz: bbc150d7df4a61fb6b06b023182ae93300e52945
5
5
  SHA512:
6
- metadata.gz: 8f83c5a859d7d59869c9ff90b1ea7ad5058173663d277e67f41d2786d84eb4968b7e339caa71ab3cb622c8e488446d65f12f31b20addc6930602f3ce6af244a5
7
- data.tar.gz: 000a86a218869baf3d6b98aee80586750f36351f008f4303be0dae39f8cec188bb29c5b29a96bb1fb9988a8949abc9762a12c69debcf3c2dedd91cbd46c2646e
6
+ metadata.gz: 2bd4823e0cfadd7747d2150ba3d278a9015dcc3e848211fbbda8d648d8ac51f06792d9e49db791b85fddc8ce3c2227030a97f2c4ad0fcb07e61621bd3279f98b
7
+ data.tar.gz: cbc2a63b876e7b52de73149f8a5d2da397e49bc9ea5e02eae562411dbd60392dbbb8a2f48ff685923b75e8764f11170417e63db7b2bdcebaf00537736f9683eb
@@ -5,7 +5,7 @@ Gem::Specification.new do |gem|
5
5
  gem.name = 'character_editor'
6
6
  gem.version = Character::Editor::VERSION
7
7
  gem.summary = 'Character WYSIWYG editor + Redactor.js'
8
- gem.license = 'MIT but not for Redactor.js'
8
+ gem.license = ''
9
9
 
10
10
  gem.authors = ['Alexander Kravets']
11
11
  gem.email = 'alex@slatestudio.com'
@@ -18,4 +18,4 @@ Gem::Specification.new do |gem|
18
18
 
19
19
  # Supress the warning about no rubyforge project
20
20
  gem.rubyforge_project = 'nowarning'
21
- end
21
+ end
@@ -1,5 +1,5 @@
1
1
  module Character
2
2
  module Editor
3
- VERSION = '0.1.3'
3
+ VERSION = '0.1.4'
4
4
  end
5
5
  end
@@ -0,0 +1,888 @@
1
+ ###
2
+ Copyright 2014 David Mauro
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+
16
+ Keypress is a robust keyboard input capturing Javascript utility
17
+ focused on input for games.
18
+
19
+ version 2.0.3
20
+ ###
21
+
22
+ ###
23
+ Combo options available and their defaults:
24
+ keys : [] - An array of the keys pressed together to activate combo.
25
+ count : 0 - The number of times a counting combo has been pressed. Reset on release.
26
+ is_unordered : false - Unless this is set to true, the keys can be pressed down in any order.
27
+ is_counting : false - Makes this a counting combo (see documentation).
28
+ is_exclusive : false - This combo will replace other exclusive combos when true.
29
+ is_solitary : false - This combo will only fire if ONLY it's keys are pressed down.
30
+ is_sequence : false - Rather than a key combo, this is an ordered key sequence.
31
+ prevent_default : false - Prevent default behavior for all component key keypresses.
32
+ prevent_repeat : false - Prevent the combo from repeating when keydown is held.
33
+ on_keydown : null - A function that is called when the combo is pressed.
34
+ on_keyup : null - A function that is called when the combo is released.
35
+ on_release : null - A function that is called when all keys in the combo are released.
36
+ this : undefined - Defines the scope for your callback functions.
37
+ ###
38
+
39
+ ###########
40
+ # Constants
41
+ ###########
42
+
43
+ _factory_defaults =
44
+ is_unordered : false
45
+ is_counting : false
46
+ is_exclusive : false
47
+ is_solitary : false
48
+ prevent_default : false
49
+ prevent_repeat : false
50
+
51
+ _modifier_keys = ["meta", "alt", "option", "ctrl", "shift", "cmd"]
52
+
53
+ _metakey = "ctrl"
54
+
55
+ ###########################
56
+ # Public object and Classes
57
+ ###########################
58
+
59
+ keypress = {}
60
+
61
+ keypress.debug = false
62
+
63
+ class Combo
64
+ constructor: (dictionary) ->
65
+ # Copy over any non-false values
66
+ for own property, value of dictionary
67
+ @[property] = value if value != false
68
+
69
+ # Standard Defaults
70
+ @keys = @keys or []
71
+ @count = @count or 0
72
+
73
+ allows_key_repeat: ->
74
+ # Combos with keydown functions should be able to rapid fire
75
+ # when holding down the key for an extended period
76
+ return not @prevent_repeat and typeof @on_keydown is "function"
77
+
78
+ reset: ->
79
+ @count = 0
80
+ @keyup_fired = null
81
+
82
+ class keypress.Listener
83
+ constructor:(element, defaults) ->
84
+ # Public properties
85
+ @should_suppress_event_defaults = false
86
+ @should_force_event_defaults = false
87
+ @sequence_delay = 800
88
+
89
+ # Private properties
90
+ @_registered_combos = []
91
+ @_keys_down = []
92
+ @_active_combos = []
93
+ @_sequence = []
94
+ @_sequence_timer = null
95
+ @_prevent_capture = false
96
+ @_defaults = defaults or {}
97
+ for own property, value of _factory_defaults
98
+ @_defaults[property] = @_defaults[property] or value
99
+
100
+ # Attach handlers to element
101
+ element = element or document.body
102
+
103
+ attach_handler = (target, event, handler) ->
104
+ if target.addEventListener
105
+ target.addEventListener event, handler
106
+ else if target.attachEvent
107
+ target.attachEvent "on#{event}", handler
108
+
109
+ attach_handler element, "keydown", (e) =>
110
+ e = e or window.event
111
+ @_receive_input e, true
112
+ @_bug_catcher e
113
+ attach_handler element, "keyup", (e) =>
114
+ e = e or window.event
115
+ @_receive_input e, false
116
+ attach_handler window, "blur", =>
117
+ # Assume all keys are released when we can't catch key events
118
+ # This prevents alt+tab conflicts
119
+ for key in @_keys_down
120
+ @_key_up key, {}
121
+ @_keys_down = []
122
+
123
+ # Helper Methods
124
+
125
+ _bug_catcher: (e) ->
126
+ # This seems to be Mac specific weirdness, so we'll target "cmd" as metaKey
127
+ # Force a keyup for non-modifier keys when command is held because they don't fire
128
+ if _metakey is "cmd" and "cmd" in @_keys_down and _convert_key_to_readable(e.keyCode) not in ["cmd", "shift", "alt", "caps", "tab"]
129
+ @_receive_input e, false
130
+ # Note: we're currently ignoring the fact that this doesn't catch the bug that a keyup
131
+ # will not fire if you keydown a combo, then press and hold cmd, then keyup the combo.
132
+ # Perhaps we should fire keyup on all active combos when we press cmd?
133
+
134
+ _cmd_bug_check: (combo_keys) ->
135
+ # We don't want to allow combos to activate if the cmd key
136
+ # is pressed, but cmd isn't in them. This is so they don't
137
+ # accidentally rapid fire due to our hack-around for the cmd
138
+ # key bug and having to fake keyups.
139
+ if _metakey is "cmd" and "cmd" in @_keys_down and "cmd" not in combo_keys
140
+ return false
141
+ return true
142
+
143
+ _prevent_default: (e, should_prevent) ->
144
+ # If we've pressed a combo, or if we are working towards
145
+ # one, we should prevent the default keydown event.
146
+ if (should_prevent or @should_suppress_event_defaults) and not @should_force_event_defaults
147
+ if e.preventDefault then e.preventDefault() else e.returnValue = false
148
+ e.stopPropagation() if e.stopPropagation
149
+
150
+ # Tracking Combos
151
+
152
+ _get_active_combos: (key) ->
153
+ # Based on the keys_down and the key just pressed or released
154
+ # (which should not be in keys_down), we determine if any
155
+ # combo in registered_combos could be considered active.
156
+ # This will return an array of active combos
157
+
158
+ active_combos = []
159
+
160
+ # First check that every key in keys_down maps to a combo
161
+ keys_down = _filter_array @_keys_down, (down_key) ->
162
+ down_key isnt key
163
+ keys_down.push key
164
+
165
+ # Get perfect matches
166
+ @_match_combo_arrays keys_down, (match) =>
167
+ active_combos.push(match) if @_cmd_bug_check match.keys
168
+
169
+ # Get fuzzy matches
170
+ @_fuzzy_match_combo_arrays keys_down, (match) =>
171
+ return if match in active_combos
172
+ active_combos.push(match) unless match.is_solitary or not @_cmd_bug_check match.keys
173
+
174
+ return active_combos
175
+
176
+ _get_potential_combos: (key) ->
177
+ # Check if we are working towards pressing a combo.
178
+ # Used for preventing default on keys that might match
179
+ # to a combo in the future.
180
+ potentials = []
181
+ for combo in @_registered_combos
182
+ continue if combo.is_sequence
183
+ potentials.push(combo) if key in combo.keys and @_cmd_bug_check combo.keys
184
+ return potentials
185
+
186
+ _add_to_active_combos: (combo) ->
187
+ should_replace = false
188
+ should_prepend = true
189
+ already_replaced = false
190
+ # An active combo is any combo which the user has already entered.
191
+ # We use this to track when a user has released the last key of a
192
+ # combo for on_release, and to keep combos from 'overlapping'.
193
+ if combo in @_active_combos
194
+ return true
195
+ else if @_active_combos.length
196
+ # We have to check if we're replacing another active combo
197
+ # So compare the combo.keys to all active combos' keys.
198
+ for i in [0...@_active_combos.length]
199
+ active_combo = @_active_combos[i]
200
+ continue unless active_combo and active_combo.is_exclusive and combo.is_exclusive
201
+ active_keys = active_combo.keys
202
+ unless should_replace
203
+ for active_key in active_keys
204
+ should_replace = true
205
+ unless active_key in combo.keys
206
+ should_replace = false
207
+ break
208
+
209
+ if should_prepend and not should_replace
210
+ for combo_key in combo.keys
211
+ should_prepend = false
212
+ unless combo_key in active_keys
213
+ should_prepend = true
214
+ break
215
+
216
+ if should_replace
217
+ if already_replaced
218
+ active_combo = @_active_combos.splice(i, 1)[0]
219
+ active_combo.reset() if active_combo?
220
+ else
221
+ active_combo = @_active_combos.splice(i, 1, combo)[0]
222
+ active_combo.reset() if active_combo?
223
+ already_replaced = true
224
+ should_prepend = false
225
+ if should_prepend
226
+ @_active_combos.unshift combo
227
+
228
+ return should_replace or should_prepend
229
+
230
+ _remove_from_active_combos: (combo) ->
231
+ for i in [0...@_active_combos.length]
232
+ active_combo = @_active_combos[i]
233
+ if active_combo is combo
234
+ combo = @_active_combos.splice(i, 1)[0]
235
+ combo.reset()
236
+ break
237
+ return
238
+
239
+ # Sequence Methods
240
+
241
+ _get_possible_sequences: ->
242
+ # Determine what if any sequences we're working towards.
243
+ # We will consider any which any part of the end of the sequence
244
+ # matches and return all of them.
245
+ matches = []
246
+ for combo in @_registered_combos
247
+ for j in [1..@_sequence.length]
248
+ sequence = @_sequence.slice -j
249
+ continue unless combo.is_sequence
250
+ unless "shift" in combo.keys
251
+ sequence = _filter_array sequence, (key) ->
252
+ return key isnt "shift"
253
+ continue unless sequence.length
254
+ for i in [0...sequence.length]
255
+ if combo.keys[i] is sequence[i]
256
+ match = true
257
+ else
258
+ match = false
259
+ break
260
+ matches.push(combo) if match
261
+ return matches
262
+
263
+ _add_key_to_sequence: (key, e) ->
264
+ @_sequence.push key
265
+ # Now check if they're working towards a sequence
266
+ sequence_combos = @_get_possible_sequences()
267
+ if sequence_combos.length
268
+ for combo in sequence_combos
269
+ @_prevent_default e, combo.prevent_default
270
+ # If we're working towards one, give them more time to keep going
271
+ clearTimeout(@_sequence_timer) if @_sequence_timer
272
+ if @sequence_delay > -1
273
+ @_sequence_timer = setTimeout ->
274
+ @_sequence = []
275
+ , @sequence_delay
276
+ else
277
+ # If we're not working towards something, just clear it out
278
+ @_sequence = []
279
+ return
280
+
281
+ _get_sequence: (key) ->
282
+ # Compare _sequence to all combos
283
+ for combo in @_registered_combos
284
+ continue unless combo.is_sequence
285
+ for j in [1..@_sequence.length]
286
+ # As we are traversing backwards through the sequence keys,
287
+ # Take out any shift keys, unless shift is in the combo.
288
+ sequence = (_filter_array @_sequence, (seq_key) ->
289
+ return true if "shift" in combo.keys
290
+ return seq_key isnt "shift"
291
+ ).slice -j
292
+ continue unless combo.keys.length is sequence.length
293
+ for i in [0...sequence.length]
294
+ seq_key = sequence[i]
295
+ # Special case for shift. Ignore shift keys, unless the sequence explicitly uses them
296
+ continue if seq_key is "shift" unless "shift" in combo.keys
297
+ # Don't select this combo if we're pressing shift and shift isn't in it
298
+ continue if key is "shift" and "shift" not in combo.keys
299
+ if combo.keys[i] is seq_key
300
+ match = true
301
+ else
302
+ match = false
303
+ break
304
+ return combo if match
305
+ return false
306
+
307
+ # Catching Combos
308
+
309
+ _receive_input: (e, is_keydown) ->
310
+ # If we're not capturing input, we should
311
+ # clear out _keys_down for good measure
312
+ if @_prevent_capture
313
+ @_keys_down = [] if @_keys_down.length
314
+ return
315
+ key = _convert_key_to_readable e.keyCode
316
+ # Catch tabbing out of a non-capturing state
317
+ if !is_keydown and !@_keys_down.length and key in ["alt", _metakey]
318
+ return
319
+ return unless key
320
+ if is_keydown
321
+ @_key_down key, e
322
+ else
323
+ @_key_up key, e
324
+
325
+ _fire: (event, combo, key_event, is_autorepeat) ->
326
+ # Only fire this event if the function is defined
327
+ if typeof combo["on_" + event] is "function"
328
+ @_prevent_default key_event, (combo["on_" + event].call(combo.this, key_event, combo.count, is_autorepeat) isnt true)
329
+ # We need to mark that keyup has already happened
330
+ if event is "release"
331
+ combo.count = 0
332
+ if event is "keyup"
333
+ combo.keyup_fired = true
334
+
335
+ _match_combo_arrays: (potential_match, match_handler) ->
336
+ # This will return all combos that match
337
+ for source_combo in @_registered_combos
338
+ if (not source_combo.is_unordered and _compare_arrays_sorted(potential_match, source_combo.keys)) or (source_combo.is_unordered and _compare_arrays(potential_match, source_combo.keys))
339
+ match_handler source_combo
340
+ return
341
+
342
+ _fuzzy_match_combo_arrays: (potential_match, match_handler) ->
343
+ # This will return combos that match even if other keys are pressed
344
+ for source_combo in @_registered_combos
345
+ if (not source_combo.is_unordered and _is_array_in_array_sorted(source_combo.keys, potential_match)) or (source_combo.is_unordered and _is_array_in_array(source_combo.keys, potential_match))
346
+ match_handler source_combo
347
+ return
348
+
349
+ _keys_remain: (combo) ->
350
+ for key in combo.keys
351
+ if key in @_keys_down
352
+ keys_remain = true
353
+ break
354
+ return keys_remain
355
+
356
+ _key_down: (key, e) ->
357
+ # Check if we're holding shift
358
+ shifted_key = _convert_to_shifted_key key, e
359
+ key = shifted_key if shifted_key
360
+
361
+ # Add the key to sequences
362
+ @_add_key_to_sequence key, e
363
+ sequence_combo = @_get_sequence key
364
+ @_fire("keydown", sequence_combo, e) if sequence_combo
365
+
366
+ # We might have modifier keys down when coming back to
367
+ # this window and they might not be in _keys_down, so
368
+ # we're doing a check to make sure we put it back in.
369
+ # This only works for explicit modifier keys.
370
+ for mod, event_mod of _modifier_event_mapping
371
+ continue unless e[event_mod]
372
+ continue if mod is key or mod in @_keys_down
373
+ @_keys_down.push mod
374
+ # Alternatively, we might not have modifier keys down
375
+ # that we think are, so we should catch those too
376
+ for mod, event_mod of _modifier_event_mapping
377
+ continue if mod is key
378
+ if mod in @_keys_down and not e[event_mod]
379
+ # The Windows key will think it is the cmd key, but won't trigger the event mod
380
+ continue if mod is "cmd" and _metakey isnt "cmd"
381
+ for i in [0...@_keys_down.length]
382
+ @_keys_down.splice(i, 1) if @_keys_down[i] is mod
383
+
384
+ # Find which combos we have pressed or might be working towards, and prevent default
385
+ combos = @_get_active_combos key
386
+ potential_combos = @_get_potential_combos key
387
+ for combo in combos
388
+ @_handle_combo_down combo, potential_combos, key, e
389
+ if potential_combos.length
390
+ for potential in potential_combos
391
+ @_prevent_default e, potential.prevent_default
392
+
393
+ if key not in @_keys_down
394
+ @_keys_down.push key
395
+ return
396
+
397
+ _handle_combo_down: (combo, potential_combos, key, e) ->
398
+ # Make sure we're not trying to fire for a combo that already fired
399
+ return false unless key in combo.keys
400
+
401
+ @_prevent_default e, (combo and combo.prevent_default)
402
+
403
+ is_autorepeat = false
404
+ # If we've already pressed this key, check that we want to fire
405
+ # again, otherwise just add it to the keys_down list.
406
+ if key in @_keys_down
407
+ is_autorepeat = true
408
+ return false unless combo.allows_key_repeat()
409
+
410
+ # Now we add this combo or replace it in _active_combos
411
+ result = @_add_to_active_combos combo, key
412
+
413
+ # We reset the keyup_fired property because you should be
414
+ # able to fire that again, if you've pressed the key down again
415
+ combo.keyup_fired = false
416
+
417
+ # Now we fire the keydown event unless there is a larger exclusive potential combo
418
+ is_other_exclusive = false
419
+ if combo.is_exclusive
420
+ for potential_combo in potential_combos
421
+ if potential_combo.is_exclusive and potential_combo.keys.length > combo.keys.length
422
+ is_other_exclusive = true
423
+ break
424
+
425
+ unless is_other_exclusive
426
+ if combo.is_counting and typeof combo.on_keydown is "function"
427
+ combo.count += 1
428
+
429
+ # Only fire keydown if we added it
430
+ if result
431
+ @_fire "keydown", combo, e, is_autorepeat
432
+
433
+ _key_up: (key, e) ->
434
+ # Check if we're holding shift
435
+ unshifted_key = key
436
+ shifted_key = _convert_to_shifted_key key, e
437
+ key = shifted_key if shifted_key
438
+ shifted_key = _keycode_shifted_keys[unshifted_key]
439
+ # We have to make sure the key matches to what we had in _keys_down
440
+ if e.shiftKey
441
+ key = unshifted_key unless shifted_key and shifted_key in @_keys_down
442
+ else
443
+ key = shifted_key unless unshifted_key and unshifted_key in @_keys_down
444
+
445
+ # Check if we have a keyup firing
446
+ sequence_combo = @_get_sequence key
447
+ @_fire("keyup", sequence_combo, e) if sequence_combo
448
+
449
+ # Remove from the list
450
+ return false unless key in @_keys_down
451
+ for i in [0...@_keys_down.length]
452
+ if @_keys_down[i] in [key, shifted_key, unshifted_key]
453
+ @_keys_down.splice i, 1
454
+ break
455
+
456
+ # Store this for later cleanup
457
+ active_combos_length = @_active_combos.length
458
+
459
+ # When releasing we should only check if we
460
+ # match from _active_combos so that we don't
461
+ # accidentally fire for a combo that was a
462
+ # smaller part of the one we actually wanted.
463
+ combos = []
464
+ for active_combo in @_active_combos
465
+ if key in active_combo.keys
466
+ combos.push active_combo
467
+ for combo in combos
468
+ @_handle_combo_up combo, e, key
469
+
470
+ # We also need to check other combos that might still be in active_combos
471
+ # and needs to be removed from it.
472
+ if active_combos_length > 1
473
+ for active_combo in @_active_combos
474
+ continue if active_combo is undefined or active_combo in combos
475
+ unless @_keys_remain active_combo
476
+ @_remove_from_active_combos active_combo
477
+ return
478
+
479
+ _handle_combo_up: (combo, e, key) ->
480
+ @_prevent_default e, (combo and combo.prevent_default)
481
+
482
+ # Check if any keys from this combo are still being held.
483
+ keys_remaining = @_keys_remain combo
484
+
485
+ # Any unactivated combos will fire
486
+ if !combo.keyup_fired
487
+ # And we should not fire it if it is a solitary combo and something else is pressed
488
+ keys_down = @_keys_down.slice()
489
+ keys_down.push key
490
+ if not combo.is_solitary or _compare_arrays keys_down, combo.keys
491
+ @_fire "keyup", combo, e
492
+ # Dont' add to the count unless we only have a keyup callback
493
+ if combo.is_counting and typeof combo.on_keyup is "function" and typeof combo.on_keydown isnt "function"
494
+ combo.count += 1
495
+
496
+ # If this was the last key released of the combo, clean up.
497
+ unless keys_remaining
498
+ @_fire "release", combo, e
499
+ @_remove_from_active_combos combo
500
+ return
501
+
502
+ # Public Registration Methods
503
+
504
+ simple_combo: (keys, callback) ->
505
+ # Shortcut for simple combos.
506
+ @register_combo(
507
+ keys : keys
508
+ on_keydown : callback
509
+ )
510
+
511
+ counting_combo: (keys, count_callback) ->
512
+ # Shortcut for counting combos
513
+ @register_combo(
514
+ keys : keys
515
+ is_counting : true
516
+ is_unordered : false
517
+ on_keydown : count_callback
518
+ )
519
+
520
+ sequence_combo: (keys, callback) ->
521
+ @register_combo(
522
+ keys : keys
523
+ on_keydown : callback
524
+ is_sequence : true
525
+ )
526
+
527
+ register_combo: (combo_dictionary) ->
528
+ # Allow a space dilineated string instead of array
529
+ if typeof combo_dictionary["keys"] is "string"
530
+ combo_dictionary["keys"] = combo_dictionary["keys"].split " "
531
+ for own property, value of @_defaults
532
+ if combo_dictionary[property] is undefined
533
+ combo_dictionary[property] = value
534
+ combo = new Combo combo_dictionary
535
+
536
+ if _validate_combo combo
537
+ @_registered_combos.push combo
538
+ return combo
539
+
540
+ register_many: (combo_array) ->
541
+ # Will return an array of the combos actually registered
542
+ @register_combo(combo) for combo in combo_array
543
+
544
+ unregister_combo: (keys_or_combo) ->
545
+ return false unless keys_or_combo
546
+
547
+ unregister_combo = (combo) =>
548
+ for i in [0...@_registered_combos.length]
549
+ if combo is @_registered_combos[i]
550
+ @_registered_combos.splice i, 1
551
+ break
552
+
553
+ if keys_or_combo.keys?
554
+ unregister_combo keys_or_combo
555
+ else
556
+ if typeof keys_or_combo is "string"
557
+ keys_or_combo = keys_or_combo.split " "
558
+ for combo in @_registered_combos
559
+ continue unless combo?
560
+ if (combo.is_unordered and _compare_arrays(keys_or_combo, combo.keys)) or (not combo.is_unordered and _compare_arrays_sorted(keys_or_combo, combo.keys))
561
+ unregister_combo combo
562
+
563
+ unregister_many: (combo_array) ->
564
+ for combo in combo_array
565
+ @unregister_combo combo
566
+
567
+ # Other public methods
568
+
569
+ get_registered_combos: ->
570
+ return @_registered_combos
571
+
572
+ reset: ->
573
+ @_registered_combos = []
574
+
575
+ listen: ->
576
+ @_prevent_capture = false
577
+
578
+ stop_listening: ->
579
+ @_prevent_capture = true
580
+
581
+ get_meta_key: ->
582
+ # Helpful for debugging purposes
583
+ return _metakey
584
+
585
+ ##################
586
+ # Helper Functions
587
+ ##################
588
+
589
+ _decide_meta_key = ->
590
+ # If the useragent reports Mac OS X, assume cmd is metakey
591
+ if navigator.userAgent.indexOf("Mac OS X") != -1
592
+ _metakey = "cmd"
593
+ return
594
+
595
+ _change_keycodes_by_browser = ->
596
+ if navigator.userAgent.indexOf("Opera") != -1
597
+ # Opera does weird stuff with command and control keys, let's fix that.
598
+ # Note: Opera cannot override meta + s browser default of save page.
599
+ # Note: Opera does some really strange stuff when cmd+alt+shift
600
+ # are held and a non-modifier key is pressed.
601
+ _keycode_dictionary["17"] = "cmd"
602
+ return
603
+
604
+ _convert_key_to_readable = (k) ->
605
+ return _keycode_dictionary[k]
606
+
607
+ _filter_array = (array, callback) ->
608
+ if array.filter
609
+ return array.filter(callback)
610
+ else
611
+ # For browsers without Array.prototype.filter like IE<9:
612
+ return (element for element in array when callback(element))
613
+
614
+ _compare_arrays = (a1, a2) ->
615
+ # This will ignore the ordering of the arrays
616
+ # and simply check if they have the same contents.
617
+ return false unless a1.length is a2.length
618
+ for item in a1
619
+ continue if item in a2
620
+ return false
621
+ return true
622
+
623
+ _compare_arrays_sorted = (a1, a2) ->
624
+ return false unless a1.length is a2.length
625
+ for i in [0...a1.length]
626
+ return false unless a1[i] is a2[i]
627
+ return true
628
+
629
+ _is_array_in_array = (a1, a2) ->
630
+ # Returns true only if all of the contents of
631
+ # a1 are included in a2
632
+ for item in a1
633
+ return false unless item in a2
634
+ return true
635
+
636
+ _index_of_in_array = Array.prototype.indexOf or (a, item) ->
637
+ for i in [0..a.length]
638
+ return i if a[i] is item
639
+ return -1
640
+
641
+ _is_array_in_array_sorted = (a1, a2) ->
642
+ # Return true only if all of the contents of
643
+ # a1 are include in a2 and they appear in the
644
+ # same order in both.
645
+ prev = 0
646
+ for item in a1
647
+ index = _index_of_in_array.call a2, item
648
+ if index >= prev
649
+ prev = index
650
+ else
651
+ return false
652
+ return true
653
+
654
+ _log_error = () ->
655
+ console.log arguments... if keypress.debug
656
+
657
+ _key_is_valid = (key) ->
658
+ valid = false
659
+ for _, valid_key of _keycode_dictionary
660
+ if key is valid_key
661
+ valid = true
662
+ break
663
+ unless valid
664
+ for _, valid_key of _keycode_shifted_keys
665
+ if key is valid_key
666
+ valid = true
667
+ break
668
+ return valid
669
+
670
+ _validate_combo = (combo) ->
671
+ validated = true
672
+
673
+ # Warn for lack of keys
674
+ unless combo.keys.length
675
+ _log_error "You're trying to bind a combo with no keys:", combo
676
+
677
+ # Convert "meta" to either "ctrl" or "cmd"
678
+ # Don't explicity use the command key, it breaks
679
+ # because it is the windows key in Windows, and
680
+ # cannot be hijacked.
681
+ for i in [0...combo.keys.length]
682
+ key = combo.keys[i]
683
+ # Check the name and replace if needed
684
+ alt_name = _keycode_alternate_names[key]
685
+ key = combo.keys[i] = alt_name if alt_name
686
+ if key is "meta"
687
+ combo.keys.splice i, 1, _metakey
688
+ if key is "cmd"
689
+ _log_error "Warning: use the \"meta\" key rather than \"cmd\" for Windows compatibility"
690
+
691
+ # Check that all keys in the combo are valid
692
+ for key in combo.keys
693
+ unless _key_is_valid key
694
+ _log_error "Do not recognize the key \"#{key}\""
695
+ validated = false
696
+
697
+ # We can only allow a single non-modifier key
698
+ # in combos that include the command key (this
699
+ # includes 'meta') because of the keyup bug.
700
+ if "meta" in combo.keys or "cmd" in combo.keys
701
+ non_modifier_keys = combo.keys.slice()
702
+ for mod_key in _modifier_keys
703
+ if (i = _index_of_in_array.call(non_modifier_keys, mod_key)) > -1
704
+ non_modifier_keys.splice(i, 1)
705
+ if non_modifier_keys.length > 1
706
+ _log_error "META and CMD key combos cannot have more than 1 non-modifier keys", combo, non_modifier_keys
707
+ validated = false
708
+
709
+ # Tell the user if they are trying to use any
710
+ # combo properties that don't actually exist,
711
+ # but allow the combo
712
+ for property, value of combo
713
+ if _factory_defaults[property] is "undefined"
714
+ _log_error "The property #{property} is not a valid combo property. Your combo has still been registered."
715
+
716
+ return validated
717
+
718
+ _convert_to_shifted_key = (key, e) ->
719
+ return false unless e.shiftKey
720
+ k = _keycode_shifted_keys[key]
721
+ return k if k?
722
+ return false
723
+
724
+ ##########################
725
+ # Key Mapping Dictionaries
726
+ ##########################
727
+
728
+ _modifier_event_mapping =
729
+ "cmd" : "metaKey"
730
+ "ctrl" : "ctrlKey"
731
+ "shift" : "shiftKey"
732
+ "alt" : "altKey"
733
+
734
+ _keycode_alternate_names =
735
+ "escape" : "esc"
736
+ "control" : "ctrl"
737
+ "command" : "cmd"
738
+ "break" : "pause"
739
+ "windows" : "cmd"
740
+ "option" : "alt"
741
+ "caps_lock" : "caps"
742
+ "apostrophe" : "\'"
743
+ "semicolon" : ";"
744
+ "tilde" : "~"
745
+ "accent" : "`"
746
+ "scroll_lock" : "scroll"
747
+ "num_lock" : "num"
748
+
749
+ _keycode_shifted_keys =
750
+ "/" : "?"
751
+ "." : ">"
752
+ "," : "<"
753
+ "\'" : "\""
754
+ ";" : ":"
755
+ "[" : "{"
756
+ "]" : "}"
757
+ "\\" : "|"
758
+ "`" : "~"
759
+ "=" : "+"
760
+ "-" : "_"
761
+ "1" : "!"
762
+ "2" : "@"
763
+ "3" : "#"
764
+ "4" : "$"
765
+ "5" : "%"
766
+ "6" : "^"
767
+ "7" : "&"
768
+ "8" : "*"
769
+ "9" : "("
770
+ "0" : ")"
771
+
772
+ _keycode_dictionary =
773
+ 0 : "\\" # Firefox reports this keyCode when shift is held
774
+ 8 : "backspace"
775
+ 9 : "tab"
776
+ 12 : "num"
777
+ 13 : "enter"
778
+ 16 : "shift"
779
+ 17 : "ctrl"
780
+ 18 : "alt"
781
+ 19 : "pause"
782
+ 20 : "caps"
783
+ 27 : "esc"
784
+ 32 : "space"
785
+ 33 : "pageup"
786
+ 34 : "pagedown"
787
+ 35 : "end"
788
+ 36 : "home"
789
+ 37 : "left"
790
+ 38 : "up"
791
+ 39 : "right"
792
+ 40 : "down"
793
+ 44 : "print"
794
+ 45 : "insert"
795
+ 46 : "delete"
796
+ 48 : "0"
797
+ 49 : "1"
798
+ 50 : "2"
799
+ 51 : "3"
800
+ 52 : "4"
801
+ 53 : "5"
802
+ 54 : "6"
803
+ 55 : "7"
804
+ 56 : "8"
805
+ 57 : "9"
806
+ 65 : "a"
807
+ 66 : "b"
808
+ 67 : "c"
809
+ 68 : "d"
810
+ 69 : "e"
811
+ 70 : "f"
812
+ 71 : "g"
813
+ 72 : "h"
814
+ 73 : "i"
815
+ 74 : "j"
816
+ 75 : "k"
817
+ 76 : "l"
818
+ 77 : "m"
819
+ 78 : "n"
820
+ 79 : "o"
821
+ 80 : "p"
822
+ 81 : "q"
823
+ 82 : "r"
824
+ 83 : "s"
825
+ 84 : "t"
826
+ 85 : "u"
827
+ 86 : "v"
828
+ 87 : "w"
829
+ 88 : "x"
830
+ 89 : "y"
831
+ 90 : "z"
832
+ 91 : "cmd"
833
+ 92 : "cmd"
834
+ 93 : "cmd"
835
+ 96 : "num_0"
836
+ 97 : "num_1"
837
+ 98 : "num_2"
838
+ 99 : "num_3"
839
+ 100 : "num_4"
840
+ 101 : "num_5"
841
+ 102 : "num_6"
842
+ 103 : "num_7"
843
+ 104 : "num_8"
844
+ 105 : "num_9"
845
+ 106 : "num_multiply"
846
+ 107 : "num_add"
847
+ 108 : "num_enter"
848
+ 109 : "num_subtract"
849
+ 110 : "num_decimal"
850
+ 111 : "num_divide"
851
+ 124 : "print"
852
+ 144 : "num"
853
+ 145 : "scroll"
854
+ 186 : ";"
855
+ 187 : "="
856
+ 188 : ","
857
+ 189 : "-"
858
+ 190 : "."
859
+ 191 : "/"
860
+ 192 : "`"
861
+ 219 : "["
862
+ 220 : "\\"
863
+ 221 : "]"
864
+ 222 : "\'"
865
+ 223 : "`"
866
+ 224 : "cmd"
867
+ 225 : "alt"
868
+ # Opera weirdness
869
+ 57392 : "ctrl"
870
+ 63289 : "num"
871
+ # Firefox weirdness
872
+ 59 : ";"
873
+
874
+ ############
875
+ # Initialize
876
+ ############
877
+
878
+ _decide_meta_key()
879
+ _change_keycodes_by_browser()
880
+
881
+ # Anonymous Module Definition
882
+ if typeof define is "function" and define.amd
883
+ define [], ->
884
+ return keypress
885
+ else if exports?
886
+ exports.keypress = keypress
887
+ else
888
+ window.keypress = keypress