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!
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
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”);
7 ResponsesLeave a comment
Make yourself heard
All about Photon Storm and our
HTML5 game development services
Filter our Content
- Cool Links
- Flash Game Dev Tips
- Game Development
- Geek Shopping
- In the Media
- Phaser 3