Those of you familiar with ActionScript 1.0 (and even ActionScript 2.0) may have, on occasion, employed the ever-so-helpful flag Key.isDown when testing for the "downness" of a key.
In Actionscript 3.0, you no longer have that luxury!
Here's what I've been doing to fake it:
import flash.events.KeyboardEvent; import flash.ui.Keyboard; var keys:Object=new Object(); stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyboardDown); stage.addEventListener(KeyboardEvent.KEY_UP, onKeyboardUp); function onKeyboardDown (evt:KeyboardEvent):void { keys[evt.keyCode]=evt.keyCode; } function onKeyboardUp (evt:KeyboardEvent):void { delete keys[evt.keyCode]; }
Then, if you need to test for key combinations, add to the relevant handler; for example, if I wanted to make sure the Ctrl, W, and Z keys were all currently down, I'd add this to the onKeyboardDown listener:
if (keys[Keyboard.CONTROL] && keys[87] && keys[90]) { // do stuff }
(87 is the key code for "W," and 90 is the key code for "Z". Don't want to memorize key codes? There's always this handy-dandly little thingy!)
It's pretty straightforward, which is probably why lots of people have already written their own routines for handling Key.isDown. If you're one of the ones who hadn't written such statements, feel free to steal them from here!
Be the first to comment!
Leave a reply