Author Topic: Detect if any key is "just pressed"  (Read 2126 times)

IQAndreas

  • Member
  • **
  • Posts: 35
  • Karma: +0/-0
    • View Profile
    • IQAndreas.com
Detect if any key is "just pressed"
« on: Wed, Dec 21, 2011 »
I don't want to detect if any key is down (done with [ic]FlxG.key.any()[/ic] ) but instead if any key was just pressed.

Does this already exist, or should I add such a function?

test84

  • Key Contributor
  • *****
  • Posts: 1328
  • Karma: +0/-0
  • ت
    • View Profile
    • My personal site.
Re: Detect if any key is "just pressed"
« Reply #1 on: Wed, Dec 21, 2011 »
I don't think we have such thing as of now, but I think a small edit to existing "any()" would do.
blog, twitter, Check out my award winning game, Rot Gut:

IQAndreas

  • Member
  • **
  • Posts: 35
  • Karma: +0/-0
    • View Profile
    • IQAndreas.com
Re: Detect if any key is "just pressed"
« Reply #2 on: Wed, Dec 21, 2011 »
Changes made, and recommended merging with the library:
https://github.com/AdamAtomic/flixel/pull/215


I hope the developers don't feel like I'm harassing them with all my pull requests. Maybe I should just leave my changes for my own fork... :-\

pixelomatic

  • Active Member
  • ***
  • Posts: 131
  • Karma: +0/-0
    • View Profile
Re: Detect if any key is "just pressed"
« Reply #3 on: Thu, Dec 22, 2011 »
It's easy guys, just think. There's already a function FlxG.keys.any() checks if any key is down. Keep a variable name anyKeyJustPressed that is initially false.

In update code of the object:
Code: [Select]
if (FlxG.keys.any() && !anyKeyJustPressed)
{
anyKeyJustPressed = true;
// Do the stuff you want when any key is just pressed.
}
else if (!FlxG.keys.any())
{
anyKeyJustPressed = false;
}

I'm sure you can also work around about just released behavior too like this.