# How to fire off actions when a key is pressed

**URL:** <https://forum.kirupa.com/t/how-to-fire-off-actions-when-a-key-is-pressed/262930>\
**Category:** flash\
**Created:** [June 11, 2008, 7:00am UTC](https://forum.kirupa.com/t/how-to-fire-off-actions-when-a-key-is-pressed/262930 "2008-06-11T07:00:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gloopy](https://avatars.discourse-cdn.com/v4/letter/g/82dd89/32.png) [@gloopy](https://forum.kirupa.com/u/gloopy)\
**Post date:** [June 11, 2008, 7:00am UTC](https://forum.kirupa.com/t/how-to-fire-off-actions-when-a-key-is-pressed/262930/1 "2008-06-11T07:00:57Z")

</div>

This bit of ActionScript randomly places MCs. I’d like to create a method to allow the MCs to be removed when the user presses the ‘C’ button.

Where the key value [comes from here](http://livedocs.adobe.com/flash/mx2004/main_7_2/wwhelp/wwhimpl/common/html/wwhelp.htm?context=Flash_MX_2004&file=00001113.html#71302).

I had a look on google and found the method used below… But it doesn’t work.

I just wondered if anyone knows of how to fire off something based on when a user presses a certain key.

Thanks

* * *

```auto
//
function setupTiles() {
    for (var i = 0; i<30; i++) {
        _root.tile.duplicateMovieClip("tile"+i, i+1);
        _root["tile"+i]._x = Math.random()*550+20;
        _root["tile"+i]._y = Math.random()*320+20;
        _root["tile"+i]._xscale = Math.random()*50+10;
        _root["tile"+i]._yscale = Math.random()*90+8;
        _root["tile"+i]._alpha = Math.random()*100+1;
        _root["tile"+i]._rotation = Math.random()*45+10;
    }
}
//
setupTiles();
//
var myListener:Object=new Object();
myListener.onKeyUp = function () {
if(Key.getCode()==67){
    for (var i in this._parent) {
        if (typeof (this._parent*) == "movieclip" && this._parent*._name != "tile") {
            this._parent*.removeMovieClip();
        }
    }
}
}
Key.addListener(myListener);

```

---

<div class="post-metadata">

**Author:** ![claudio](https://avatars.discourse-cdn.com/v4/letter/c/7cd45c/32.png) [@claudio](https://forum.kirupa.com/u/claudio)\
**Post date:** [June 11, 2008, 11:06am UTC](https://forum.kirupa.com/t/how-to-fire-off-actions-when-a-key-is-pressed/262930/2 "2008-06-11T11:06:07Z")

</div>

The onKeyUp event handler is being invoked correctly, you just have a scope issue in your for…in loop, as _this.\_parent_ should return _undefined_.

Here’s one solution.

Cheers.

```auto

var myListener:Object=new Object();
myListener.ref = this;
myListener.onKeyUp = function () {
if(Key.getCode()==67){
	var ref = this.ref;
    for (var i in ref) {
        if (typeof (ref*) == "movieclip" && ref*._name != "tile") {
            ref*.removeMovieClip();
        }
    }
}
}
Key.addListener(myListener);

```

---

<div class="post-metadata">

**Author:** ![gloopy](https://avatars.discourse-cdn.com/v4/letter/g/82dd89/32.png) [@gloopy](https://forum.kirupa.com/u/gloopy)\
**Post date:** [June 11, 2008, 9:58pm UTC](https://forum.kirupa.com/t/how-to-fire-off-actions-when-a-key-is-pressed/262930/3 "2008-06-11T21:58:07Z")

</div>

Thanks Claudio - that worked!

---

<div class="post-metadata">

**Author:** ![claudio](https://avatars.discourse-cdn.com/v4/letter/c/7cd45c/32.png) [@claudio](https://forum.kirupa.com/u/claudio)\
**Post date:** [June 12, 2008, 1:32am UTC](https://forum.kirupa.com/t/how-to-fire-off-actions-when-a-key-is-pressed/262930/4 "2008-06-12T01:32:45Z")

</div>

Glad I could help.
