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

Get Adobe Flash player

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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);
}

AS3

Get Adobe Flash player

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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);
}

Download

Source files: clipboard_as3.fla clipboard_as2+as3.zip

  • del.icio.us
  • Digg
  • E-mail this story to a friend!
  • Facebook
  • FriendFeed
  • Google Bookmarks
  • LinkedIn
  • Mixx
  • Print this article!
  • Turn this article into a PDF!
  • StumbleUpon
  • Technorati
  • Twitter
  • RSS

Related posts

Visit Circlecube @ FlashDen
Stomping the Search Engines 2, from StomperNet

  • Mothmenace
    Nice tutorial - many thanks.

    You should include the "import flash.system.*" though.
  • Most helpful - thanks.

    I don't get the highlighted text effect after copying however - could it me a Mac thing?

    Thanks anyways ...
  • knuckfubuck
    Great tutorial. Exactly what I was looking for. Thanks.
  • Great work

    Thanks
  • Thanks, it helps!
  • jo
    hey, this helped me a lot!! thanx so much....rock on!
  • fakir
    Thank you!
  • Joe
    Thanks! Very well done - exactly what I was looking for. Thanks again, and have an AWESOME week.

    joeFREELANCE
  • ddee
    Spot on, great solution!
    Thanks
blog comments powered by Disqus