9 Replies Latest reply on Mar 1, 2011 7:49 AM by Ram77

    KeyboardEvent listener not "hearing" Enter key

    TapEstes

      I had an Adobe AIR 1.5 app where I could use evt.keyCode == Keyboard.ENTER to recognize when the Enter key is pressed.  I'm updating the app to 2.0, but can't seem to get it to recognize the Enter key.  In the following code, I get a trace for pressing ANY key other than the Enter key.  When I press the Enter key I get nothing.  Thoughts?

       

      Thanks,

       

      s

       

      function keyPressed(evt:KeyboardEvent):void{

           trace(evt.keyCode);

      }

       

      stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);

        • 1. Re: KeyboardEvent listener not "hearing" Enter key
          commadelimited Level 2

          It looks like you're using Flash/Flex. Have you tried copying out the pertinent bits of code into a pure Flash or Flex project and testing to make sure they work there? It's possible that this could be a bug in AIR, but not likely.

           

          Have you tried this on another computer to test if it's your machine?

          • 2. Re: KeyboardEvent listener not "hearing" Enter key
            TapEstes Level 1

            Andy,

             

            Thanks for taking the time to get back to me.  I'm using Flash CS5.  I've

            done some more digging and it looks like this is tied to the text field.

            When the text field is a "classic" field, the Enter keystroke is captured

            as expected but when it's a TLF field no joy.  And, as you suspect, this

            doesn't appear to be an AIR issue as it happens when I save it as an

            as3/html project as well.  I read somewhere that turning off

            keyboard shortcuts in CS5 is the ticket, but I haven't had much success

            there.

             

            s

            • 3. Re: KeyboardEvent listener not "hearing" Enter key
              commadelimited Level 2

              I don't do much work in Flash or Flex these days so I only have a vauge notion of what a TLF field is but I seem to recall it's the new version of a rich text field. When you hit the enter key, does the cursor behave like it should? Does it move you to the next line?

               

              But I found another post where someone found a solution for your issue:

              http://www.actionscript.org/forums/showthread.php3?t=247417#post1029860

               

              Please let me know if this helps you out.

              • 4. Re: KeyboardEvent listener not "hearing" Enter key
                TapEstes Level 1

                Andy,

                 

                Thanks much for taking the time to dig up that hint.  Unfortunately, it

                didn't work for me.  I added a listener to the text field and then tested

                for the carriage return.  All other keys on the keyboard registered, but

                when I hit the return key - nada.  However - and this is the part that

                confounds me - the enter key is in fact moving the cursor down to the next

                line.

                 

                To make sure it wasn't the keyboard shortcuts issue I mentioned earlier, I

                went ahead and published as an .AIR app and installed the package.  Still no

                joy.  Looks like the dev environment keyboard shortcuts aren't causing my

                problem either.

                 

                Bewildered,

                 

                s

                • 5. Re: KeyboardEvent listener not "hearing" Enter key
                  commadelimited Level 2

                  It might be worth submitting that as a bug to the Flash team.

                   

                  I'm mystified though...if the cursor moves, there's got to be some event that you can capture.

                  • 6. Re: KeyboardEvent listener not "hearing" Enter key
                    TapEstes Level 1

                    Thanks for the help.  I'll submit a bug report.  Quick question, where

                    should I do that?  When I looked at the adobe bug reporting tool I couldn't

                    find a product category that corresponded to Flash CS5.

                     

                    Thanks again,

                     

                    s

                    • 7. Re: KeyboardEvent listener not "hearing" Enter key
                      commadelimited Level 2

                      Well, technically it's all part of the Flash player so I'd submit the issue to the public Flash player bug database:

                       

                      https://bugs.adobe.com/jira/browse/FP#selectedTab=com.atlassian.jira.plugin.system.project %3Aissues-panel

                      • 8. Re: KeyboardEvent listener not "hearing" Enter key
                        Ram77

                        Ok you can try this. First you need the keyboard listener on the applicationComplete event and not creationComplete event. And then NOT use the constants KEYNAME_LEFTARROW / KEYNAME_RIGHTARROW and so on.. They dont work.. at least for me.

                         

                        Here is a sample code:

                         

                        <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"  width="100%" height="100%" applicationComplete="initKeyBoardListener()">

                        <mx:Script>

                                <![CDATA[

                                       private function initKeyBoardListener():void

                                       {

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

                                       }

                         

                                       private function keyboardNavigate(e:KeyboardEvent):void

                                       {

                                            if(e.keyCode == 37) //37 --> left arrow

                                            {

                                                 // do something;

                                            }

                                            if(e.keyCode == 38) //38 --> up arrow

                                            {

                                                 // do something;

                                            }

                                            if(e.keyCode == 39)  //39 --> right arrow

                                            {

                                                 // do something;

                                            }

                                            if(e.keyCode == 40)  //40 --> down arrow

                                            {

                                                 // do something;

                                            }

                                            if(e.keyCode == 32)  //32 --> spacebar

                                            {

                                                 // do something;

                                            }

                                            if(e.ctrlKey == true) //control key

                                            {

                                                 // do something;

                                            }

                                            if(e.shiftKey == true) //shift key

                                            {

                                                 // do something;

                                            }

                                       }

                        ]]>

                        </mx:Script>

                        <mx:VBox width="100%" height="100%" verticalAlign="middle" horizontalAlign="center">

                             <!-- YOUR COMPONENTS HERE -->

                        </mx:VBox>

                        </mx:WindowedApplication>

                        • 9. Re: KeyboardEvent listener not "hearing" Enter key
                          Ram77 Level 1

                          Ok you can try this. First you need the keyboard listener on the applicationComplete event and not creationComplete event. And then NOT use the constants KEYNAME_LEFTARROW / KEYNAME_RIGHTARROW and so on.. They dont work.. at least for me.

                           

                          Here is a sample code:

                           

                           

                          <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml"  width="100%" height="100%" applicationComplete="initKeyBoardListener()">

                           

                           

                          <mx:Script>

                                  <![CDATA[

                           

                           

                                         private function initKeyBoardListener():void

                                         {

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

                                         }

                           

                           

                           

                                         private function keyboardNavigate(e:KeyboardEvent):void

                                         {

                           

                           

                                              if(e.keyCode == 37) //37 --> left arrow

                                              {

                                                   // do something;

                                              }

                                              if(e.keyCode == 38) //38 --> up arrow

                                              {

                                                   // do something;

                                              }

                                              if(e.keyCode == 39)  //39 --> right arrow

                                              {

                                                   // do something;

                                              }

                                              if(e.keyCode == 40)  //40 --> down arrow

                                              {

                                                   // do something;

                                              }

                                              if(e.keyCode == 32)  //32 --> spacebar

                                              {

                                                   // do something;

                                              }

                                              if(e.ctrlKey == true) //control key

                                              {

                                                   // do something;

                                              }

                                              if(e.shiftKey == true) //shift key

                                              {

                                                   // do something;

                                              }

                           

                                         }

                           

                          ]]>

                          </mx:Script>

                          <mx:VBox width="100%" height="100%" verticalAlign="middle" horizontalAlign="center">

                               <!-- YOUR COMPONENTS HERE -->

                          </mx:VBox>

                           

                          </mx:WindowedApplication>

                           

                           

                           

                           

                          --- Hope this helps

                          Mahesh Iyer