Double keypress

Started by Trent R, Wed 22/10/2008 01:59:29

Previous topic - Next topic

Trent R

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
To give back to the AGS community, I can get you free, full versions of commercial software. Recently, Paint Shop Pro X, and eXPert PDF Pro 6. Please PM me for details.


Current Project: The Wanderer
On Hold: Hero of the Rune

Khris

Try this:
Code: ags
// 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!

Trent R

#2
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....
To give back to the AGS community, I can get you free, full versions of commercial software. Recently, Paint Shop Pro X, and eXPert PDF Pro 6. Please PM me for details.


Current Project: The Wanderer
On Hold: Hero of the Rune

Khris

Code: ags
// 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!

Trent R

#4
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
To give back to the AGS community, I can get you free, full versions of commercial software. Recently, Paint Shop Pro X, and eXPert PDF Pro 6. Please PM me for details.


Current Project: The Wanderer
On Hold: Hero of the Rune

Khris

#5
Tested, working:
Code: ags
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);
  }
}

Trent R

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
To give back to the AGS community, I can get you free, full versions of commercial software. Recently, Paint Shop Pro X, and eXPert PDF Pro 6. Please PM me for details.


Current Project: The Wanderer
On Hold: Hero of the Rune

SMF spam blocked by CleanTalk