Multiple Key Detection - Page 3
       code by Michael Avila, written by Kirupa  |  3 April 2006

In the previous page, you learned how to specify key combinations. But that is only part of the fun. We need to specify what needs to be done when a certain key combination is pressed. For that, we look at the switch statement in the code:

switch (name) {
case "caseName1" :
case "caseName2" :

Replace caseName1 and caseName2 with the caseName value you specified in your addCombination arguments. When a key combination is executed, the appropriate switch case will be fired. Any code that is contained in that particular case will be executed. One thing to remember, though, is that you will need a separate case for every key combination you specify.

While in the above example I only provided two cases, you can have as many cases as you want. Just remember to give your caseName values a unique name, or else Flash won't know which case to fire.

For the most part, the code you need to use the key detection is straightforward. I want to take a big u-turn and talk about the actual code in the KeyDetection class file. For, I think that is where the real action is!

Code Explained
Learning how to use the code is just one part of this tutorial. The other major part involves learning why the code works. Let's start at the top of our file:

// a list of all the key codes that have been pressed
private var keys_pressed : Array;
// a multi-dimensional list of all of our key combinations
private var key_combinations : Array;
// objects listening to this detection
private var listeners : Array;

The keyDetection class contains three private variables, keys_pressed, key_combination, and listeners, that will be instantiated for each object created.

public function KeyDetection ()
keys_pressed = new Array ();
key_combinations = new Array ();
listeners = new Array ();
// allow this object to listen for events from the key object
Key.addListener (this);

The above code is the constructor used to create KeyDetection objects. The constructor basically creates the object. Very little heavy lifting goes on here. The main thing to note is that the private variables we declared earlier are initialized here, and that ensures that a local copy of these variables are attached to the object being created by the constructor.

In the last line, I add this object to the Key's addListener method. The this variable inside the constructor refers to the object being created by this constructor.

public function addListener (listener : Object) : Void
for (var i : Number = 0; i < listeners.length; i ++)
if (listeners [i] == listener) return;
listeners.push (listener);

If your listeners array does not contain the object being passed in, then you add this object to your listeners array. This method is called by your Key.addListener line in the constructor.

We have barely scratched the surface of the code. There is more on the next page!


1 | 2 | 3 | 4 | 5 | 6 | 7

SUPPORTERS:'s fast and reliable hosting provided by Media Temple.