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.

Revisions

  1. Sonoda Ryohei revised this gist Jul 7, 2014. 1 changed file with 8 additions and 7 deletions.
    15 changes: 8 additions & 7 deletions file1.coffee
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,22 @@
    # 関数化、リトライ対応版
    # ほぼTwitterのソースだけど若干パフォーマンスいいかも
    # ほぼTwitterのソースだけど
    class CommandWatcher
    constructor: (commands) ->
    @keys = []
    @length = commands.length
    @command = commands.join ','
    watch: (handler) =>
    $(document).on('keydown', (event) ->
    @keys.push event.which
    watcher = @
    $(document).on 'keydown', (event) ->
    watcher.keys.push event.which
    # マッチしたら実行後、即return
    if @keys.length is @length and keys.join(',') is @command
    if watcher.keys.length is watcher.length and watcher.keys.join(',') is watcher.command
    handler()
    @keys = []
    watcher.keys = []
    return
    # マッチしなかったらリセット
    if @command.indexOf(@keys.join(',')) isnt 0
    @keys = []
    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 ->
  2. Sonoda Ryohei revised this gist Jun 26, 2014. 1 changed file with 22 additions and 0 deletions.
    22 changes: 22 additions & 0 deletions file1.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    # 関数化、リトライ対応版
    # ほぼTwitterのソースだけど若干パフォーマンスいいかも
    class CommandWatcher
    constructor: (commands) ->
    @keys = []
    @length = commands.length
    @command = commands.join ','
    watch: (handler) =>
    $(document).on('keydown', (event) ->
    @keys.push event.which
    # マッチしたら実行後、即return
    if @keys.length is @length and keys.join(',') is @command
    handler()
    @keys = []
    return
    # マッチしなかったらリセット
    if @command.indexOf(@keys.join(',')) isnt 0
    @keys = []
    return

    new CommandWatcher([38,38,40,40,37,39,37,39,66,65]).watch ->
    # ここにコナミコマンドのイベントを実装
  3. Sonoda Ryohei revised this gist Jun 25, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions file0.js
    Original file line number Diff line number Diff line change
    @@ -1,13 +1,13 @@

    (function(fire) {
    var cmd = [38,38,40,40,37,39,37,39,66,65], keys = [];
    (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 = [];
    });
    })(function(){
    })([38,38,40,40,37,39,37,39,66,65], function(){
    // ここにコナミコマンドのイベントを実装
    });
  4. Sonoda Ryohei revised this gist Jun 23, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion file0.js
    Original file line number Diff line number Diff line change
    @@ -8,6 +8,6 @@
    if (keys.join(',') === CMD) fire();
    keys = [];
    });
    )(function(){
    })(function(){
    // ここにコナミコマンドのイベントを実装
    });
  5. Sonoda Ryohei revised this gist Jun 23, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion file0.js
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    var l = cmd.length, CMD = cmd.join(',');
    $(document).on('keydown', function(event) {
    keys.push(event.which);
    if (keys.lenght < l) return true;
    if (keys.length < l) return true;
    if (keys.join(',') === CMD) fire();
    keys = [];
    });
  6. Sonoda Ryohei revised this gist Jun 23, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion file0.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    var cmd = [38,38,40,40,37,39,37,39,66,65], keys = [];
    var l = cmd.length, CMD = cmd.join(',');
    $(document).on('keydown', function(event) {
    keys.puhs(event.which);
    keys.push(event.which);
    if (keys.lenght < l) return true;
    if (keys.join(',') === CMD) fire();
    keys = [];
  7. Sonoda Ryohei revised this gist Jun 23, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion file0.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@

    (function(fire) {
    var cmd = ['38,38,40,40,37,39,37,39,66,65'], keys = [];
    var cmd = [38,38,40,40,37,39,37,39,66,65], keys = [];
    var l = cmd.length, CMD = cmd.join(',');
    $(document).on('keydown', function(event) {
    keys.puhs(event.which);
  8. Sonoda Ryohei revised this gist Jun 23, 2014. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions file0.js
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,11 @@

    (function(fire) {
    var cmd = '38,38,40,40,37,39,37,39,66,65', keys = [];
    var l = cmd.length;
    var cmd = ['38,38,40,40,37,39,37,39,66,65'], keys = [];
    var l = cmd.length, CMD = cmd.join(',');
    $(document).on('keydown', function(event) {
    keys.puhs(event.which);
    if (keys.lenght < l) return true;
    if (keys.join(',') === cmd) fire();
    if (keys.join(',') === CMD) fire();
    keys = [];
    });
    )(function(){
  9. Sonoda Ryohei created this gist Jun 23, 2014.
    13 changes: 13 additions & 0 deletions file0.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@

    (function(fire) {
    var cmd = '38,38,40,40,37,39,37,39,66,65', keys = [];
    var l = cmd.length;
    $(document).on('keydown', function(event) {
    keys.puhs(event.which);
    if (keys.lenght < l) return true;
    if (keys.join(',') === cmd) fire();
    keys = [];
    });
    )(function(){
    // ここにコナミコマンドのイベントを実装
    });