9 Replies Latest reply on Mar 28, 2011 9:44 AM by bucpatr1

    How to use dispatchEvent() or any other alternative?

    pgpradeep Level 1

      Hi,

       

      I want to programmatically dispatch events to alter the state or data of components.

      For example, to input text into a TextInput control, I tried :

       

      ti.dispatchEvent.apply(ti,[ new TextEvent(TextEvent.TEXT_INPUT,false,false,"sampleText") ]);

      OR

      ti.dispatchEvent(new TextEvent(TextEvent.TEXT_INPUT,false,false,"sampleText"));

       

      WHERE ti is handle of TextInput (id of TextInput).

       

      Neither of these statements were successful in altering the text of the control.

      A simple ti.text = "sampleText"; does alter the text.

       

      However, for a Button component:

       

      btn.dispatchEvent.apply(btn,[ new MousetEvent(MouseEvent.CLICK) ]);

      OR

      btn.dispatchEvent(new MousetEvent(MouseEvent.CLICK));

       

      works and the button is clicked(btn is handle to Button control).

       

      How can I programmatically dispatch events? And where can I get a list of events that can be dispatched for ALL the Flex components?

      For example, event for selecting ComboBox item, event for editing DataGrid cell, etc.

       

      Any help would be greatly appreciated.

       

      Thanks,

      Pradeep.

        • 1. Re: How to use dispatchEvent() or any other alternative?
          Codeflayer

          Hi there,

           

          I'm not quite understanding what you're trying to do and why setting the

          text isn't ideal for you but maybe this will help you out (no gaurantees, I

          haven't had my coffee yet)

           

          Try using TextOperationEvent instead of TextEvent. TextOperationEvent is the

          event that fires when text changes in a text input.

           

          Maybe you'll have better luck doing whatever it is you're trying to do with

          that event instead.

           

          Good luck!

          • 2. Re: How to use dispatchEvent() or any other alternative?
            JoshBeall Level 1

            pgpradeep wrote:

             

            Hi,

             

            I want to programmatically dispatch events to alter the state or data of components.

            For example, to input text into a TextInput control, I tried :

             

            ti.dispatchEvent.apply(ti,[ new TextEvent(TextEvent.TEXT_INPUT,false,false,"sampleText") ]);

            OR

            ti.dispatchEvent(new TextEvent(TextEvent.TEXT_INPUT,false,false,"sampleText"));

            I am not familiar with the Flex internals, but I do not think this is possible.  I suspect that the TextEvent is not used by the TextInput to determine when the user has made changes.  Rather, when the user changes the text, a TextEvent is dispatched that notifies *other* components of the change.

             

            pgpradeep wrote:

             

            However, for a Button component:

             

            btn.dispatchEvent.apply(btn,[ new MousetEvent(MouseEvent.CLICK) ]);

            OR

            btn.dispatchEvent(new MousetEvent(MouseEvent.CLICK));

             

            works and the button is clicked(btn is handle to Button control).

            What makes you say that the "button is clicked"?  I guess it all depends on what you mean by "clicked."  If you mean "the user clicked on the button" then in your example, the button is not clicked, because the user did not click on the button.  If, however, "clicked" means "the event handler for the button's click event is called" then yes, with that definition, the button was clicked.

             

            I'm not sure what your use case is and why using the various component methods/properties directly is not acceptable (e.g., simply setting the "text" property on a TextInput--which, as you noted, works fine).  Can you articulate why this would not work for you?

             

            You may also wish to look into the Flex automation APIs.  I'm not familiar with exactly how it works, but it's how FlexMonkey allows you to record user interaction and play it back.  It may do what you need.  Google Flex automation APIs and FlexMonkey and maybe you will find what you need.

             

              -Josh

            • 3. Re: How to use dispatchEvent() or any other alternative?
              Flex harUI Adobe Employee

              There is no way to fake keystrokes to MX controls, but you can to Spark

              controls.

              • 4. Re: How to use dispatchEvent() or any other alternative?
                pgpradeep Level 1

                Hi,

                 

                I need to playback actions in Flex, thats why I need to dispatch events and not set properties directly.

                 

                I know the button is getting clicked because the registered event-listener is getting executed.

                 

                I think we can simulate keystrokes for ActionScript 3.0. So, it should be possible for MX controls.

                I need to simulate actions for all components, as I am writing a playback agent.

                I am not using the Automation files, as they need pre-compilation.

                 

                I am able to execute button-click, as already mentioned. I am able to open the combobox dropdown, but other than these, I am finding it very difficult to playback actions.

                 

                I require the action-component list.

                So, if anyone can give me the list of events associated with each component, I would be grateful.

                 

                Thanks,

                Pradeep.

                • 5. Re: How to use dispatchEvent() or any other alternative?
                  EvyatarBH Level 3

                  After setting the TextInput's text property value, some event should probably be dispatched automatically.

                  How about listening and handling these events?

                   

                  However, it's probably recommended to check the last comment on the following thread -

                  http://www.actionscript.org/forums/showthread.php3?t=206005

                  (Although I'm not sure if it's still valid and relevant... I just googled "flex textinput change event")

                   

                  Good luck!

                  • 6. Re: How to use dispatchEvent() or any other alternative?
                    JoshBeall Level 1

                    pgpradeep wrote:

                     

                    I need to playback actions in Flex, thats why I need to dispatch events and not set properties directly.

                    You definitely want to look at FlexMonkey, then, as recording and playing back UI activity is exactly what it does.

                     

                    pgpradeep wrote:

                     

                    I know the button is getting clicked because the registered event-listener is getting executed.

                    Ok, so you're defining "clicked" as meaning "the registered event-listener is executed."  If you carry that logic to the TextInput, then "typed in the TextInput" should simply mean "the registered event listener for the TextInput got executed."

                     

                    Just as the button would not actually need to have the mouse cursor hover and then depress the button in order for your event listener to get executed, so the TextInput would not actually need to have the text in it change.  However, your first post made it sound like you wanted the text to change...

                    • 7. Re: How to use dispatchEvent() or any other alternative?
                      pgpradeep Level 1

                      I dont want to use Automation APIs as they require pre-compilation.

                      So if FlexMonkey uses Automation APIs, then I would not want to follow that path.

                       

                      When I execute the textInput event, I want the text to be displayed on the TextImput control and for all associated event-listeners to be executed.

                      That is event-driven programming, a model that Flex follows.

                       

                      So, I want to replay events, but not using Automation APIs.

                       

                      Thanks,

                      Pradeep.

                      • 8. Re: How to use dispatchEvent() or any other alternative?
                        JoshBeall Level 1

                        Well, let us know if you figure it out!  Sounds like an interesting idea.

                         

                        To get back to your button example: the point I'm trying to illustrate is that simply because your registered click handler fires doesn't mean that the code internal to the button that fires when a click happens will also fire. That may or may not be ok, depending on what you're attempting to do.  If you want to fully play back exactly what happened when the user actually interacted with the component, I'm guessing that is *not* ok.

                         

                        Make sense?

                        • 9. Re: How to use dispatchEvent() or any other alternative?
                          bucpatr1 Level 2

                          I actually ran into something similar to this when trying to write FlexUnit tests for a TextInput with two way binding. Since flex only triggers the two way binding when the TextInput changes due to "user interaction" simply setting the text property won't get the job done. What you need to do is break the text change you want to simulate into two phases. In phase 1 you set the text property of the Input and watch for the ValueCommit event. Once the value commit fires, the text value has been entered into the input but none of the display changes or binding will have occurred. To complete the simulation you have to 'trick' flex into thinking the user has made a change to the field. To do that you dispatch a TextOperationEvent from the TextInput you are working with. If you simulate both phases correctly, you should be able to listen for the 'change' event to be dispatched from the TextInput just like you would if the user had actually interacted with the field. Hope that helps.

                           

                          PS. Here is an example of the syntax for the TextOperationEvent (target is the instance of TextInput you are simulating interaction with):

                           

                          SkinnableTextBase(target).textDisplay.dispatchEvent(new TextOperationEvent(TextOperationEvent.CHANGE, false, false, new FlowOperation(SkinnableTextBase(target).textDisplay.textFlow)));