1. Code
  2. ActionScript

Quick Tip: Create a Copy to Clipboard Button 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.

Final SWF

The example below is what we're going to be aiming for during this Quick Tip.

Press Cmd + V (or Ctrl + v on a PC) to paste the text elsewhere.

Step 1: Brief Overview

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.

Step 2: Create a New File

Open Flash and create a new Flash File (ActionScript 3.0).

Set the stage size to 320 x 240px.

Step 3: Create the Text Fields

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:

In this tip we'll focus on the System.setClipboard() method.

Select the Text Tool (T) and create two textfields, a dynamic and a input field.

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.

Step 4: Set the Instance Names

Open the Properties Panel and set the fields' instance names as shown in the image.

Step 5: Create a Button

A button will call the copy to clipboard action.

Select the Rectangle Tool (R), create a 60x28 px rectangle and fill it with #333333. Add a static text field as a label.

Convert the shapes to MovieClip (select them, then click Modify > Convert to Symbol) and name the button copyButton.

Step 6: Open the Actions Panel

Press Option + F9 or go to Window > Actions to open the Actions Panel.

Step 7: Write the Copy Function

This is the function that will copy the text to the clipboard.

Step 8: Add a Mouse Listener

The copy function is called by a MouseEvent.

An Event Listener will be added to the button, this will trigger the copyText function when the user clicks the button.

Step 9: Test

Save your work and press Cmd + Return to run the application.

Press Cmd + V (or Ctrl + v on a PC) to paste the text elsewhere.


This is a basic example of how you can use this function; experiment and use it in your own projects!

Thanks for reading!

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.