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
- copy to OS clipboard = System.setClipboard(“Text to COPY”) of System.setClipboard(textBoxToCopy.text)
- set selection to text that is copied
- give user feedback
Examples and Source
AS2
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
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

























14 Comments
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 …
Great tutorial. Exactly what I was looking for. Thanks.
Great work
Thanks
Thanks, it helps!
hey, this helped me a lot!! thanx so much….rock on!
Thank you!
Thanks! Very well done – exactly what I was looking for. Thanks again, and have an AWESOME week.
joeFREELANCE
Spot on, great solution!
Thanks
Awesome tutorial. And also a big thank’s to Mothmenace who added the import!
Many thanks
is there a solution to copy a jpeg into the clipboard using as3
Thanks a lot! Great Tutorial.
Is there a way to copy the Current URL to the System Clipboard?
There are a couple ways to do it, mostly using ExternalInterface to get the url (which I wrote about here: http://blog.circlecube.com/2008/03/tutorial/get-current-url-and-query-string-parameters-to-flash-tutorial/). and then it’s just as easy as copying the string to the clipboard.
Awesome. Very helpful.