Skip to content

Instantly share code, notes, and snippets.

@sonodar
Created June 23, 2014 12:36
Show Gist options
  • Save sonodar/dfde21b0363b1753ef66 to your computer and use it in GitHub Desktop.
Save sonodar/dfde21b0363b1753ef66 to your computer and use it in GitHub Desktop.
[JavaScript] コナミコマンドのスニペット ref: http://qiita.com/sonodar/items/b1da050e4cc2806bba9d
(function(cmd, fire) {
var keys = [];
var l = cmd.length, CMD = cmd.join(',');
$(document).on('keydown', function(event) {
keys.push(event.which);
if (keys.length < l) return true;
if (keys.join(',') === CMD) fire();
keys = [];
});
})([38,38,40,40,37,39,37,39,66,65], function(){
// ここにコナミコマンドのイベントを実装
});
# 関数化、リトライ対応版
# ほぼTwitterのソースだけど
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
new CommandWatcher([38,38,40,40,37,39,37,39,66,65]).watch ->
# ここにコナミコマンドのイベントを実装
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment