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

In the previous page, I briefly framed the problem associated with getting our checkCombination method to work. Let's continue our step-by-step trace of how it works in this page.

First, let's go through the nested for loop:

for (var j:Number = 0; j<key_combinations.length; j++) {
for (var i:Number = 0; i<keys_pressed.length; i++) {
if (keys_pressed[i] == key_combinations[j][i+1]) {
if (i == key_combinations[j].length-2) {
} else {

Initially, j is initialized to zero. In the second loop, i is initialized to 0 also. We are now at the if statement:

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

I substituted a 0 for both the i and j variables in the if statement. keys_pressed[0] maps to the number 17 from our keys_pressed array. If you recall, The key_combinations array is a nested array. By calling key_combinations[0][1], I am calling the first array's second item - which is 17. Success!

Because the previous if statement equated out to true, we go to the second if statement (with i and j values substituted in):

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

The length of our key_combinations[0] array ([redo, 17, 89]), is three. So, 3-2 is 1, and 0 does not equal 1. This conditional results in false. Let's go back to our second for loop and try again!

Trial 0.1
So, we are back at the second for loop. The value for j remains the same at 0, because the first for loop is what controls the value of the j variable. But the value of i will increment (i++). It is now 1. Now, our first if statement is:

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

The maps out to if (90 == 89). Nope - that does not work! We jump down the break statement and exit out of this loop. We are now back at the first for loop. Let's try again!

Trial 1.0
So, we are back at (almost) where we started from - the first for loop. This time, the value of j is set to 1 instead instead of 0. We proceed to the second for loop, and we start back with i being equal to 1. Now, let's take a look at the first if statement again:

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

Notice that the value of j is 1, so we will be looking at the second item in our key_combinations array. More specifically, we will be looking at key_combinations' second item's second item:

key_combinations' second item:

[undo, 17, 90]

key_combinations' second item's second item:


Similar to before, our if statement checks out. keys_pressed[0] is 17 and so is the value returned by our key_combinations[1][1] array. We then go to the second if statement:

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

Like a recurring bad dream, though, you will unfortunately have the equality 0 == 1, which is false. Even though our j value is incremented by one, it still refers to an item of length 3.

Phew! We are still not done yet. Let's cover some more of our checkCombinations code on the next page and wrap this tutorial up!


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

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