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

In the previous page, you got a brief glimpse at what cool things this tutorial will show you. Let's start by first explaining how you can implement this effect into your own animations.

How to Use:
It is very simple to incorporate the Multiple Key Detection feature into your projects.

Note

If you are fairly familiar with Flash, read the directions in Michael's post, and feel free to skip over to the end of the next page.

If you are a little overwhelmed with all of this, the following paragraphs should help you get started. First, download the KeyDetection.as file from the following link:

Download Class File

After downloading the file from the above link, extract the KeyDetection.as file to the location you want your Flash animation to be located. For simplicity, your Flash project and KeyDetection.as file must be located in the same folder.

With your animation created, the following is an example of the code you will be using:

var keyDet = new KeyDetection();
keyDet.addCombination("letterK", Key.CONTROL, 75);
keyDet.addCombination("letterJ", Key.CONTROL, 74);
myObj = new Object();
myObj.onKeyCombination = function(name:String) {
switch (name) {
case "letterK" :
duplicateK();
break;
case "letterJ" :
duplicateJ();
break;
}
};
keyDet.addListener(myObj);

In the above code, I left only the important parts of the code colored, for those are the lines that you will want to modify to suit your animation. Let's go through what some of the more important parts of the code do.

To add a key combination, you would use the following format:

keyDet.addCombination("caseName", Key,...,Key)

The object keyDet is a KeyDetection() object as seen in the first line of code above. You will use the addCombination method to input your key combination's name and keys.

The value for caseName corresponds to what you want to call your particular combination. I will explain the significance of the caseName in a short bit. To better help you understand how to add key combinations, I will provide several examples.

Ctrl + K:

keyDet.addCombination("letterK", Key.CONTROL, 75);

Ctrl + J:

keyDet.addCombination("letterJ", Key.CONTROL, 74);

Ctrl + Shift + S:

keyDet.addCombination("save as", Key.CONTROL, Key.SHIFT, 83);

The numbers 75, 74, and 83 are the corresponding ASCII codes for the letters K, J, and S. Also, notice that in the last example I have a key combination involving three keys instead of the traditional two key method.

For a list of ASCII codes and the characters they map to, the following table (courtesy of Wikipiedia) should help you:

ASCII Code Letter
65 A
66 B
67 C
68 D
69 E
70 F
71 G
72 H
73 I
74 J
75 K
76 L
77 M
78 N
79 O
80 P
81 Q
82 R
83 S
84 T
85 U
86 V
87 W
88 X
89 Y
90 Z
 

Adding key combinations is just one part of the equation. In the next page, you'll learn how to specify events that will be triggered for specific key combinations.

 

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




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.