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
  • April 18th 2008 at 9:59 am

    Hi Rich,

    Thanks for commenting on my blog. I’ve taken a look at what you’ve done and it’s pretty handy. Getting those multiple key presses easily is nice. I’ll keep your modified clas somewhere safe so that I can find it easily if I need it. Cheers and keep up the good work. Send my regards to Wallace and Gromit.

    Adrian

  • Reinhard Schmid
    May 17th 2008 at 11:31 am

    Hi Rich ,
    I have just played around with the KeyPoll Class and tried to create sort of “Geometry Wars” controlls for a space ship, but I came across a big problem : if you press a key and then release it quickly and press another key, there is a delay of about half a second before the second key press is recognized.
    you can see this in your example and it is also happening in the original class by richard lord.
    do you have any ideas why this is happening or how to get rid of this delay ?
    thanks for your help.

    reini

  • Reinhard Schmid
    May 17th 2008 at 11:39 am

    i forgot : you have to press and hold the second key to see the delay

  • June 19th 2008 at 9:58 pm

    Hey, I just stumbled upon this. Works great! Thanks a lot.

  • August 27th 2008 at 12:27 pm

    Hi,

    I have just started to learn ActionScript 3 and needed to do some simple Key.isDown for a game.
    Found it odd that is not in ActionScript 3 but then found your KeyPoll class, tried it and it works great!

    Thanks :)
    Xlander

  • hi
    October 8th 2008 at 6:55 pm

    ive found a bug in your class:
    1: press and hold a key.
    2: While its hold down press another key, and release it
    It will display that there is no key pressed, altough the 1st key is still hold down

  • Daniel
    January 31st 2009 at 11:02 pm

    Nice!

    Yhmks for sharing!

Make yourself heard