jump to navigation

Create a Copy to Clipboard Button in Flash September 10, 2010

Posted by Tournas Dimitrios in Flash.

The clipboard is a software facility that can be used for short-term data storage and/or data transfer between documents or applications, via copy and paste operations.In this Quick Tip, we will create a Copy to Clipboard button in Flash.

Two TextFields will be placed on stage, an input field and a dynamic field. A button will be used to get and copy the input field text to the clipboard showing a message in the dynamic field.

Now, in this tip we’ll handle the clipboard in the Flash Player Browser plug-in, this means we won’t have access to the flash.desktop.Clipboard class that is only available in AIR, reducing our possible actions.

Every InteractiveObject (that is, every object with which the user can interact) dispatches copy and paste events. If the object that currently has focus is a TextField, the default behavior of this event is to cause any currently selected text in the text field to be copied to the clipboard. You can listen to these events using:

 myObject.addEventListener(Event.COPY, onCopy);
 myObject.addEventListener(Event.CUT, onCut);
 myObject.addEventListener(Event.PASTE, onPaste); 

In this tip we’ll focus on the System.setClipboard() method.Select the Text Tool (T) and create two textfields, a dynamic field , a input field and a button .Set the instance names as folows

  • Input Field : myInputField_txt
  • Dynamic Field : myDynamicField_txt
  • Button : copyButton_btn

You can enter some text in the input field to use it as default. Enter something that needs to be copied exactly as it’s written, like an email address vs.tournasdimitrio@gmail.com

copyButton_btn.addEventListener(MouseEvent.CLICK , copyText);
 function copyText(e:MouseEvent):void
   System.setClipboard(myInputField_txt.text); //The main line
   myDynamicField_txt.text = "Copied!"; //A message will be shown in the messageField as feedback

Save your work and and enjoy ……..


No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s