3 Replies Latest reply on Sep 23, 2010 8:36 AM by Bob Stucky

    Keypress event

    DavideBarranca Level 1

      Hello,

      I'm having troubles trying to catch a key-press, I can't understand what's going wrong...

       

      My extension has a button that opens a NativeWindow component:

       

      private var newWindow:myNativeWindow;

      public function buttonClick():void

      {

      newWindow = new myNativeWindow();

      newWindow.open();

      newWindow.addEventListener(KeyboardEvent.KEY_UP, myFunction)

       

       

      }

       

      Well, it doesn't work, myFunction is never called (I'd like to intercept ESC and return, but for testing any keypress is ok)

      I've tried to write the event listener inside the Window component in a creationComplete function, with no luck.

      I'm surely missing something, but I can't figure out what.

      Thanks for your help,

       

      Davide

        • 1. Re: Keypress event
          Bob Stucky Adobe Employee

          Hi Davide

           

          What does myNativeWindow (the class file) look like?

           

          Bob

          • 2. Re: Keypress event
            DavideBarranca Level 1

            Hello Bob,

            here it is (still under construction, but the scheme is this one):

             

            <?xml version="1.0" encoding="utf-8"?>

            <mx:Window xmlns:mx="http://www.adobe.com/2006/mxml"

               layout="absolute"

               width="300"

               height="200"

               resizable="false"

               systemChrome = "standard"

               type = "utility"

               transparent="false" title="WARNING!"

              

              creationComplete="window1_creationCompleteHandler(event)" >

            <mx:Script>

            <![CDATA[

            import mx.events.FlexEvent;

            import mx.core.Window;

             

            protected function window1_creationCompleteHandler(event:FlexEvent):void

            {

            addEventListener(KeyboardEvent.KEY_UP, chiudi)

            }

             

            private function chiudi(event:KeyboardEvent):void

            {

            this.close();

            }

            ]]>

            </mx:Script>

            <mx:Button x="26" y="141" label="STOP"/>

            <mx:Button x="163" y="141" label="GO" click="chiudi"/>

            <mx:Label x="57" y="68" text="There's no active selection!" height="22" width="161"/>

            <mx:Label x="92" y="98" text="Continue anyway?"/>

            <mx:Label x="133" y="42" text="WARNING"/>

            </mx:Window>

            • 3. Re: Keypress event
              Bob Stucky Adobe Employee

              Hi Davide,

               

              A personal preference is to include they key handler in the mx:Window tag:

               

              <mx:Window keyDown="myKeyHandler( event )"....>

               

              Adding an event listener does the same thing, though.

               

              Here's the keyHandler that worked for me:

               

                          private function keyHandler( event: KeyboardEvent ): void
                          {
                              trace( "Keypressed: " + String.fromCharCode( event.charCode ) );
                              if ( event.keyCode == Keyboard.ESCAPE ) {
                                  trace( "Escape Pressed!" );
                                  dispatchEvent( new Event(ESCAPE_KEY_DOWN) );
                              }
                          }

                          private function onCreationComplete(): void
                          {
                              addEventListener( ESCAPE_KEY_DOWN, chiudi );
                          }
                          private function chiudi( event: Event = null ):void 
                          {  trace( "closing" );
                              this.close(); 
                          }

               

              One thing to note is that to get key presses, the window has to be open, active, with some control somewhere in the window selected/active. I was a little surprised that I never received a key down event for an escape key unless the window type was set to CSXSWindowType.MODAL_DIALOG.

               

              Bob