5 Replies Latest reply on Oct 20, 2010 9:36 PM by BhaskerChari

    How to dispatch keyboard escape fake event ?

    Mohit Verma1234

      Hi guys

       

       

       

        i stuck in a problem . actually i want to know if there is any way to dispatch fake escape event in flex.

       

        i mean dispatching a custom event of pressing the keyboard escape key fakely.

       

       

      Thanks and regards

        Mohit

        • 1. Re: How to dispatch keyboard escape fake event ?
          BhaskerChari Level 4

          Hi Mohith,

           

          You can try this code...

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();">
          <mx:Script>
            <![CDATA[
             import mx.effects.Fade;
             import mx.controls.Alert;
            
             public function init():void
                {
                 this.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
                }
                public function onKeyDown(event:KeyboardEvent):void
                {
                 if(event.keyCode == Keyboard.ESCAPE)
                 {
                  Alert.show("Esc key pressed : keyCode =" + event.keyCode + "");
                 }
                }
                   private function fakeEscKeyEvent():void {
              var keyEvent:KeyboardEvent = new KeyboardEvent(KeyboardEvent.KEY_DOWN, true, false, Keyboard.ESCAPE, Keyboard.ESCAPE);
              this.dispatchEvent(keyEvent);
             }

            ]]>
          </mx:Script>

          <mx:Button label="Fake Esc Key Press" click="fakeEscKeyEvent();" />

          </mx:Application>

           

           

           

          Thanks,

          Bhasker

          1 person found this helpful
          • 2. Re: How to dispatch keyboard escape fake event ?
            Mohit Verma1234 Level 1

            Hi Bhasker

             

             

            Thanks for replying . The code that u have provided works but what is my problem is actually i am dispatching the fake event on mouseout of main application

              in air.

             

            Because i am using menubar component  mx:menubar . so what i want to do is actually close the menubar component submenu popup  on mouseout by dispactching the fake escape event

             

            i have implemented the code that u have provided but it is not closing menubar.'

             

             

             

            Thanks and Regards

              Mohit

            • 3. Re: How to dispatch keyboard escape fake event ?
              BhaskerChari Level 4

              Hi Mohith,

               

              You need to manually hide the menus when you mouseOut of the application something like as shown below...

               

              You need to get the index of the menu opened and need to hide the menu based on the index..

               

              var menu:Menu = menuBar.getMenuAt(0); //This is for first menu, you need to check for which menu was opened

               

              menu.hide();

               

              But you need to somehow manage and find when your menu is mouseOut and then execute the above code...

               

               

              Thanks,

              Bhasker

              • 4. Re: How to dispatch keyboard escape fake event ?
                Mohit Verma1234 Level 1

                Hi BhaskerChari

                 

                Now it works.

                 

                 

                Thanks for answering and helping.

                 

                Thanks and regards

                Mohit

                • 5. Re: How to dispatch keyboard escape fake event ?
                  BhaskerChari Level 4

                  Hi Mohit,

                   

                   

                  Does it worked with the menu.hide() approach....?? How did u manage your mouseOut event on the Application ..Can you share the sample working code...:)

                   

                  Can you please mark this post as answered.

                   

                   

                  Thanks,

                  Bhasker