Using the Key class

Written by Aaron Clinger on January 27th, 2009

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:

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.

Comments

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.

Written by Aaron Clinger on July 24th, 2009

Shift shouldn’t be required. My guess is you were testing this in the Flash IDE using test movie. The IDE often hijacks the key commands and does not pass them to the SWF correctly. If you test this in a browser, or disable keyboard shortcuts (in the control menu) it should help. Let me know if it doesn’t.

Written by Aubrey Taylor on October 27th, 2009

You should always disable hotkeys when testing from the Flash IDE. You can do this by :

1. Publish from the Flash IDE, 2. Mouse over to control > Disable Keyboard Shortcuts. This should minimize the weirdness, though I would just test from a browser to be sure.

Comments are no longer accepted for this blog post.