16 Replies Latest reply on Jun 24, 2010 12:04 PM by puddytat5000

    TextInput Change Event

    puddytat5000

      Hi, the change event for text inputs will fire when the enter key is pressed. Is this a bug?

        • 1. Re: TextInput Change Event
          skellerXYZ

          The change event will only fire when the text in the TextInput control actually changes, not when the enter key is pressed.  It fires repeatedly as the user types or delete characters.  You can also listen to the "enter" event.

           

           

          <mx:Script>

               <![CDATA[

                         private function textChange(event:Event):void

                         {

                              // user is enter or deleting text

                         }

                        

                         private function cr(event:Event):void

                         {

                              // user pressed "enter" key

                         }

               ]]>

           

          </mx:Script>

           

          <mx:TextInput change="textChanged(event);" enter="cr(event);" />

           

           

           

          • 2. Re: TextInput Change Event
            puddytat5000 Level 1

            In the flex compiler settings if the 'Required Flash Player version' is set to 9 then the events will fire as expected. However when the version is set to 10 the change event will fire when enter is pressed.

             

            Is your version set to 10?

            • 3. Re: TextInput Change Event
              skellerXYZ Level 1

              Actually I have been working on an AIR app. for AIR version 1.5 and the behaviors is as I described.

               

              spence

              • 4. Re: TextInput Change Event
                puddytat5000 Level 1

                I still have this problem, is there any solution?

                • 5. Re: TextInput Change Event
                  daslicht Level 2

                  Thats not working for you :

                   

                  <s:TextInput enter="Alert.show('enter')">

                   

                  ?

                  • 6. Re: TextInput Change Event
                    rootsounds Level 4

                    No surprise, I have run into a number of issues with events (particularly those related to change) not firing as documented. You have to be careful to test these and work around whatever undesired firing that you run into. In this case, it should be easy enough to test.

                    • 7. Re: TextInput Change Event
                      skellerXYZ Level 1

                      I am not sure what is going on in your case but I have a suggestion.  I've attached a little code that may help you work around this issue.  First with the debugger find out what type of event is actually being raised when the enter key is pressed then in your handler you can do something like what I've done in the code below.  I hope this helps.

                       

                      By the way when I tested this example on Flex 3.5 (not AIR) I never got a keyboard event when I pressed ENTER I got the FlexEvent.  So I would use the debugger to find out what type of event is being dispatched when the ENTER key is pressed then deal with it however you need to, dismiss it or whatever.

                       

                      Good luck.

                      spence

                       

                       

                      <mx:Script>

                      <![CDATA[

                       

                                private function itChanged(event:Event):void

                       

                       

                       

                                     if (event is KeyboardEvent)

                                     {

                       

                                          var retKey:KeyboardEvent = event as KeyboardEvent;

                       

                                          if (retKey.keyCode == Keyboard.ENTER)

                                          {

                       

                                               return;

                                          }

                                     }

                       

                                     if (event is FlexEvent)

                                     {

                       

                                          var enterKey:FlexEvent = event as FlexEvent;

                       

                                          if (enterKey.type == FlexEvent.ENTER)

                                          {

                       

                                               return;

                                          }

                                     }

                       

                                     return;

                                     }

                      ]]>

                       

                       

                      </mx:Script>

                      </mx:Script>

                       

                       

                       

                      <mx:TextInput

                       

                      text="Blah" change="itChanged(event);" enter="itChanged(event);"/>

                      • 8. Re: TextInput Change Event
                        skellerXYZ Level 1

                        I just posted a reply that I hope will help.

                         

                        spence

                        • 9. Re: TextInput Change Event
                          puddytat5000 Level 1

                          Thanks a lot for the reply, but after pressing the enter key the change event will fire, the event will be a  flash.events.Event and the type will be 'change'.

                          • 10. Re: TextInput Change Event
                            skellerXYZ Level 1

                            So I have one more thing for you to try.  In my above example give the TextInput control an id.

                             

                            text="Blah" change="itChanged(event);" enter="itChanged(event);"/>

                            <mx:TextInput id="cityNa"

                             

                            Then in your creationComplete or some init function add an event listener for a keyboard event to the TextInput.

                             

                                  cityNa.addEventListener(KeyboardEvent.KEY_DOWN, itChanged);

                             

                            Then when the the enter key is pressed you will get a FlexEvent with type = FlexEvent.ENTER.

                             

                            spence

                            • 11. Re: TextInput Change Event
                              puddytat5000 Level 1

                              ok sorry didnt realize you wanted to call the change function from both the change event and enter even beforet.

                               

                              So Ive tried calling the same change function when from the change event and enter event. The result is that the function gets call twice when the enter key is pressed.

                               

                              I also tried calling the function from the change event and keydown event. and again it gets called twice.

                              • 12. Re: TextInput Change Event
                                rootsounds Level 4

                                My workaround idea:

                                • KeyDown - change OR Enter (test for it)
                                • Cut - change
                                • Paste - change

                                 

                                 

                                On another note, is it just me or is the TextInput.textInput event description contradictory when it comes to deletion?

                                http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/controls/TextInput.h tml?allClasses=1#event:textInput

                                • 13. Re: TextInput Change Event
                                  puddytat5000 Level 1

                                  No its not just you. The textInput event does not fire on deletion like it should, as stated in the documentation.

                                  • 14. Re: TextInput Change Event
                                    rootsounds Level 4

                                    "Dispatched  when the user types, deletes, or pastes text into the control.   No event is dispatched when the user presses the Delete key, or  Backspace key."

                                     

                                    It first says that the event is dispatch when the user deletes text in the control, but then that it does not fire on press of the Delete key or Backspace key which are the main ways of deleting text from a control for the user. The only other that I can think of is a Cut (does it fire on Cut?). Since the primary (or all?) deletion mechanisms are ignored, this description could be written in a much more intuitive manner by removing the "deletes".

                                     

                                    Granted, as I mentioned earlier, these events don't always perform as described anyway.

                                    • 15. Re: TextInput Change Event
                                      puddytat5000 Level 1

                                      The following is my workaround:

                                       

                                      I set the data property of the textInput with the text value each time the change event fires. the next time the event fires I compare the data property with the text property to see if the text has actually changed.

                                       

                                      private function txtEmployeeId_change(e:Event):void {

                                       

                                           if (e.currentTarget.text != e.currentTarget.data)

                                                clearEmployee();

                                       

                                           e.currentTarget.data = e.currentTarget.text;

                                      }

                                      • 16. Re: TextInput Change Event
                                        puddytat5000 Level 1

                                        I have had a few problems with events that seem to be specific to compilation with flash 10.

                                         

                                        Any idea about this one?

                                        http://forums.adobe.com/thread/631437?tstart=0