Skip to content

Instantly share code, notes, and snippets.

@ponkotuy
Created July 6, 2014 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ponkotuy/91145d3969b9c17eef58 to your computer and use it in GitHub Desktop.
Save ponkotuy/91145d3969b9c17eef58 to your computer and use it in GitHub Desktop.
CommandWatcher
class CommandWatcher
constructor: (commands) ->
@keys = []
@length = commands.length
@command = commands.join ','
watch: (handler) =>
watcher = @
$(document).on 'keydown', (event) ->
watcher.keys.push event.which
# マッチしたら実行後、即return
if watcher.keys.length is watcher.length and watcher.keys.join(',') is watcher.command
handler()
watcher.keys = []
return
# マッチしなかったらリセット
if watcher.command.indexOf(watcher.keys.join(',')) isnt 0
watcher.keys = []
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment