Insert string into textfield at cursor position

Hi,

What I’m trying to do is: Inserting a text (string) into a textfield by pasting.

  • User puts the cursor at a position
  • User clicks the 'Paste" button
  • A string then has to be inserted at the cursor position, keeping all the text that is already there.

Code below should do the magic, but how to paste at the cursor position ?
Any hints would be welcome !

[code]btnPaste.addEventListener(MouseEvent.CLICK, fncInsert);

function fncInsert(e:Event):void{
Clipboard.generalClipboard.clear();
System.setClipboard(“String to insert”);

// PASTE STRING AT CURSOR POSITION IN TEXTFIELD
// WHILE KEEPING ALL EXISTING TEXT IN TEXTFIELD

}[/code]

Thanks in advance !

If this is As3 and a TLFTextfield, you could probably use caret?
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fl/text/TLFTextField.html#caretIndex

Say, is this for school and you go to a forum to get the code?

No it’s not a school assignement :slight_smile:
It’s a personal project and I was hoping someone could give me a hint on how to do this the quick way. If I start without a starting point I’ll probably end up with way too complicated code

And i’ts a classic input textfield

I ended up with this:

[code]
function fncInsertStandardData(e:Event):void{
var strComplete:String = txtOverview.text;
var strA:String = strComplete.substr(0, txtOverview.selectionBeginIndex);
var strB:String = strComplete.substr(txtOverview.selectionEndIndex, strComplete.length);

var strInsert:String = "";
strInsert = e.target.selectedItem.label + "\n" + e.target.selectedItem.data + "\n";

txtOverview.text = strA + strInsert + strB;
cboTypeInsert.selectedIndex = 0;

}[/code]

+1 for your clean presentation. There is a built in replaceText function, but it won’t help you much.

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/engine/TextElement.html#replaceText()