Copy TextField text to System clipboard | Actionscript (AS2 + AS3) Tutorial

Overview

Integrating the clipboard of the operating system with your flash projects is sometimes essential. It’s a very simple and boils down to one basic method… System.setClipboard(). I’ve found a couple other things help the user experience though, such as selecting the text that gets copied and giving the user some sort of feedback to let them know that the text was successfully copied. Here’s a simple way to do it. Have any suggestions to make it better?

I’ve included an as2 version as well as as3. I’ve promised myself to migrate to as3, so I’m not coding anything in 2 that I don’t do in 3 also. This was to discourage me from coding in as2 and to encourage me to code as3, but also let me learn by doing it in both to see the actual differences if I was stuck doing a project in as2. I figured this could help others see the differences between the two versions of actionscript a bit easier and make their own migration as well!

Steps

  1. copy to OS clipboard = System.setClipboard(“Text to COPY”) of System.setClipboard(textBoxToCopy.text)
  2. set selection to text that is copied
  3. give user feedback

Examples and Source

AS2

[kml_flashembed fversion=”9.0.0″ movie=”https://circlecube.com/circlecube/wp-content/uploads/sites/10/2008/11/clipboard_as2.swf” targetclass=”flashmovie” publishmethod=”dynamic” width=”550″ height=”400″]

Get Adobe Flash player

[/kml_flashembed]

[cc lang=”actionscript” tab_size=”2″ lines=”40″]
textBox.textBox.text = “Click this text box to copy the text or click the COPY button below. You will see feedback to the user and this text copied to your clipboard!\n\n”+
‘copyButton.onRelease = textBox.onPress = function(){\n\tSelection.setFocus(“textBox”);\n\tSelection.setSelection(0, textBox.text.length);\n\tSystem.setClipboard(textBox.text);\n\ttrace(“copied: “+textBox.text);\n\tfeedback(“Text Copied!”);\n}’;

copyButton.onRelease = textBox.onPress = function(){
Selection.setFocus(“textBox.textBox”);
Selection.setSelection(0, textBox.textBox.text.length);
System.setClipboard(textBox.textBox.text);
trace(“copied: “+textBox.textBox.text);
textFeedback(“Text Copied!”);
}

function textFeedback(theFeedback:String){
feedback.text = theFeedback;
setTimeout(function(){feedback.text=””;}, 1200);
}
[/cc]

AS3

[kml_flashembed fversion=”9.0.0″ movie=”https://circlecube.com/circlecube/wp-content/uploads/sites/10/2008/11/clipboard_as3.swf” targetclass=”flashmovie” publishmethod=”dynamic” width=”550″ height=”400″]

Get Adobe Flash player

[/kml_flashembed]

[cc lang=”actionscript” tab_size=”2″ lines=”40″]
textBox.text = “Click this text box to copy the text or click the COPY button below. You will see feedback to the user and this text copied to your clipboard!\n\n”+
‘function copyText(e:MouseEvent):void{\n\ttextBox.setSelection(0, textBox.text.length)\n\tSystem.setClipboard(textBox.text);\n\ttrace(“copied: “+textBox.text);\n\ttextFeedback(“Text Copied!”);\n}’;

//set it so the textBox selection will show even when textBox has no focus
textBox.alwaysShowSelection = true;

textBox.addEventListener(MouseEvent.CLICK, copyText);
copyButton.addEventListener(MouseEvent.CLICK, copyText);

function copyText(e:MouseEvent):void{
textBox.setSelection(0, textBox.text.length)
System.setClipboard(textBox.text);
trace(“copied: “+textBox.text);
textFeedback(“Text Copied!”);
}

function textFeedback(theFeedback:String):void {
feedback.text = theFeedback;
setTimeout(function(){feedback.text=””;}, 1200);
}
[/cc]

Download

Source files: clipboard_as3.fla clipboard_as2+as3.zip

22 thoughts on “Copy TextField text to System clipboard | Actionscript (AS2 + AS3) Tutorial

  1. Thanks! Very well done – exactly what I was looking for. Thanks again, and have an AWESOME week.

    joeFREELANCE

  2. Awesome tutorial. And also a big thank’s to Mothmenace who added the import!

    Many thanks

  3. hey 😉
    Good tutorial unfortunately it’s not exactly what i am looking for.
    Does anybody know how to copy only a selected text not a whole textfield.
    I hope somebody will help me

  4. Nice tutorial Evan! I have a question though. I am currently on a project to create a ticket logger for our company. This ticket logger would use the system.clipboard as well. However, the problem that I am having is that I cannot make it to work with multiple textareas. I know that the clipboard only holds one data at a time. Thus, the same exact code will not work with multiple textareas. I was informed that I need to gather all those data as one big string but I don’t know exactly how to do it. BTW, I am using Actionscript 3.0 via Flash CS5.

  5. Any idea how to use this code to copy desired node which text box reads from from xml file? For example email node:

    Location
    Name
    email address
    Contacts/img/empty.jpg

    Thanks!

    1. @gojakv – I’m not quite sure what you mean. It is possible to get xml data into a text field and then copy to clipboard if that’s what you’re after.

  6. I have been using for some time this nice Banner, from developer FX. They have a really nice Live control panel, that allows you to change the Flash banner on the fly. Thing I was surprised is that the control panel is also in Flash and once you finish changing the attributes you can generate an XML list of attributes with a copy/paste button. I was wondering how they got it to work, and thanks to this tutorial I now know. If you are interested in a nice application of this type of script you can check http://www.flashxml.net/banner-rotator.html

  7. Hi, I just have a problem with the copying and pasting the information to a different text editor. It does not recognize new lines. For example, if i copy

    function copyText(e:MouseEvent):void{
    textBox.setSelection(0, textBox.text.length)
    System.setClipboard(textBox.text);
    trace(“copied: “+textBox.text);
    textFeedback(“Text Copied!”);

    if I paste it to Notepad it shows as a single line.

Comments are closed.