Enhanced KeyPoll Class Released

I’ve been using the excellent KeyPoll class by Richard Lord / Big Room Ventures. It’s an extremely fast and efficient class for detecting keyboard presses for your games. But I needed the class to do more, so I’ve extended it, added in some new functionality and released it. My new version can detect multiple key presses at once, has the full scancode const list built-in and adds a new function to return the last pressed scancode. Plus it’s still just as fast and flexible as the original.

Grab the download and examples after the jump.

KeyPoll Version 1.2 – 10th April 2008
Download (15KB zip file)
Includes AS3 class, example FLA/SWF and a FlashDevelop project.

Use is easy:

1) Import the bigroom.input.KeyPoll class
2) Create an instance of it, set to listen to a DisplayObject (typically the stage)
3) Check for keyboard presses!

[as]
package
{
import bigroom.input.KeyPoll;
import flash.events.Event;

public class keypoll_test
{
private var keyboard:KeyPoll;

public function keypoll_test():void
{
// Creates the KeyPoll object and tells it to listen to the stage for keyboard events
keyboard = new KeyPoll(stage);

// Create event listens for key presses
addEventListener(Event.ENTER_FRAME, keyCheck, false, 0, true);
}

private function keyCheck(event:Event):void
{
// Returns whatever the most recent key held down was
trace(“KeyCode currently down: ” + keyboard.keyCode.toString());

// Test 1
// This code tests for just the A key being pressed on its own
if (keyboard.isDown(KeyPoll.A))
{
trace(“The A key is pressed”);
}

// Test 2
// This code tests for the B key and the SHIFT key being held at the same time
if (keyboard.isDown(KeyPoll.B, KeyPoll.SHIFT))
{
trace(“The B and SHIFT keys are pressed”);
}

// Test 3
// Tests code tests for the C key, the SHIFT key and the UP key all at the same time!
if (keyboard.isDown(KeyPoll.C, KeyPoll.SHIFT, KeyPoll.UP))
{
trace(“The C + SHIFT + UP keys are pressed”);
}
}
}
}
[/as]

Posted on April 12th 2008 at 12:07 am by .
View more posts in ActionScript3. Follow responses via the RSS 2.0 feed.


7 Responses

Leave a comment

Make yourself heard