dmauro / Keypress

A keyboard input capturing utility in which any key can be a modifier key.
http://dmauro.github.io/Keypress/
Apache License 2.0
3.18k stars 313 forks source link

is_counting + is_sequence? #5

Closed xiaoli closed 11 years ago

xiaoli commented 11 years ago

code below:

{
          "keys"          : "up up down down left right left right b a enter",
          "is_exclusive"  : true,
          "is_counting"   : true,
          "is_sequence"   : true,
          "is_ordered"    : true,
          "on_keydown"    : function(e, count) {
            switch (count) {
              case 1:
                console.log('Want 30 lives? Please try again :)');
                break;
              case 2:
                console.log('No luck still. One more time? :D');
                break;
              case 3:
              default:
                console.log('Tried 3 times already.');
                break;
            }
            return false;
          }
}

I can't get the count of the combo. Am I missing something here?

Thanks for the great work of this library, save me a lot of time :)

dmauro commented 11 years ago

The counting combos are kind of their own thing that is different from a sequence combo; they aren't able to mix. A counting combo requires that a key is held down the entire time (as on the demo page) , it's kind of a strange functionality that you probably don't need (I just wanted it for my game). What you want to do is very simple though:

var cheat_code_count = 0;
{
          "keys"          : "up up down down left right left right b a enter",
          "is_exclusive"  : true,
          "is_counting"   : true,
          "is_sequence"   : true,
          "is_ordered"    : true,
          "on_keydown"    : function(e) {
            cheat_code_count += 1
            switch (cheat_code_count) {
              case 1:
                console.log('Want 30 lives? Please try again :)');
                break;
              case 2:
                console.log('No luck still. One more time? :D');
                break;
              case 3:
              default:
                console.log('Tried 3 times already.');
                break;
            }
            return false;
          }
}

It's very easy to track that count yourself. The counting combos are for a very specific case which would be very complex for someone to track without the help of the library.

Thanks for the kind words though, and good luck with your project!

xiaoli commented 11 years ago

oh, i see. thanks for that.

Great work once again.