PDA

View Full Version : combo keydown


hyperzoanoid
2002.08.22, 01:12 PM
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?

Jeff Binder
2002.08.22, 02:18 PM
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.

kainsin
2002.08.22, 02:36 PM
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.

ededed
2002.08.22, 03:59 PM
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 :D )

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



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;


- (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];
}

hyperzoanoid
2002.08.23, 10:51 PM
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?

furballphat
2002.08.24, 05:54 AM
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.

Tobi
2002.08.24, 06:27 AM
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.

OneSadCookie
2002.08.24, 06:35 AM
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.

Tobi
2002.08.24, 07:01 AM
yeah, that would be quite fast. Here's an example for the combo checking (not tested):



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

hyperzoanoid
2002.08.24, 01:23 PM
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 :)