In this post I’m comparing Flex 4 and Silverlight 4’s clipboard access abilities. My previous post was rather long, so I’ll try to keep this one short :). Accessing the clipboard should be an interesting comparison, since both Flash 10 and Silverlight 4 take different approaches. I deliberately said “Flash 10” in the previous sentence, because accessing the clipboard in a Flex application is done by using the native Flash 10 ActionScript api and there isn’t any specific Flex code required.
Accessing the Clipboard in Flash 10
Accessing the clipboard in Flash has changed going from Flash player 9 to Flash Player 10. in Flash 9, one could set text on the user’s clipboard with the System.setClipboard() method from anywhere in the application. With Flash 10, this can only be done from a user initiated action. Also, while you can still use the System.setClipboard() method, you have more possibilities when using the new Clipboard class. Take a look at the user interface of the following Flex application:
The idea is simple: a user can enter text in the first textbox, press copy, then press paste to see the copied text in the second textbox. Sounds simple right? Yet this simple scenario could not be implemented with Flash. Let’s take look at the code:
The handleCopy() method on line 12 is the method that get’s called when a user clicks on the copy button.It copies the text in the first textfield using the static “generalClipboard” property of the Clipboard class. The setData() method accepts any object and you must specify the format for the object using the ClipboardFormats class. Quite a few formats are supported, but most of them are for AIR, Flash can only use TEXT_FORMAT,RICH_TEXT_FORMAT and HTML_FORMAT, of which the TEXT_FORMAT format is the most useful. The others are primarily for copy / pasting between Flash applications and they all boil down to text anyway.
The handlePaste() method on line 17 get’s called when a user clicks the paste button. Surprisingly enough, this method throws a SecurityError while I’m certainly in a user initiated action. It turns out that you can only read the clipboard content in a “paste” event, which is only fired when a user presses CTRL + V or clicks on the “paste” item in the the context menu that appears when a user right clicks a component in the Flash application.
Just to demonstrate how the paste event works, I’ve implemented it on the application level. Take a look on line 24 at the setContextMenuPasteOnly() method. This method is called in response to the “creationComplete” event of the application. It disables most of the items of the default Flash context menu and enables the so called “clipboardMenu”. Of the “clipBoardMenu” items, only the “paste” item is active. When the user right clicks the application (not the textbox) the following menu is shown:
When a user clicks “Paste” or presses CTRL + V, the application fires the “paste” event, when this happens the handleApplicationPaste() method on line 35 is called and the copied text is put in the second textbox. Note that most of the built in text components already have support for the “paste” item in the context menu and also support CTRL + V, all I have accomplished is that the user can press CTRL+V or right click anywhere in the application to paste the copied text. Pasting when the user clicks the button isn’t going to happen in Flash 10.
Accessing the Clipboard in Silverlight 4
Using the clipboard in Silverlight 4 is actually a lot easier than in Flash 10. Take a look at the following user interface:
Same idea as the Flex app, but in Silverlight I can implement this application as intended. Clipboard access in Silverlight is also done using the Clipboard class. This class has three important methods, which are actually self-explanatory:
This means that you can only set and read text from the clipboard in Silverlight, no other formats. The interesting part in Silverlight is, that you can actually access the clipboard from any user initiated event. When you access the clipboard the first time in a Silverlight application, the application asks for the user’s permission:
If not granted, the accessing of the clipboard throws a SecurityException. If granted, Silverlight won’t ask again until the next run of the application. Take a look at the code behind:
As I said, accessing the clipboard in Silverlight is very simple.
Well, Silverlight is the winner for me. I do have to code more if I want to create a nice right mouse button menu like in Flash, or add CTRL + V support to the entire application, but the added flexibility that Silverlight offers by allowing me to use any user initiated event for accessing the clipboard, simply outweighs that. You can find the Flex 4 example here and the Silverlight 4 example here.