10 Replies Latest reply: Jun 29, 2012 10:39 AM by sinious RSS

    setting the focus from stage to text field

    Domhnall Ó Suibhne

      I am writing a program which requests that a user inputs a word into a text field which appears when a key is pressed. The input text is then checked against an array of  words. This works fine, however if  the stage .focus is changed from the stage to the text field so that there will be a flashing cursor ready in the text box o receive the users input, the input word will not be checked against the array. The program will not run. the  mytextfield.focus  works to the point where the cursor is in the tet field and receives the text etc but it then says that the word is not in the array. It fails to check it properly. Im 3 days trying to sort it and the only way it works is if the user has to click into the box which is what I dont want. Also is there a way of showing the textfield background as white without having an error message about it?

        • 1. Re: setting the focus from stage to text field
          Ned Murphy MVP

          You should show the code that is relevant to your problem

          • 2. Re: setting the focus from stage to text field
            sinious MVP

            Are you running the check on the "change" event of the TextField? If not that'd be how I'd check against your array:

             

            http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextField.ht ml#event:change

             

            Every time it's changed I'd check it.

            • 3. Re: setting the focus from stage to text field
              Domhnall Ó Suibhne Community Member

              Hi again thanks for your attention. Yes I will include my code below. Just briefly my program is designed to analyse a response from a pupil to a word presented. The code for the analysis is not included here. Im getting the basic interactions set up. So the first code below is  code from the Actions associated with a Frame which has a black background and a word presented. In this case the word being presented is "in". If the tester hits the SPACE key then a text box with white background must appear on the bottom left of the frame and accept the pupil's response which is typed in by the tester. The input text  is then compared to words in a small array and the focus which has been changed from the stage to the textfield appears to prevent the text being checked properly in the array.

              This array is in a seperate .as file which will analyse all words with the Vowel "i"  (Again much of the analysis code not included).

               

              This file should run for you so that if you take out the stage.focus=textInIn; leaving the stage.focus=stage;  The program runs correctly, i.e.the word will be located in the array. If however the stage.focus=textInIn; is included in the code, then the cursor will be blinking as required and ready to receive the users input. But the program will then say that the word is not in the array so its preventing the program from working.

               

              Finally there is an error message re the white text background on the textfield that i am unable to change. Thanks for your attention, it is most appreciated.

               

              This is the code below for the Actions Window for Frame 1

               

              import flash.events.KeyboardEvent;

               

               

              stop();

              var myVoweli:Voweli = new Voweli();

              stage.focus = stage;

              mainText.text = myVoweli.vowelI("");

              textInIn.visible = false;

              mainText.visible = false;

              mainText.border = true;

              textInIn.border = true;

              textInIn.type="input";

              textInIn.text = "";

               

               

              textInIn.background = "white";

               

               

              stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);

               

               

              function keyPressed(event:KeyboardEvent):void

              {

                        if (event.keyCode == Keyboard.SPACE)

                        {

                                  stage.focus=textInIn;

               

                                  textInIn.visible = true;

                                  mainText.visible = true;

                                  mainText.text = myVoweli.vowelI(textInIn.text);

               

               

                        }

                        {

                                  if (event.keyCode == Keyboard.ENTER)

                                  {

                                            gotoAndPlay(2);

                                  }

                        }

              };

               

              This is the code below from the .as File in same folder as .fla called Voweli.as

               

              package

              {

                  public class Voweli

                  {

                      /**

                       * Defines the names that receive a proper greeting.

                       */

                      public static var vowelArraysI:Array = [ "in","fix", "mix","tip","kin","wit","sim","sip"];

               

                      /**

                       * Builds a greeting string using the given name.

                       */

                      public function vowelI(textAtI:String = ""):String

                      {

                          var greeting:String;

                          if (textAtI == "")

               

                          {

               

                              greeting = "Please type the pupil's response in the text box, and then press "

                                          + "the SPACE key again.";

               

                          } 

                          else if (vowelArrayI(textAtI)) 

                          {

                              greeting = "Yep thats in the array, " + textAtI + ".";

                          } 

                          else 

                          {

                              greeting = "Sorry " + textAtI + ", is not on the list.";

                          }

                          return greeting;

                      }

                      

                      /**

                       * Checks whether a name is in the vowelArraysI list.

                       */

                      public static function vowelArrayI(inputName:String = ""):Boolean 

                      {

                          if (vowelArraysI.indexOf(inputName) > -1) 

                          {

                              return true;

                                                      trace(indexOf(inputName));

                          } 

                          else 

                          {

                              return false;

                          }

                      }

                  }

              }

              • 4. Re: setting the focus from stage to text field
                sinious MVP

                You can always get an event sent every time a character is typed and analyze it every keystroke, or just once when enter is pressed. Here's an example that excludes any analysis code but uses the event I mentioned above.

                 

                http://pastebin.com/iJE2Fcrk

                • 5. Re: setting the focus from stage to text field
                  Domhnall Ó Suibhne Community Member

                  The analysis comes later after the text has been entered.  but I will try and use what you have outlined and thanks again I am very grateful to you. I was hoping there was a solution to the text field being ready to receive text with blinking cursor, that didn't require a rewrite of the way I had planned the program. I will give that a try though, and let you know how it went. Thanks again.

                  • 6. Re: setting the focus from stage to text field
                    sinious MVP

                    The person needs to have at least clicked "somewhere" on the application to give it focus in Windows. After that, you definitely can add the cursor blinking at the end if that's what you want.

                     

                    myTextField.setSelection(myTextField.length,myTextField.length);

                     

                    That will put the blinking cursor at the end of the TextField. If you want it in a specific position or to select a specific character you can just change the begin and end index values. Here's more info:

                     

                    http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextField.ht ml#setSelection()

                    • 7. Re: setting the focus from stage to text field
                      Domhnall Ó Suibhne Community Member

                      Excellent, thank you, that worked very well. However it gave rise to another problem in that the text box continues to display in the next frame with the text entered etc. Is there some way to wipe out all that so that  the next frame is a new frame with its own features without any carry over from the last frame?

                      • 8. Re: setting the focus from stage to text field
                        sinious MVP

                        Just set the TextFields text property to an empty string..

                         

                        myTextField.text = "";

                        • 9. Re: setting the focus from stage to text field
                          Domhnall Ó Suibhne Community Member

                          Great that works fine. Ta again!

                          • 10. Re: setting the focus from stage to text field
                            sinious MVP

                            You're welcome, please mark the thread answered if you have what you need.

                             

                            Good luck!