# KeyboardEvent problem

**URL:** <https://forum.kirupa.com/t/keyboardevent-problem/324949>\
**Category:** Uncategorized\
**Created:** [September 17, 2011, 6:16am UTC](https://forum.kirupa.com/t/keyboardevent-problem/324949 "2011-09-17T06:16:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![felastine](https://avatars.discourse-cdn.com/v4/letter/f/50afbb/32.png) [@felastine](https://forum.kirupa.com/u/felastine)\
**Post date:** [September 17, 2011, 6:16am UTC](https://forum.kirupa.com/t/keyboardevent-problem/324949/1 "2011-09-17T06:16:13Z")

</div>

Hi there.

I’m doing a test on AS3 event system.

```auto

import flash.events.KeyboardEvent;
function detectKeyDown(e:KeyboardEvent):void {
    trace(e.keyCode);
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, detectKeyDown);

```

The above code is on the main timeline. It works and traces every key press I made. But on line 5, if I remove “stage.” before addEventListener(…), I can’t trace a thing.

```auto

package {
    import flash.display.Sprite;
    import flash.events.KeyboardEvent;
    public class ShroudClearer extends Sprite {
        public function ShroudClearer(xPos:Number,yPos:Number) {
            x = xPos;
            y = yPos;
            addEventListener(KeyboardEvent.KEY_DOWN, detectKeyDown);
        }
        public function detectKeyDown(e:KeyboardEvent):void {
            trace(e.keyCode);
            x++;
            y++;
        }
    }
}

```

I created a class, created an instance of this class by:  
**var myShroudClearer:ShroudClearer = new ShroudClearer(0,0);**  
on timeline.

Nothing happens when I press any key.

So WHY, for the former and latter cases?

Thanks in advance.  
Felastine.
