# Multiple key detection

**URL:** <https://forum.kirupa.com/t/multiple-key-detection/633601>\
**Category:** flash\
**Created:** [August 10, 2015, 9:14am UTC](https://forum.kirupa.com/t/multiple-key-detection/633601 "2015-08-10T09:14:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xooo](https://avatars.discourse-cdn.com/v4/letter/x/b782af/32.png) [@Xooo](https://forum.kirupa.com/u/Xooo)\
**Post date:** [August 10, 2015, 9:14am UTC](https://forum.kirupa.com/t/multiple-key-detection/633601/1 "2015-08-10T09:14:06Z")

</div>

Hi, I’m new around here. I am doing my final year project with Flash CS5. I 'm trying to do that pressing two keys the program go to the next frame , for this I have been guided tutorial “multiple key detection” , but to run the program I get the following error: 5007 an ActionScript file must have at least one definition externally visible . I do not know what to do, could you help me? I’m not very expert in Flash yet. Excuse my English . Thank you.

---

<div class="post-metadata">

**Author:** ![zeke\_chan](https://avatars.discourse-cdn.com/v4/letter/z/f14d63/32.png) [@zeke\_chan](https://forum.kirupa.com/u/zeke_chan)\
**Post date:** [August 10, 2015, 10:40am UTC](https://forum.kirupa.com/t/multiple-key-detection/633601/2 "2015-08-10T10:40:00Z")

</div>

Hey Xooo, it’s hard to identify what the problem is exactly. Perhaps you could post your code here?

---

<div class="post-metadata">

**Author:** ![Xooo](https://avatars.discourse-cdn.com/v4/letter/x/b782af/32.png) [@Xooo](https://forum.kirupa.com/u/Xooo)\
**Post date:** [August 10, 2015, 12:06pm UTC](https://forum.kirupa.com/t/multiple-key-detection/633601/3 "2015-08-10T12:06:05Z")

</div>

Hi I am using the tutorial found on this site about " Multiple Key detection" that provided a archivo.as :

/\* Created to allow us to listen for key combinations.  
\*

- author Michael Avila
- version 1.0  
\*/

class KeyDetection {  
// 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;  
/\* Constructor _/  
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);  
}  
/_ Registers an object to listen for events from the KeyDetection class  
\*  
\* param The object that will listen for the events  
_/  
public function addListener(listener:Object):Void {  
for (var i:Number = 0; i\<listeners.length; i++) {  
if (listeners[i] == listener) {  
return;  
}  
}  
listeners.push(listener);  
}  
/_ Unregisters an object that is listening for events from the KeyDetection class  
\*  
\* param The object you wish to remove from the listeners list  
_/  
public function removeListener(listener:Object):Void {  
for (var i:Number = 0; i\<listeners.length; i++) {  
if (listeners[i] == listener) {  
listeners.splice(i, 1);  
}  
}  
}  
/_ Adds a key combination to listen for  
\*  
\* param The name you are giving this combination. Note that this is how you will identify which combination  
\* has been pressed.  
\* …  
\* param The key codes that you are part of this combination. Note that they will need to be pressed in the order  
\* that you list them in order for the combination to be fire successfully.  
\*  
\* usage

```
var key_detector = new KeyDetection();
* key_detector.addCombination(“undo”, Key.CONTROL, 90);
*
```
  
\*  
\*/  
public function addCombination(name:String, keyCode1:Number, keyCode2:Number):Void {  
key\_combinations.push(arguments);  
}  
// invokes the onKeyCombination event on all listeners  
private function invokeOnKeyCombination(combo\_name:String):Void {  
for (var i:Number = 0; i\<listeners.length; i++) {  
listeners[i].onKeyCombination(combo\_name);  
}  
}  
private function onKeyDown() {  
var key:Number = Key.getCode();  
cleanKeysPressed();  
if (key != keys\_pressed[keys\_pressed.length-1]) {  
keys\_pressed.push(key);  
}  
checkCombinations();  
}  
private function checkCombinations() {  
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) {  
invokeOnKeyCombination(key\_combinations[j][0]);  
return;  
}  
} else {  
break;  
}  
}  
}  
}  
private function cleanKeysPressed() {  
for (var i:Number = 0; i\<keys\_pressed.length; i++) {  
if (!Key.isDown(keys\_pressed[i])) {  
keys\_pressed.splice(i, (keys\_pressed.length-i));  
}  
}  
}  
}

Now in my program I have used this code:

var keyDet = new KeyDetection();  
keyDet.addCombination(“teclaQ+Derecha”, 39, 81);  
myObj = new Object();  
myObj.onKeyCombination = function(name:String) {  
switch (name) {  
case “teclaQ+Derecha” :  
gotoAndPlay (6);  
break;  
}  
};  
keyDet.addListener(myObj);

What I want you to do is to simultaneously press the Q key and the arrow to the right, the program jumps to Frame 6. But when compiling the program gives me the problem 5007 : AstionScript file must have at least one externally visible definition , the error tells me he is on Frame 1 of KeyDetection.as file.And from here I do not know how to fix the error. I use Flash CS6 , but my file is saved as CS5.

---

<div class="post-metadata">

**Author:** ![zeke\_chan](https://avatars.discourse-cdn.com/v4/letter/z/f14d63/32.png) [@zeke\_chan](https://forum.kirupa.com/u/zeke_chan)\
**Post date:** [August 10, 2015, 2:07pm UTC](https://forum.kirupa.com/t/multiple-key-detection/633601/4 "2015-08-10T14:07:05Z")

</div>

Looking at your code, I’m guessing the problem lies in the KeyDetection.as class file.

In the first line:

```
class KeyDetection {

```

There should be a public keyword before the class keyword like so:

```
public class KeyDetection {

```

The default attribute for a class is set to **internal** … which only allows access to other class files and code within the same directory or folder… this includes sub-folders.

So to allow access of a class file from anywhere within your project, you have to add the **public** keyword before the class declaration.

If that doesn’t help, feel free to zip up your files and send me a private message with your file attached 😄

---

<div class="post-metadata">

**Author:** ![Xooo](https://avatars.discourse-cdn.com/v4/letter/x/b782af/32.png) [@Xooo](https://forum.kirupa.com/u/Xooo)\
**Post date:** [August 10, 2015, 4:02pm UTC](https://forum.kirupa.com/t/multiple-key-detection/633601/5 "2015-08-10T16:02:10Z")

</div>

Hello again. Call me clumsy , but I’m new at this. I’m trying to send a private message with my code, but do not know how to do it … 😥

---

<div class="post-metadata">

**Author:** ![zeke\_chan](https://avatars.discourse-cdn.com/v4/letter/z/f14d63/32.png) [@zeke\_chan](https://forum.kirupa.com/u/zeke_chan)\
**Post date:** [August 11, 2015, 3:12am UTC](https://forum.kirupa.com/t/multiple-key-detection/633601/6 "2015-08-11T03:12:04Z")

</div>

Not a problem =) I’ve sent you a PM with my email address so you could send it there.

---

<div class="post-metadata">

**Author:** ![zeke\_chan](https://avatars.discourse-cdn.com/v4/letter/z/f14d63/32.png) [@zeke\_chan](https://forum.kirupa.com/u/zeke_chan)\
**Post date:** [August 12, 2015, 5:32am UTC](https://forum.kirupa.com/t/multiple-key-detection/633601/7 "2015-08-12T05:32:28Z")

</div>

Hey Xooo, are you still having trouble with your code? Let me know if I can be of help =)

---

<div class="post-metadata">

**Author:** ![Xooo](https://avatars.discourse-cdn.com/v4/letter/x/b782af/32.png) [@Xooo](https://forum.kirupa.com/u/Xooo)\
**Post date:** [August 12, 2015, 11:46am UTC](https://forum.kirupa.com/t/multiple-key-detection/633601/8 "2015-08-12T11:46:45Z")

</div>

Hello, thank you very much for your help. As there was a way to do decided to give me a different animation program, as urged me . Thank you for your help . 😄 😃

---

<div class="post-metadata">

**Author:** ![zeke\_chan](https://avatars.discourse-cdn.com/v4/letter/z/f14d63/32.png) [@zeke\_chan](https://forum.kirupa.com/u/zeke_chan)\
**Post date:** [August 12, 2015, 2:26pm UTC](https://forum.kirupa.com/t/multiple-key-detection/633601/9 "2015-08-12T14:26:48Z")

</div>

Alright =) All the best to you
