Simple Copy Paste in Adobe AIR

AIR Tutorials, Downloads, Getting Started Add comments

Adobe AIR clipboard class allows you to copy data and objects through clipboard. The operating system clipboard can be accessed by generalClipboard property.

Data to the clipboard can be added by setData() or setDataHandler method and to retrive Data from clipboard use getData()

Example

Clipboard.generalClipboard.setData(ClipboardFormats.TEXT_FORMAT,"some Text value to clipboard");
Clipboard.generalClipboard.getData(ClipboardFormats.TEXT_FORMAT);


The formats supported by Adobe AIR

BITMAP_FORMAT: a BitmapData object
FILE_LIST_FORMAT: an array of File objects
HTML_FORMAT: HTML-formatted string data
TEXT_FORMAT: string data
RICH_TEXT_FORMAT: a ByteArray containing Rich Text Format data
URL_FORMAT: a URL string

ClipboardFormats defines these constants

For Example to get a text from clipboard check if the clipboard contains text before converting it to string by using

if(Clipboard.generalClipboard.hasformat(ClipboardFormats.TEXT_FORMAT)){
var clipboardtextvalue:String = Clipboard.generalClipboard.getData(ClipboardFormats.TEXT_FORMAT);
}

Hasformat returns true or false

To clear clipboard use Clipboard.generalClipboard.clear();

The following simple example demonstrates how to copy a Text to clipboard and also how to retrive the same.

 <?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" title="Adobe AIR Clipboard Example">
<mx:Script>
	<![CDATA[
		import flash.desktop.Clipboard;
   		import flash.desktop.ClipboardFormats;    	
 
    	private function copytoClipboard(text:String):void{
 
            /* Clear any unwanted data in clipboard if exists */
            Clipboard.generalClipboard.clear();
 
            /* Copy the textinput value to the clipboard */
 
            Clipboard.generalClipboard.setData(ClipboardFormats.TEXT_FORMAT, text);
 
            /* clear the text once copied for demo */            
            texttocopy.text=null;
 
            pabtn.enabled=true;
            cpbtn.enabled=false;                   
        }
 
        private function pastefromClipboard():void{            
            /* Show the content in the textinput showclipboardcontent */
            showclipboardcontent.text=String(Clipboard.generalClipboard.getData(ClipboardFormats.TEXT_FORMAT));
            pabtn.enabled=false;
            cpbtn.enabled=true;
        }
 
	]]>
</mx:Script>
 
<mx:Style>
	.pad5{
		paddingBottom:5;
		paddingLeft:5;
		paddingRight:5;
		paddingTop:5;
	}
</mx:Style>
<mx:VBox styleName="pad5">
	<mx:HBox>
		<mx:Label text="Enter Text to be Copied"/>
		<mx:TextInput id="texttocopy"/>
		<mx:Button id="cpbtn" label="Copy to Clipboard" click="copytoClipboard(texttocopy.text)" enabled="true"/>
	</mx:HBox>
	<mx:HBox>
		<mx:Label text="Show Clipboard Content"/>
		<mx:TextInput id="showclipboardcontent"/>
		<mx:Button id="pabtn" label="Paste Clipboard/Ctrl+V" click="pastefromClipboard()" enabled="false"/>		
	</mx:HBox>	
</mx:VBox>
</mx:WindowedApplication>

One Response to “Simple Copy Paste in Adobe AIR”

  1. abhishek Says:

    could we do same code for copy paste of controls like button,labels,etc ?
    :)

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS