2 Replies Latest reply on Jun 9, 2011 6:21 AM by AhmedCogenli

    Strange problem with keyboard event listener

    AhmedCogenli

      I have a full screen touchscreen app. For closing application I've set a keyboard event listener for "0" key. I don't want the user press Alt+F4 key combination due to some restrictions. Problems starts when the application loses the focus. Here is the code;

       

                  protected function onComplete(event:FlexEvent):void
                  {

                      this.setFocus();
                      this.addEventListener(KeyboardEvent.KEY_DOWN,trapKeys,true, 0, true );

                  }

       

                  private function trapKeys(e:KeyboardEvent):void {
                                     
                      if (e.keyCode == 96) {
                          this.close();
                      }
                     
                      else if (e.keyCode == 48) {
                          this.close();
                      }
                             
                  }

       

      When user change the screen to a different app with Alt+TAB or any other key combination flex app loses the focus and when he turns back to my app "0" doesn't work. How can I solve this issue?

       

      Thanks in advance.

        • 1. Re: Strange problem with keyboard event listener
          Peter Blazejewicz Level 4

          Hi Ahmed,

           

          I've absolutely no issue with solution you're trying to implement - are you using full screen with keyboard interactive mode?

          Please consider:

          // on complete add keyboard listener

          protected function applicationCompleteHandler(event:FlexEvent):void

          {

               this.stage.addEventListener(KeyboardEvent.KEY_DOWN, trapKeys);

          };

          //          

          protected function applicationActivateHandler(event:AIREvent):void

          {

               if(this.stage)

               {

                    // switch display state to full screen when activated

                    if(this.stage.displayState != StageDisplayState.FULL_SCREEN_INTERACTIVE)

                    {

                         this.stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;

                    };

               };

          };

          //

          private var exitHandled:Boolean = false;

          // on NUMBER_0 || NUMPAD_0 exit/close

          protected function trapKeys(event:KeyboardEvent):void

          {

               if((event.keyCode == Keyboard.NUMBER_0

                    || event.keyCode == Keyboard.NUMPAD_0)

                    && this.exitHandled == false)

               {

                    exitHandled = true;

                    this.stage.removeEventListener(KeyboardEvent.KEY_DOWN, trapKeys);

                    this.close();

               };

          }

          (note: applicaton "applicatonComplete" and "applicationActivate" events are routed to above handlers)

           

          regards,

          Peter

          • 2. Re: Strange problem with keyboard event listener
            AhmedCogenli Level 1

            Peter, thanks a lot, your code works perfect. I think applicationActivateHandler does the trick. Thanks a lot