1 Reply Latest reply on Aug 14, 2010 2:32 AM by MarkTaylor46

    Keyboard Event!!!!

    Yue_Hong Level 1

      Hi,

       

      I want to create a keyboard event listener in flex air application. When I try to run the application it popup a message and says:

       

      TypeError: Error #1009: Cannot access a property or method of a null object reference.
          at Main/windowedapplication1_creationCompleteHandler()[F:\Flex Project\experiment\Capture Keyboard Input\src\Main.mxml:19]
          at Main/___Main_WindowedApplication1_creationComplete()[F:\Flex Project\experiment\Capture Keyboard Input\src\Main.mxml:4]
          at flash.events::EventDispatcher/dispatchEventFunction()
          at flash.events::EventDispatcher/dispatchEvent()
          at mx.core::UIComponent/dispatchEvent()[E:\dev\4.0.0\frameworks\projects\framework\src\mx\co re\UIComponent.as:12266]
          at mx.core::UIComponent/set initialized()[E:\dev\4.0.0\frameworks\projects\framework\src\mx\core\UIComponent.as:1577]
          at mx.managers::LayoutManager/doPhasedInstantiation()[E:\dev\4.0.0\frameworks\projects\frame work\src\mx\managers\LayoutManager.as:759]
          at mx.managers::LayoutManager/doPhasedInstantiationCallback()[E:\dev\4.0.0\frameworks\projec ts\framework\src\mx\managers\LayoutManager.as:1072]

       

       

      I don't know what I did wrong.

      Here is my application code:

       

       

      <?xml version="1.0" encoding="utf-8"?>
      <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                             xmlns:s="library://ns.adobe.com/flex/spark"
                             xmlns:mx="library://ns.adobe.com/flex/mx" creationComplete="windowedapplication1_creationCompleteHandler(event)">
          <fx:Declarations>
              <!-- Place non-visual elements (e.g., services, value objects) here -->
          </fx:Declarations>
          <fx:Script>
              <![CDATA[
                  import mx.events.FlexEvent;
                  public function handleKeyDown(event:KeyboardEvent) :void
                  {
                      display.text = "Key was pressed: " + event.keyCode;
                  }

       

                  protected function windowedapplication1_creationCompleteHandler(event:FlexEvent):void
                  {
                      stage.addEventListener(KeyboardEvent.KEY_DOWN, handleKeyDown);
                  }

       

              ]]>
          </fx:Script>
          <s:Label id="display"/>
      </s:WindowedApplication>

       

       

      Hope you can help me to find out why it didn't work. Thanks.