10 Replies Latest reply on Aug 16, 2009 12:44 PM by jiggy1965

    move blinking cursor too

    jiggy1965 Level 1

      I've got a main textfield myTextfield01. When I put focus on another textfield which is empty (either manually by clicking on another field, or like below using setFocus) I want the focus to go back to myTextField01 again. And put the blinking cursor and the very end of whatever is typed into it. So I tried this.

       

      myTextfield01.onKillFocus = function(newFocus:Object) {
          if (newFocus.text == "") {
              Selection.setFocus(myTextfield01);
              Selection.setSelection(myTextfield01.text.length-1, myTextfield01.text.length);
          }
      };

       

      But that didn't work. It went back to myTextfield01, but the blinking cursor stayed at the empty textfield and any text typed would be added to that empty field.

       

      When I put this in frame one:

       

      myTextfield01.onKillFocus = function(newFocus:Object) {
          if (newFocus.text == "") {
              nextFrame());
          }
      };

       

      And this in frame two:

       

              Selection.setFocus(myTextfield01);
              Selection.setSelection(myTextfield01.text.length-1, myTextfield01.text.length);
       

       

      it did work. Both focus and cursor where in myTextfield01 again.

       

      So why doesn't it work when I combine it all in one frame?

        • 1. Re: move blinking cursor too
          kglad Adobe Community Professional & MVP

          try:

           

           

           

          myTextfield01.onKillFocus = function(newFocus:Object) {
              nf=newFocus
          };
          this.onMouseUp=function(){
          if(nf&&nf.text==""){
          Selection.setFocus(myTextfield01);
          varL=myTextfield01.text.length;
          Selection.setSelection(varL,varL);
          nf=null;
          }
          }
          1 person found this helpful
          • 2. Re: move blinking cursor too
            jiggy1965 Level 1

            So why doesn't this work?

             

            myTextfield01.onKillFocus = function(newFocus:Object) {
                nf = newFocus;
                if (nf && nf.text == "") {
                    Selection.setFocus(myTextfield01);
                    varL = myTextfield01.text.length;
                    Selection.setSelection(varL,varL);
                    nf = null;
                }
            };

             

            Why do I have to seperate the part between 'if' and 'null' from the onKillFocus function. And why put that piece of code separate into a MouseUp function?

            • 3. Re: move blinking cursor too
              kglad Adobe Community Professional & MVP

              because the mouseup is the last event that occurs in the sequence that leads to a focus change.  any focus changes you make (in code) before the mouseup will be followed by the mouseup that changes focus again.

              • 4. Re: move blinking cursor too
                jiggy1965 Level 1

                myTextfield01.onKillFocus = function(newFocus:Object) {

                    if (newFocus.text == "") {
                        Selection.setFocus(myTextfield01);

                    }
                };

                 

                I don't understand. After a focus has changed I'm checking for an empty field and if true let it change focus back with setFocus immediately. Which it does cause all the text of myTextield01 is selected now. So myTextfield01 has focus but the cursor doesn't move with it. I don't understand why I have to use another event to place Selection.setFocus(myTextfield01) in when I use Selection.setFocus(myTextfield01); already in the onKillfocus event.

                • 5. Re: move blinking cursor too
                  kglad Adobe Community Professional & MVP

                  use the trace() function with the code i gave so you understand the sequence of events dispatched by flash.

                  • 6. Re: move blinking cursor too
                    jiggy1965 Level 1

                    Tried it with trace:

                    myTextfield01.onKillFocus = function(newFocus:Object) {
                        trace(this._name+" has lost focus. New focus goes to: "+newFocus._name);
                        if (newFocus.text == "") {
                            Selection.setFocus(myTextfield01);
                            trace(newFocus._name+" has lost focus. New focus goes to: "+Selection.getFocus());
                        }
                    };
                    //
                    //A: myTextfield01 has lost focus. New focus goes to: myTextfield02
                    //B: myTextfield02 has lost focus. New focus goes to: _level0.myTextfield01

                     

                    When I go from myTextfield01 to myTextfield02 it traces A.

                    myTextfield02 is empty so it goes immediately back to myTextfield01 and traces B.

                     

                    Which is like expected, but the cursor nevertheless stays in myTextfield02. So why doesn't ik go along with the textfield now in focus? And why does it work when I separate it into a mouseup event?

                    • 7. Re: move blinking cursor too
                      kglad Adobe Community Professional & MVP

                      use the trace() function with the code i gave so you understand the sequence of events dispatched by flash.

                       


                       

                      myTextfield01.onKillFocus = function(newFocus:Object) {
                      trace("kill focus event: where you keep trying to setfocus");
                          nf=newFocus
                      };
                      this.onMouseUp=function(){
                      trace("mouse up event.  focus is changed to: "+Selection.getFocus());
                      if(nf&&nf.text==""){
                      Selection.setFocus(myTextfield01);
                      varL=myTextfield01.text.length;
                      Selection.setSelection(varL,varL);
                      nf=null;
                      }
                      }

                      1 person found this helpful
                      • 8. Re: move blinking cursor too
                        jiggy1965 Level 1

                        I don't understand what you're trying to say.

                         

                        When de onKillFocus event occurs, why then can't I use setFocus during that event?

                         

                        myTextfield01.onKillFocus = function(newFocus:Object) {
                            trace("kill focus event: where I want to use setfocus");
                            Selection.setFocus(myTextfield01);
                            trace("When kill focus event occurs, focus is changed back o: "+Selection.getFocus());
                            nf = newFocus;
                        };

                        • 9. Re: move blinking cursor too
                          kglad Adobe Community Professional & MVP

                          ok.  do it your way.  good luck.

                          • 10. Re: move blinking cursor too
                            jiggy1965 Level 1

                            Can anyone tell me why this doesn't work?

                             

                            myTextfield01.onKillFocus = function(newFocus:Object) {
                                Selection.setFocus(myTextfield01);
                                trace("myTextfield02 = empty, going back to: "+Selection.getFocus());
                                //output:myTextfield02 = empty, going back to: _level0.myTextfield01
                            };

                             

                            When I go to myTextfield02 (which is empty), it goes back to myTextfield01 as the output of the traces proves... except for the blinking input cursor. That stays in myTextfield02 (and any text typed is entered in myTextfield02 too). Both the focus AND the cursor should go back to myTextfield01. Seperating that part in a mouseup event worked, but I fail to understand why putting it all in the onkillfocus event doesn't work. When it's the exact same thing: " when the onkillfocus event occurs, do something"