tags:

views:

62

answers:

1

In my AIR application I am having problems catching dispatched events in my eventmap. The class that is dispatching the events looks like this:

Shortcuts.as

[Event(name="centeredZoomOut", type="flash.events.Event")]

public class Shortcuts extends EventDispatcher
{
    // Event Names
    public static const CENTERED_ZOOM_OUT:String = "centeredZoomOut";

    public function Shortcuts(target:IEventDispatcher=null)
    {
        super(target);
        NativeApplication.nativeApplication.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
    }

    private function onKeyUp(event:KeyboardEvent):void
    {
        this.dispatchEvent(new Event(CENTERED_ZOOM_OUT, true));
    }
}    

I know that the event is getting dispatched from debugging it, but it is not getting caught by the following eventmap.

ShortcutMap.mxml

<?xml version="1.0" encoding="utf-8"?>
<EventMap 
xmlns="http://mate.asfusion.com/"
xmlns:mx="http://www.adobe.com/2006/mxml"&gt;

<mx:Script>
    <![CDATA[
        import mx.controls.Alert;
        import mx.events.FlexEvent;
        import models.Shortcuts;
    ]]>
</mx:Script>

<EventHandlers type="{ Shortcuts.CENTERED_ZOOM_OUT }">
    <MethodInvoker generator="{ShortCutExample}" method="showAlert" arguments="{['centeredZoom']}" />
</EventHandlers>

Here is the main application file called "ShortCutExample"

ShortCutExample.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication 
xmlns:mx="http://www.adobe.com/2006/mxml" 
xmlns:maps="maps.*"
layout="absolute"  
creationComplete="init()">

<mx:Script>
    <![CDATA[
        import mx.controls.Alert;
        import models.Shortcuts;

        private var shortcuts:Shortcuts;

        private function init():void
        {
            this.shortcuts = new Shortcuts();
        }

        public function showAlert(value:String):void
        {
            Alert.show(value);
        }
    ]]>
</mx:Script>
<maps:ShortcutMap/>

</mx:WindowedApplication>

Why is my eventmap not catching the event?

+1  A: 

Because I was not adding the object to the display list and it did not extend DisplayObject the dispatched events were not being caught by the eventmap. To solve this problem create a private variable of type GlobalDispatcher and dispatch your events from that variable.

private var dispatcher:GlobalDispatcher = new GlobalDispatcher();
...
this.dispatcher.dispatchEvent(new ShortCutEvent(ShortCutEvent.CENTERED_ZOOM_OUT, true));
asawilliams