laarsk
laarsk

Reputation: 872

Actionscript 3 - get position of Cursor in TextArea?

I am making something in Actionscript 3, where people can modify a piece of text, within a TextArea. Now, it's easy to get the typed character, but using event.getChar.

But, I would also like to know where the character was typed: the (text)cursor position. I've read about that it's easy to do with a TextField, however, I want to use a TextArea for a few reasons:

I've read about it being possible with a TextField, but I'm not sure how I would make that into an input field...

Also, the TextArea is recommended for multiline text.

If I could hack a TextField to behave like a TextArea, I'm fine with that.

So, my question: How can I get the position of the cursor in a TextArea?

or

How can I make a TextField behave like a TextArea?

EDIT: I managed to make an input TextField, but the caretIndex returns xyz coordinates, pretty useless for text editing/comparing... Any suggestions on that?

Upvotes: 1

Views: 2793

Answers (1)

mihai
mihai

Reputation: 4214

you can see which letter was clicked in a textfield by doing the following:

var tf:TextField;
var clicked_on_index:int = tf.getCharIndexAtPoint(tf.mouseX, tf.mouseY);//find index of char clicked on in string
var clicked_on_char:String = tf.text.substr( clicked_on_index, 1 );//find char clicked on from textfield

or if you just want to know the position of the last character entered:

var tf:TextField;
tf.addEventListener(Event.CHANGE,function(event:Event):void{
    var newCharacterPosition:int=tf.caretIndex;
    var totalCharacters:int=tf.text.length;
});

Upvotes: 3

Related Questions