Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Trent R on Wed 22/10/2008 01:59:29

Title: Double keypress
Post by: Trent R on Wed 22/10/2008 01:59:29
I am bloody lost.  :(

I've been trying for a while now to make a double keypress function in my room and I can't get it to work whatsoever. I searched the forum and found a few topics on making a double click function for the mouse. I tried to modify those, but they didn't work.

The biggest problem is that I can't figure out how to make it so it won't trigger both the single press and double press results. I've tried WaitKey and using a Timer (though I suspect I messed that code up).


~Trent
Title: Re: Double keypress
Post by: Khris on Wed 22/10/2008 02:27:02
Try this:
// at the top of the room script
int keytimer, lastkey = -1;
#define DELAY 20

function on_double_key_press(int k) {
  // your code here
}

function on_key_press(int k) {
  if (k == lastkey && keytimer > 0) {
    lastkey = -1;
    ClaimEvent();
    on_double_key_press(k);
  }
  else {
    lastkey = k;
    keytimer = DELAY;
  }
}

function repeatedly_execute_always() {
  if (keytimer > 0) keytimer--;
}


Untested!
Title: Re: Double keypress
Post by: Trent R on Thu 23/10/2008 01:45:13
Thanks Khris, because your code cleaner than mine at the least.

However, it still activates both the single and double press results. Currently, I'm putting the single press result after the line 'keytimer = DELAY', is that right?

For movement, I think I can just code the double press to undo the result of the single. But I have other actions, where undoing the single isn't possible. I guess I can work around it by just using more keys.

~Trent



[Edit]:
Mwahaha! I just had a revelation!

Despite that my original question was not fully answered, I have revamped my battle system so that I shouldn't need to separate the single and double press actions.


~Trent
PS-That doesn't mean I wouldn't still like to know though....
Title: Re: Double keypress
Post by: Khris on Thu 23/10/2008 08:19:12
// at the top of the room script
int keytimer, lastkey = -1;
#define DELAY 20

function on_double_key_press(int k) {
  // your code here
}

function on_key_press(int k) {
  if (k != lastkey) {
    lastkey = k;
    keytimer = DELAY;
  }
  else {
    if (keytimer > 0) {
      lastkey = -1;
      on_double_key_press(k);
    }
    else {
      // react to single keypresses
    }
  }
}

function repeatedly_execute_always() {
  if (keytimer > 0) {
    keytimer--;
    if (keytimer == 0) on_key_press(lastkey);
  }
}


Again, untested!
Title: Re: Double keypress
Post by: Trent R on Thu 23/10/2008 08:55:55
Quote from: KhrisMUC on Thu 23/10/2008 08:19:12
Again, untested!
That's why I'm here  :)

Unfortunately that didn't work either... The double result now acts first, with the single following a bit later.
[Edit]: Something I noticed: If you press a key once, it will run the the single result. Then, if you try to double press that same key, the double result won't run, but the single will run without the usual delay.

~Trent
Title: Re: Double keypress
Post by: Khris on Thu 23/10/2008 08:58:54
Tested, working:
int keytimer, lastkey = -1;
#define DELAY 7

function on_single_key_press(int k) {
  Display("Single key press detected!");
}

function on_double_key_press(int k) {
  Display("Double key press detected!");
}

function on_key_press(int k) {
  if (k != lastkey) {
    lastkey = k;
    keytimer = DELAY;
  }
  else {
    if (keytimer > 0) on_double_key_press(k);
    else on_single_key_press(k);
    lastkey = -1;
    keytimer = 0;
  }
}

function repeatedly_execute_always() {
  if (keytimer > 0) {
    keytimer--;
    if (keytimer == 0) on_key_press(lastkey);
  }
}
Title: Re: Double keypress
Post by: Trent R on Thu 23/10/2008 09:15:05
Awesome! Thank you so much Khris!

Usually I hate asking for code, because usually I can figure it out eventually and I learn more if I don't. But this just had me completely stumped.


~Trent