CASA Lib’s Key class creates an easy way to receive events when multiple keys are held/released (key combo) and when a sequence of keys are pressed (e.g. a word is typed).
It also returns the ability in ActionScript 3.0 to easily determine if a key is pressed/down. A feature we were used to in AS2.
To use the Key class, first initialize the StageReference class. This makes it easy to determine if a key is down by calling Key.getInstance().isDown(keyCode:uint); from anywhere in the application. Though, the real utility comes when trying to receive more complex key events.
To receive an event when keys are typed in sequence or multiple keys are pressed/released, first inform the Key class of the multiple keys you wish to monitor. This is accomplished by defining a KeyCombo object. The KeyCombo class stores a minimum of two key codes passed into the constructor as an Array:
/* 67 = C key 65 = A key 83 = S key 65 = A key */ casaCombo = new KeyCombo(new Array(67, 65, 83, 65));
Pass this key combination to the Key class, instructing the class to dispatch the following events:
KeyComboEvent.DOWN- Keys are pressed in unisonKeyComboEvent.RELEASE- Keys are released in unisonKeyComboEvent.SEQUENCE- Keys are typed in order
The class will dispatch the above events any time any added combo matches. If more than one combo has been added, check which combo triggered the event in the event listener by using KeyCombo’s equals method:
protected function _onComboTyped(e:KeyComboEvent):void {
if (this._casaCombo.equals(e.keyCombo)) {
trace("User typed casa.");
}
}
Now, put these parts together to create a simple example:
package {
import flash.display.MovieClip;
import org.casalib.ui.Key;
import org.casalib.ui.KeyCombo;
import org.casalib.events.KeyComboEvent;
import org.casalib.util.StageReference;
public class MyExample extends MovieClip {
protected var _casaCombo:KeyCombo;
protected var _key:Key;
public function MyExample() {
super();
StageReference.setStage(this.stage);
this._casaCombo = new KeyCombo(new Array(67, 65, 83, 65));
this._key = Key.getInstance();
this._key.addKeyCombo(this._casaCombo);
this._key.addEventListener(KeyComboEvent.DOWN, this._onComboDown);
this._key.addEventListener(KeyComboEvent.RELEASE, this._onComboRelease);
this._key.addEventListener(KeyComboEvent.SEQUENCE, this._onComboTyped);
}
protected function _onComboDown(e:KeyComboEvent):void {
if (this._casaCombo.equals(e.keyCombo)) {
trace("User is holding down keys c-a-s-a.");
}
}
protected function _onComboRelease(e:KeyComboEvent):void {
if (this._casaCombo.equals(e.keyCombo)) {
trace("User no longer holding down keys c-a-s-a.");
}
}
protected function _onComboTyped(e:KeyComboEvent):void {
if (this._casaCombo.equals(e.keyCombo)) {
trace("User typed casa.");
}
}
}
}
The code above fires if the c, a and s keys are held/released or if c-a-s-a is typed. The Key class also dispatches events for single key events. See the Key class documentation for more information.
Written by Sam on June 29th, 2009
Is this sample suppose to work only while pressing SHIFT? I had to hold down SHIFT in order to get it to execute.