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

In the previous page, we dived through more of our checkCombinations method. Let's pick up from where we left off and go back to our second for loop and try again. The value of j is still 1, but the value of i is now 1:

if (keys_pressed[1] == key_combinations[1][2])

Just like before, when we map the values referenced by our array indices, we get:

if (90 == 90)

This equates to true! So far so good. Let's proceed on to the next if statement:

if (1 == key_combinations[1].length - 2)

This time, this too holds true. The length of our key_combinations[1] array minus 2 does equal 1. We now execute the following line of code:

     invokeOnKeyCombination (key_combinations [j][0]);

The value of j, of course, is 1. Substituting in the values from the key_combinations array, we get the following:

key_combinations -> [[redo, 17, 89], [undo, 17, 90], [paste, 17, 86]];

The item referenced by index position 1 (j = 1), is:

[undo, 17, 90]

The 0th object is the string value undo. In short, the invokeOnKeyCombination function takes in the argument undo. If you recall from earlier, the invokeOnKeyCombination simply maps your input to the case/switch statement you designated in your FLA file:

myObj.onKeyCombination = function(name:String) {
switch (name) {
case "undo" :
message_txt.text = "Undo Combination Pressed";
case "redo" :
message_txt.text = "Redo Combination Pressed";
case "new" :
message_txt.text = "New Combination Pressed";

A text field called message_txt will display the sentence "Undo Combination Pressed." Excellent. You are good to go!

I hope this tutorial helped you to incorporate key detection in your animations. More importantly, this tutorial should have provided your more practice with using, accessing, and manipulating data stored in nested arrays.

If you are interested in seeing my implementation of the animation on the first page of this tutorial, I have provided the source file for it:

If you have any questions, please post them on the forums.

Michael Avila (MichaelxxOA)

Huge Grin

Kirupa Chinnathambi (kirupa)
kirupa | MIT



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

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