# addListener.....Ilyas?

**URL:** <https://forum.kirupa.com/t/addlistener-ilyas/9704>\
**Category:** flash\
**Created:** [December 16, 2002, 11:42pm UTC](https://forum.kirupa.com/t/addlistener-ilyas/9704 "2002-12-16T23:42:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alethos](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/alethos/32/117_2.png) [@alethos](https://forum.kirupa.com/u/alethos)\
**Post date:** [December 16, 2002, 11:42pm UTC](https://forum.kirupa.com/t/addlistener-ilyas/9704/1 "2002-12-16T23:42:41Z")

</div>

Heya.

I saw that Ilyas has a tutorial that mentions he will be posting another tutorial on addListener. I was wondering if anybody can point me to a tutorial for it, or if anybody can explain it for me, with an example?

TIA.

-Al

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 16, 2002, 11:44pm UTC](https://forum.kirupa.com/t/addlistener-ilyas/9704/2 "2002-12-16T23:44:51Z")

</div>

[size=1]_takes out gun_  
_puts silencer_  
_shoots Al_[/size]

You were saying? :beam:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 16, 2002, 11:48pm UTC](https://forum.kirupa.com/t/addlistener-ilyas/9704/3 "2002-12-16T23:48:32Z")

</div>

That was bloody fast…and extremely helpful…

Seriously though, just a quick rundown on how you would use add Listener. I know the syntax, but I don’t really see why it would be used.

_takes off bullet proof vest and tosses it on the ground_

And no more gunplay, k? That hurt.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 16, 2002, 11:54pm UTC](https://forum.kirupa.com/t/addlistener-ilyas/9704/4 "2002-12-16T23:54:14Z")

</div>

OK, OK, so here we go, listeners.

In flash, with the whole new event model, certain objects broadcast messages. Those objects are Mouse, Key, Stage, and Selection. For instance, if you move the mouse, press a key, resize the movie, or select something, a special message will be sent by those objects.

And sometimes you want certain objects to listen to the messages. So you will add them to the list of the listeners of that object. As simple as that. Now if you can show me that piece of code where I used addListener, I can explain a little bit more.

pom 🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 16, 2002, 11:55pm UTC](https://forum.kirupa.com/t/addlistener-ilyas/9704/5 "2002-12-16T23:55:57Z")

</div>

I forgot to say: movie clips (and possibly buttons, I can’t remember) are naturally listening to those messages.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 16, 2002, 11:56pm UTC](https://forum.kirupa.com/t/addlistener-ilyas/9704/6 "2002-12-16T23:56:52Z")

</div>

```php

// _root "listens" to keyboard events
Key.addListener(_root);

// Now that _root. listens, we can define the function
// executed when we press a key :
_root.onKeyDown = function(){

// What was the last key pressed ?
switch(Key.getCode()){
	case 65: trace("A"); break;
	case 66: trace("B");break;
	case 67: trace("C");break;
	case 68: trace ("D");break;
	default: trace ("Not A, B, C nor D");
	}
}

```

There’s your code. Why was adding the listener required here?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [December 17, 2002, 12:08am UTC](https://forum.kirupa.com/t/addlistener-ilyas/9704/7 "2002-12-17T00:08:04Z")

</div>

Because there’s no reason why the \_root should listen to the Key messages 🙂

But to be very honnest, I rarely use listeners (only when I have to). I’ve seen it used in the last components, because it seems to be a very good way to optimize files. :-\
