combo keydown

Nibbie
Posts: 1
Joined: 2008.09
Post: #1
i need a solution on how to get combo keystrokes, so i can call a method based on the collective strokes, but ive never done this before, so im kinda lost, maybe someone could point me in the right direction?

Right now i only have two ideas which is to put a delay in, and the keys pressed in the delay will be used.

or

when a key is pressed it will have a certain delay, if another key is pressed then the delay will lengthen just for another key stroke. When the delay is run out then a method will be called.

so would these be the right way of doing it?
Quote this message in a reply
Jeff Binder
Unregistered
 
Post: #2
Would you explain more clearly what you're trying to do? Are you looking for normal modifier keys (i.e. Command-S), or something where the keys must be pressed at about the same time?

For modifier keys, you can just get the modifiers that are currently down from the NSEvent.

If you want the keys to be pressed at the same time, the second method you mentioned should work, although I'm not quite sure what you meant with the first one.
Quote this message in a reply
kainsin
Unregistered
 
Post: #3
You have the right idea to start with. However, you should also check to see if a combo has been completed and execute the proper function right away, rather than waiting for the delay to run out. Also, if the user presses the wrong key in a sequence, then the list of keys pressed should be reset. But make sure that the list starts with the wrong key! The user might have stopped half-way through the current combo, only to start another combo.
Quote this message in a reply
ededed
Unregistered
 
Post: #4
Do you want somthing like "Hold Command-Alt-K to gain access to the secret teminal") or do you want a system that act's like a fighting game where you have to pres punch punch kick then back to do a superkick or whatever.

(I have not checked this code but it will either work or hopefully help Grin )

If it's the first on then you should do somthing along the lines of:

Code:
BOOL commandKey, AltKey, KKey;

- (void)checkKeys
{
   if(commandKey && AltKey && KKey)
      [self comboPressed];
}

- (void)keyDown:(NSEvent*)e
{
   if(event keyCode] == 'k')
      KKey = YES:

    .........
    [self checkKeys];
}

- (void)keyUp:(NSEvent*)e
{
   if(event keyCode] == 'k')
      KKey = NO:
}


But if you wanted a beat'em'up system then you must call checkKeys every frame so that it will timeout:

double combo;

Code:
- (void)checkKeys
{
   if(combo >= 0 && combo <= 1) {
      if(punchKey)
         combo += 1;
      else
         combo -= 0.1;
      
      if(kickKey) // if they press the wrong key they must start again
         combo = 0;
   }
   else {
      if(kickKey)
         combo += 1;
      else
          combo -= 0.1;
      
       if(punchKey)
          combo = 0;
   }
    
    if(combo < 0)
        combo = 0;
    
    if(combo > 2)
       [self superKick];
}
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2008.09
Post: #5
sorry i should have stated that im looking for the key combo system for a beatemup game.

ededed, you mentioned that i would have to call check keys every frame so that it would "time out", im not exactly sure what you mean by that and i also have another question.

wouldn't calling it every frame be expensive since at around 30 fps there may only be 2 keystrokes for a combo?

also, how long do you think the delay should be between keystrokes if i choose to allow a delay for combos?
Quote this message in a reply
furballphat
Unregistered
 
Post: #6
I believe that ed is trying to say you should have booleans for all the keys that could down. Every frame all these variables should be set to NO. If a key is pressed the event queue will call the keyDown method. In here you should set the corresponding keys to YES.
Quote this message in a reply
Tobi
Unregistered
 
Post: #7
Here's my idea:

First you have an empty string.
Then if you hit a key, that char is appended to the end of the string.
If too much time went by since the last key stroke (like 1 second), the string is emptied again. This is the timeout. Then you need a function which checks this string all the time for combos. If it finds a combo, the string will be cleared and the combo is executed. You also need to remove chars from the beginning of the string so it doesn't become too large in case the player never enters a right combo. The smaller the string is the faster it can be checked for combos. And you will never loose a combo this way.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #8
If your largest combo requires five keys hit in sequence, you never need to store more than five keys in your string. That means that your string will always be short and easy to check for combos.

Note that to check for a combo of, say, 3 keys, you need only to check the last three keys in the string. The first two can't have any effect.
Quote this message in a reply
Tobi
Unregistered
 
Post: #9
yeah, that would be quite fast. Here's an example for the combo checking (not tested):

Code:
Boolean IsComboInString(char *string, *combo)
{
int    i;
char *step1;
char *step2;

step1=&string[strlen(string)];
step2=&combo[strlen(combo)];

for (i=0; i<strlen(combo); i++)
    {
    step1--;
    step2--;
    if (*step1!=*step2){return false;}
    }

return true;
}

Good luck!

-Tobi
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2008.09
Post: #10
yeah, what tobi explained was kindof what i had in mind.
ill store the keystrokes in a string, then when the "delay" runs out, ill execute the combos.
each keystroke will extend the delay, and unless a combo has been done, and thats pretty much it.

Thankyou to all that helped me-Hyper Smile
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  [Cocoa] Problem with keyDown Criollo 4 4,197 Dec 13, 2003 06:38 PM
Last Post: Tycho