10 Replies Latest reply on Jul 8, 2009 11:00 AM by oldMster

    Catch every mouse click event in application

    twitskull

      Hi,

       

      I would like to globaly catch every mouse click event within my Flex application. Also I would like to get ID of the element (checkbox, button, etc)? Elements in my application are static but there are many. So writing functions for all the elements is not an option. Is there a way to catch every click event?

       

       

      Thnx in advance

        • 1. Re: Catch every mouse click event in application
          _Natasha_ Level 4

          Hi,

          it's very easy.

          Add click event handler for Application and you'll get all events if they don't stoped (using event.stopPropagation())

          • 2. Re: Catch every mouse click event in application
            Gregory Lafrance Level 6

            You could try addEventListener() on the systemManager.

            • 3. Re: Catch every mouse click event in application
              twitskull Level 1

              I figured it out. But the problem is that I can't get the ID of component clicked. If I have checkbox in my application with id chk1 I would need to get ID of it so I can do something with it (like disable it).

              • 4. Re: Catch every mouse click event in application
                RWinscot Level 1

                It sounds to me like you should be using the Flex Automation Framework... since that's what it was intended for. There is also the issue of capturing id's - which in most cases are generated at runtime. Again the framework helps you manage that slippery slope.

                 

                http://livedocs.adobe.com/flex/3/html/help.html?content=functest_components2_10.html

                 

                Cheers,

                 

                Rick Winscot

                • 5. Re: Catch every mouse click event in application
                  _Natasha_ Level 4

                  event.target is the first object user clicked.

                  • 6. Re: Catch every mouse click event in application
                    RWinscot Level 1

                    Not always... it's better to think of event.target as the one that dispatched the event. That's why there is a reference to event.currentTarget as well.

                     

                    Cheers,

                     

                    Rick Winscot

                    • 7. Re: Catch every mouse click event in application
                      twitskull Level 1

                      With this code I allways get Sandbox0 as event.currentTarget. Sandbox is name of my application. I do not get chk0, chk1, etc

                       

                       

                      <?xml version="1.0" encoding="utf-8"?>
                      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="100%" height="100%" creationComplete="onCreationComplete()">
                          <mx:CheckBox x="152" y="131" label="Checkbox" id="chk0"/>
                          <mx:CheckBox x="264" y="195" label="Checkbox" id="chk1"/>
                          <mx:CheckBox x="483" y="211" label="Checkbox" id="chk2"/>
                          <mx:CheckBox x="174" y="271" label="Checkbox" id="chk3"/>
                          <mx:CheckBox x="383" y="357" label="Checkbox" id="chk4"/>
                         
                          <mx:Script>
                              <![CDATA[
                                 
                                  import mx.controls.Alert;
                                 
                                  private function onCreationComplete():void
                                  {
                                      application.addEventListener(MouseEvent.CLICK, reportClick);
                                  }

                       

                                  private function reportClick(event:MouseEvent):void
                                  {
                                      Alert.show(event.currentTarget.toString() + " dispatches MouseEvent. Local coords [" + event.localX + "," + event.localY + "] Stage coords [" + event.stageX + "," + event.stageY + "]");
                                     
                                  }

                       


                              ]]>
                          </mx:Script>

                       

                         
                      </mx:Application>

                      • 8. Re: Catch every mouse click event in application
                        oldMster Level 3

                        event.target will contain the object that 'fired' the event originally.  event.currentTarget is the object that the event listener is being processed for.   I changed the alert to the following:

                         

                        Alert.show("Target " + event.target.toString() + " currentTarget " + event.currentTarget.toString() + " dispatches MouseEvent. Local coords [" + event.localX + "," + event.localY + "] Stage coords [" + event.stageX + "," + event.stageY + "]");

                         

                        And target indicates ClickTest0.chk0, ClickTest0.chk1, etc. as I click the various check boxes, currentTarget always indicates ClickTest0.  I named my application ClickTest instead of sandbox.

                         

                        If you had set a click event handler on a specific check box, the event would get 'handled' twice.  The first time would be from the handler attached to the checkbox, and the target and currentTarget would be the same.  The second time would be for the handler attached to the application, and target would be the checkbox that fired the event, currentTarget would be the application that got the event as it 'bubbled'.

                        • 9. Re: Catch every mouse click event in application
                          Flex harUI Adobe Employee

                          CurrentTarget is the object you applied the listener to.  Target is the object that dispatched the event.

                           

                          Alex Harui

                          Flex SDK Developer

                          Adobe Systems Inc.

                          Blog: http://blogs.adobe.com/aharui

                          • 10. Re: Catch every mouse click event in application
                            oldMster Level 3

                            To see the multiple handling, you can use this modified code.  In addition, I added code to show how to stop the event from bubbling if you handle it at the checkbox level, and don't want it to be handled by the application level.  clicking the 2nd check box will show that.

                             

                            <?xml version="1.0" encoding="utf-8"?>
                            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="100%" height="100%" creationComplete="onCreationComplete();">
                                <mx:CheckBox x="152" y="131" label="Checkbox" id="chk0" click="reportClick(event);"/>
                                <mx:CheckBox x="264" y="195" label="Checkbox" id="chk1" click="reportClick(event);/>
                                <mx:CheckBox x="483" y="211" label="Checkbox" id="chk2"/>
                                <mx:CheckBox x="174" y="271" label="Checkbox" id="chk3"/>
                                <mx:CheckBox x="383" y="357" label="Checkbox" id="chk4"/>

                             

                                 <mx:Script>        <![CDATA[

                                        import mx.controls.Alert;
                                        private function onCreationComplete():void
                                        {
                                            application.addEventListener(MouseEvent.CLICK, reportClick);
                                        }

                                        private function reportClick(event:MouseEvent):void
                                        {
                                            Alert.show("Target " + event.target.toString() + " currentTarget " + event.currentTarget.toString() + " dispatches MouseEvent. Local coords [" + event.localX + "," + event.localY + "] Stage coords [" + event.stageX + "," + event.stageY + "]");

                                           if (event.currentTarget == chk1)

                                           {

                                                event.stopPropagation();

                                           }
                                          
                                        }

                                    ]]>
                                </mx:Script>

                             

                             

                             

                               
                            </mx:Application>