Skip to content

Instantly share code, notes, and snippets.

@goshki
Created February 28, 2011 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goshki/847489 to your computer and use it in GitHub Desktop.
Save goshki/847489 to your computer and use it in GitHub Desktop.
Check for how long the given key has been held in FlashPunk mitigating the key-repeat under Linux
package {
import flash.utils.getTimer;
import net.flashpunk.*;
import net.flashpunk.utils.*;
/**
* <p>
* The reason to use such test is that under Linux holding a key triggers a key-repeat functionality after
* a moment and so, key-press events are generated. Therefore the Input.pressed() method will periodically
* report true even though the player just holds the key. Such behaviour is not noticeable under Windows
* (player has to release the button and press it again to generate a key-press event).
*
* <p>
* The same problem exists in Flixel with the FlxG.justPressed() method.
*/
public class FlashPunkKeyholdTest extends Engine {
public var spaceWasPressed:Boolean = false;
public var spacePressTime:int = 0;
public function Main() {
super( 640, 480, 60, false );
}
override public function update():void {
super.update();
if ( Input.check( Key.SPACE ) ) {
if ( !spaceWasPressed ) {
spaceWasPressed = true;
spacePressTime = getTimer();
}
}
else {
if ( spaceWasPressed ) {
trace( "SPACE press time: " + ( getTimer() - spacePressTime ) );
spaceWasPressed = false;
spacePressTime = 0;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment