|Articles & Tutorials|
|AS3 - Using the system clipboard|
|Friday, 13 January 2012 21:02|
Flash lets you use the System clipboard to copy and paste data. These features are provided in the Clipboard class.
Import related classes first:
The Clipboard class holds a reference to the system clipboard which we can use to access it:
Just note that you cannot create new Clipboard instances. If you try to do so an error will be thrown.
The clipboard can hold multiple data of different types at the same time. Standard types are defined in the ClipboardFormats class as consts:
You can also define custom types to hold Actionscript objects, but references will be valid only within the application itself.
To set data into the clipboard we'll use the setData method. You need to specify the data format before. For example, let's save a string:
The string will be saved in the system clipboard and you'll be able to paste it even outside your application.
To save custom data we will use a custom data format. It's usually good practice to specify the qualified class name of the object to avoid name conflicts. Custom data format name cannot begins with "air:" or "flash:".
For example, let's say we have a PlayerInfo object we want to copy and paste to another instance of our game. We'll copy it using this:
A third optional parameter, serializable, specifies if you want to save both a copy and reference of your object (true) or just a reference (false). It has to be set to true if you want your data to be available between different Flash applications. Serializable is true by default
If you try to save data using a data format already used, old data will be overwritten.
You can check what formats are used in the clipboard object reading the formats array property:
Or you can use the hasFormat method to check if the clipboard contains data of the specified format:
Copying data using a data handler
We can define a function as a data handler to generate data everytime we request it. To do that we use the setDataHandler method. Let's say we have this function to generate a representation of the user info to show in the game lobby:
We can set it as a handler for the clipboard:
Everytime we'll request the related data, this function will be called.
Just note that, if you set both data and data handler of the same type, data handler will never be called.
You can then retrieve data using the getData method. Under Flash Player you can only use this method inside an Event.PASTE event handler.
Data of standard types are returned as a new object in their specified type:
When requesting custom format data we can specify if we want a reference or a clone of the object using the optional second parameter of the getData method. Transfer modes are defined in the ClipboardTransferMode class as static literal consts:
By default transfer mode is set to ORIGINAL_PREFERRED.
We can then use it to retrieve custom data:
If we use CLONE_ONLY or ORIGINAL_ONLY and the requested object is not available it will return null.
Clearing the clipboard
To clear all data inside the clipboard use the clear method:
Instead, if you want to delete just one format of data you can use clearData, specifying the format: