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

    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

    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

    This entry was posted in tutorial and tagged , , , , , , , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

    22 Comments

    1. Mothmenace
      Posted November 17, 2008 at 12:44 pm | Permalink

      Nice tutorial – many thanks.

      You should include the “import flash.system.*” though.

    2. Posted December 3, 2008 at 2:00 pm | Permalink

      Most helpful – thanks.

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

      Thanks anyways …

    3. knuckfubuck
      Posted December 4, 2008 at 7:48 pm | Permalink

      Great tutorial. Exactly what I was looking for. Thanks.

    4. Posted December 12, 2008 at 4:33 am | Permalink

      Great work

      Thanks

    5. Posted January 29, 2009 at 5:01 am | Permalink

      Thanks, it helps!

    6. jo
      Posted February 11, 2009 at 9:10 am | Permalink

      hey, this helped me a lot!! thanx so much….rock on!

    7. fakir
      Posted March 17, 2009 at 9:18 am | Permalink

      Thank you!

    8. Joe
      Posted April 15, 2009 at 7:49 am | Permalink

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

      joeFREELANCE

    9. ddee
      Posted April 23, 2009 at 9:16 am | Permalink

      Spot on, great solution!
      Thanks

    10. Joey
      Posted August 9, 2009 at 5:03 pm | Permalink

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

      Many thanks

    11. Posted October 29, 2009 at 9:41 am | Permalink

      is there a solution to copy a jpeg into the clipboard using as3

    12. Srikanth
      Posted November 22, 2009 at 4:18 pm | Permalink

      Thanks a lot! Great Tutorial.

      Is there a way to copy the Current URL to the System Clipboard?

    13. Jamus
      Posted February 1, 2010 at 12:14 pm | Permalink

      Awesome. Very helpful.

    14. mandia
      Posted May 9, 2010 at 1:01 pm | Permalink

      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

    15. Posted August 30, 2011 at 2:06 pm | Permalink

      This is very helpful script thanks!

    16. Alex
      Posted December 28, 2011 at 10:02 pm | Permalink

      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.

      • Posted January 4, 2012 at 10:37 am | Permalink

        @Alex – it sounds like you could construct a string variable from your different textareas and then copy that to the clipboard.

    17. gojakv
      Posted January 4, 2012 at 9:45 am | Permalink

      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!

      • Posted January 4, 2012 at 10:39 am | Permalink

        @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.

    18. Karl
      Posted January 24, 2012 at 10:59 am | Permalink

      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

    19. Alex
      Posted March 3, 2012 at 11:54 am | Permalink

      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.

    Post a Comment

    Your email is never published nor shared. Required fields are marked *

    *
    *

    You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

    • Recent Posts

      WordPress updates plugin directory

      New additions to the plugin directory include: favorites, incorporating support forums into it's own tab for each plugin as well as support stats being displayed! Great! I think we also need the ability to give plugins ratings and reviews (bonus points if it can be done from within a wordpress admin dashboard when installing plugins). [...]

      Short Head

      Use zipf's short head to tune your website rather than redesign the whole thing. To make a website successful it needs to meet the needs of the users. Find out what those needs are by using the short head philosophy to equate most searched things as the biggest needs of the users. Use personas to [...]

      Img Set?

      Great article at a list apart discusing the state of the industry regarding responsive images. This picks apart the set attribute of the img element from a surprisingly objective view coming from someone so close to the picture element. Insightful discussion about the principle behind the proposals than the actual solution too. If the working [...]

      Triudo

      A mesmerizing animated triangle-ish shape form. Embedded Link triduo triduo Tweet

      Git – the paradigm shift

      A great developer story about the differences on what Git is vs other version control and what Git is not. This is how we should learn it. I heard over and over that it was distributed, but never grasped what that meant, so here are a few links and explanations that will help unlearn version [...]

      Tweening Lib comes to Javascript!

      I'm very excited to share the news that the tween library from GreenSock (hands down the best tweening library I used in flash) is not ported for use in javascript! This will be great! I missed that simple syntax from as3 when animating javascript, and now I can have my cake and eat it too. [...]

      Responsive CSS Tricks

      Here are a few useful css tricks to remember when building responsive design sites from web designer wall Embedded Link 5 Useful CSS Tricks for Responsive Design Making the design to be responsive is very easy as shown in my Responsive Design in 3 Steps tutorial, but maintaining the elements to look aesthetically balanced on [...]

      Picture element of srcset attribute?

      Bruce details the reasons and story behind the srcset attribute which is now introduced as an alternative to the picture element. Some aspects of the attribute are nice (like the fact that it's an attribute and not a new element, so it's creating up new elements with for problems. It's adapting currently used elements to [...]

      SVG Preloader with Raphael JS

      Here's a very creative use of using a newly available technology. Using svg graphics which are very lightweight, for a website preloader. I like the animation used as well. Embedded Link Make a stylish preloader with SVG | Tutorial | .net magazine Many sites neglect users with slow connections. Ian Culshaw explains how to use [...]

      CSS3 Button/Icon set

      I've been secretly hoping to see a few of these pop up once the whole icon font idea spread through the nets. I really like this idea and it's a very nice implementation too! I only see some quality issues on a couple of the icons (such as youtube), but it's awesome and I hope [...]

    • Recent Comments

      Bruce Brownlee

      Bruce Brownlee

      Ah IE6. I'd have 2 more years of sleep without IE6. Margin doubling, no properties,...
      versaena

      versaena

      how to give color at runtime…… thank you
      Mobile Websites

      Mobile Websites

      I disagree, mobile websites are the future – desktop websites and mobile websites...
      Matt Fasick

      Matt Fasick

      That's cool. I like the ripple effect as well.
      Nico

      Nico

      hi! really great job guy! very impressive.. just a question… do u have a solution to do a refresh...
      Evan Mullins

      Evan Mullins

      Agreed! I've just seen some people get pretty heated about separating all functionality...