4 Replies Latest reply on Mar 23, 2007 7:30 AM by Greg Dove

    TextArea cursor postion


      In the Textarea Component how do I find where the cursor is. So for instance if the TextArea.text = x1x2x4x5 and I want to click a button to insert x3 where the cursor currently is. So I click in the text area between 2 and x and click my button. Impossible to do unless I know where the cursor currently is in the text area. I want to be able to click a button and where ever the cursor is in the text area my additional text will be inserted there.

      Thanks for any help!
        • 1. Re: TextArea cursor postion
          Greg Dove Level 4
          Clicking the button makes the TextArea lose the focus, which means you can't access the Selection.getCaretIndex();

          If someone else has an easier solution I'd love to hear about it... but here's my approach:

          In the past when I've dealt with this, its been by listening for the focusIn event for the TextArea... then using setInterval to call a function periodically (maybe every 50 ms) to record the Selection.getCaretIndex() and as long as it is != -1 (-1 means its not there) storing it in a variable. You can then use clearInterval to cancel the inteval when the focusOut event occurs on the TextArea. Your button would use the stored version of the caret index as the insertion point for what you want to add to the text... and for a nice touch you can set the focus back to the text area and then Selection.setSelection for the range of the text you just "pasted" if you want. You may have to manually "fire" the focusIn event to start the sequence for the setInteval again... I can't remember if it fires when done by code.
          • 2. Re: TextArea cursor postion
            RandalJ99 Level 1
            Fantastic! I'll give that a try. Thanks for the help!
            • 3. Re: TextArea cursor postion
              kglad Adobe Community Professional & MVP
              you don't need to use a loop. just use the onMouseUp method to record the Selection.getBeginIndex() and Selection.getEndIndex() (if it's greater than -1).

              if you have more than one textfield, you can use the onSetFocus() method of textfields to pass the textfield that's being selected to your onMouseUp handler.
              • 4. Re: TextArea cursor postion
                Greg Dove Level 4
                kglad... good option.. and far more elegant...would you use the Key object to track selection changes made using the keyboard as well? arrow keys etc