4 Replies Latest reply on Feb 24, 2009 3:23 AM by Richard_Abbott

    Setting focus to Flex control

    Richard_Abbott Level 3
      Hi all,
      I am currently using the standard AC_FL_RunContent(...) JavaScript method to put my SWF file into the right part of the DOM on a page. On one page it would be nice if the user was able to type straight away into a text input. Calling setFocus() on this control in creation_complete does the trick so far as Flex is concerned, but the Flex app itself is not getting focus. So the page looks as though the text input has focus, but in fact you have to click on the Flex-controlled area in order to actually begin to type.

      Any ideas how I would set focus in a complete sense so the user can just type without clicking?

      Thanks,
      Richard
        • 1. Re: Setting focus to Flex control
          Level 7

          "Richard_Abbott" <webforumsuser@macromedia.com> wrote in message
          news:gnh4c5$i73$1@forums.macromedia.com...
          > Hi all,
          > I am currently using the standard AC_FL_RunContent(...) JavaScript method
          > to
          > put my SWF file into the right part of the DOM on a page. On one page it
          > would
          > be nice if the user was able to type straight away into a text input.
          > Calling
          > setFocus() on this control in creation_complete does the trick so far as
          > Flex
          > is concerned, but the Flex app itself is not getting focus. So the page
          > looks
          > as though the text input has focus, but in fact you have to click on the
          > Flex-controlled area in order to actually begin to type.
          >
          > Any ideas how I would set focus in a complete sense so the user can just
          > type
          > without clicking?

          You can probably find some javascript code that will work for some browsers.


          • 2. Re: Setting focus to Flex control
            Richard_Abbott Level 3
            Hi Amy,
            looks like you are right about this, and moreover we are back in browser-dependent land. The following articles deal with it a bit...
            http://www.robrusher.com/archives/34
            https://bugs.adobe.com/jira/browse/FP-879
            http://www.adobe.com/livedocs/flex/3/html/help.html?content=passingarguments_5.html

            but it seems there is no generally-valid way to do it. Thanks anyway for the tip,

            Richard
            • 3. Re: Setting focus to Flex control
              RUSH-ME Level 3
              Hi Richard
              Thanks for your contribution.
              I was facing the same problem. Now its running fine.
              Thanks again

              • 4. Re: Setting focus to Flex control
                Richard_Abbott Level 3
                Just to tidy this up, I found some code that works for IE and Firefox (at least on Windows, haven't yet tested elsewhere). The problems are:
                1) IE and FF refer to the Flex application in different ways (as documented in the Flex doc under ExternalInterface)
                2) The sequence of start-up operations is different, so FF does not have the Flex app ready to be queried until later.
                #2 means that if you just stick some code in directly after the AC_FL_RunContent(...) call then IE works but FF does not. In the end I did something more long-winded which at least has the advantage of working. Into the HTML (a convenient place is in the script block that has
                // Globals
                // Major version of Flash required
                etc) I put the following
                window.onload = function()
                {
                if (navigator.appName.indexOf("Microsoft") != -1)
                {
                // IE
                window["Logon"].flexEnsureFocus();
                } // if Microsoft
                else
                {
                // other browser
                do_other_focus();
                } // else
                };

                function do_other_focus()
                {
                if (document["Logon"] == null)
                {
                window.setTimeout(do_other_focus(), 50);
                } // if (document["Logon"] == null)
                else
                {
                try
                {
                document.getElementsByName("Logon")[0].focus();
                document["Logon"].flexEnsureFocus();
                } // try
                catch (e)
                {
                window.setTimeout("document['Logon'].flexEnsureFocus()", 50);
                } // catch
                } // else
                } // do_other_focus

                where flexEnsureFocus is the external name of a Flex function that calls userNameTextInput.setFocus();

                A bit hacky, perhaps, and it still does not extend to working on Safari or Opera, but it's a step in the right direction...
                Richard