KeyboardEvents inside classes (not a movieclip)

Hello!

All I want is to create a class to treat users keyboard entries, and store it on public vars for all other classes to read it.

I’d like to know:
to get the keyboardEvent working inside a class, do I need my class to extend MovieClip? There is not other way?

I mean. It must be imported to stage, even if the stage itself wont use the class?

The following is working. But how to use it without extending MC class?

this is the class…

package lib {
    
    import flash.events.*;
    import flash.display.MovieClip;
    
    public class KeyboardInput extends MovieClip {
        
        // Constants:
        // Public Properties:
        public var userKeyHistory:String;
        // Private Properties:
        
        // Initialization:
        public function KeyboardInput(){
            
            userKeyHistory = "0"
            addEventListener(Event.ADDED_TO_STAGE,function(ev:Event):void{
                stage.addEventListener(KeyboardEvent.KEY_DOWN,function(e:KeyboardEvent):void{
                    trace(String.fromCharCode(e.charCode))
                });
            });
                    
        }
        
        // Public Methods:

        
        // Protected Methods:
    }
    
}

i need to write this on first frame of the main fla… (?)

import lib.KeyboardInput;

var key:KeyboardInput = new KeyboardInput();
addChild(key)

;

once is added to stage, the class KeyboardInput will work and thus store inside userKeyHistory. The point is that I was trying to avoid deppending on adding it to the stage…
maybe, is there a way to extend EventDispatcher… I dunno.

Thanks for showing the way :wink:

Btp~