Posts Tagged ‘flex clipboard example

14
Feb
10

Clipboard Copy And Paste in Flex web Application

Sometime we may  need to access clipboard data for our Applications. In flex we can easily save our data to System clipboard by using System.setClipboard() method. For security reasons getting clipboard data from flex code is not allowed.

An alternate way to get the clipboard data to flex Application is through java script by ExternalInterface.call(). Once control was transferred to javaScript we can access the clipboard data and return it to flex Application. below is the attached code

CopyAndPaste.mxml

<?xml version=”1.0″ encoding=”utf-8″?>
<mx:Application xmlns:mx=”http://www.adobe.com/2006/mxml ” layout=”absolute”>
<mx:TextArea id=”copyData” x=”70″ y=”96″ width=”272″ height=”65″/>
<mx:TextArea id=”pasteData” x=”70″ y=”195″ width=”272″ height=”65″/>
<mx:Button x=”417″ y=”118″ label=”Copy To Clipboard” width=”126″  click=”CopyToClipboard()”/>
<mx:Button x=”417″ y=”213″ label=”Paste From Clipboard” width=”126″ click=”PasteFromClipboard()”/>
<mx:Script>
<![CDATA[
import mx.controls.Alert;
public function CopyToClipboard():void{
System.setClipboard(copyData.text);
}

public function PasteFromClipboard():void{
var str2:String=ExternalInterface.call("PasteFromClipboard");
pasteData.text=str2;
}
]]>
</mx:Script>
</mx:Application>

In index.template.html (flex generated html wrapper ) place the following code inside <script> . . . </script>

function PasteFromClipboard()
{
var str=”"
var browser=navigator.appName;
if(browser==”Microsoft Internet Explorer”)
{
return (window.clipboardData.getData(‘Text’));
}
else if (window.netscape)
{
netscape.security.PrivilegeManager.enablePrivilege( ‘UniversalXPConnect’ );
this.clipboardid = Components.interfaces.nsIClipboard;
this.clipboard = Components.classes['@mozilla.org/widget/clipboard;1'].getService( this.clipboardid );
this.clipboardstring = Components.classes['@mozilla.org/supports-string;1'].createInstance( Components.interfaces.nsISupportsString );
netscape.security.PrivilegeManager.enablePrivilege( ‘UniversalXPConnect’ );
var transfer = Components.classes['@mozilla.org/widget/transferable;1'].createInstance( Components.interfaces.nsITransferable );
transfer.addDataFlavor( ‘text/unicode’ );
this.clipboard.getData( transfer, this.clipboardid.kGlobalClipboard );
var str = new Object();
var strLength = new Object();
transfer.getTransferData( ‘text/unicode’, str, strLength );
str = str.value.QueryInterface( Components.interfaces.nsISupportsString );
return str+”";
}
}

Note : The above program will work only in Internet explorer and morzilla firefox

copy to clipboard flex



Follow

Get every new post delivered to your Inbox.