# Key handler does not see global objects

**URL:** <https://forum.kirupa.com/t/key-handler-does-not-see-global-objects/306117>\
**Category:** flash\
**Created:** [March 10, 2010, 12:00am UTC](https://forum.kirupa.com/t/key-handler-does-not-see-global-objects/306117 "2010-03-10T00:00:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rudeman](https://avatars.discourse-cdn.com/v4/letter/r/bcef8e/32.png) [@rudeman](https://forum.kirupa.com/u/rudeman)\
**Post date:** [March 10, 2010, 12:00am UTC](https://forum.kirupa.com/t/key-handler-does-not-see-global-objects/306117/1 "2010-03-10T00:00:31Z")

</div>

I’m making mobile aplication in Flash Lite 3.0. I’ve wanted to add key event handling, when some problem occured. Below is my class, which controls main menu with three buttons. Function onKey() is an event handler, but unfortunently it does not detect any global objects (e.g. currentButton or Butons). Only trace() and local code are working. How can I deal with it?

```auto
class MainMenuInit
{
    var i:Number;
    
    var Buttons:Array;
    var currentButton:Number;
    var keyListener:Object;
    
    var objectLocator:ObjectLocator;
    
    function MainMenuInit(objectLocator:ObjectLocator)
    {
        this.objectLocator = objectLocator;
        Buttons = [];
        init();
        handlers();
    }
    
    function init():Void
    {
        var mainMenu = _root.attachMovie('MainMenu', 'mainMenu', 1,{_x:0, _y:0});
        
        currentButton = 0;
        
        if( Buttons.length == 0)
            for(i = 1; i<4; i++)
                Buttons.push(mainMenu["Button"+i]);
                                                 
        Buttons[0].gotoAndStop(2);
        Buttons[1].gotoAndStop(3);
        Buttons[2].gotoAndStop(1);
        
    }
    
    function handlers():Void
    {
        keyListener = new Object();
        keyListener.onKeyDown = onKey;
        Key.addListener( keyListener );
    }
    
    function onKey():Void
    {
        if( currentButton < Buttons.length - 1 )
        {
            Buttons[currentButton].gotoAndStop(1);
            currentButton++;
            if(Buttons[currentButton].currentFrame == 3) 
                currentButton++;
            Buttons[currentButton].gotoAndStop(2);
        }
        trace(currentButton);
    }

}

```
