• Blog
  • Info Support
  • Career
  • Training
  • International Group
  • Info Support
  • Blog
  • Career
  • Training
  • International Group
  • Search
logo InfoSupport
  • Latest blogs
  • Popular blogs
  • Experts
      • Alles
      • Bloggers
      • Speakers
  • Meet us
  • About us
    • nl
    • en
    • .NET
    • Advanced Analytics
    • Agile
    • Akka
    • Alexa
    • Algorithms
    • Api's
    • Architectuur
    • Artificial Intelligence
    • ATDD
    • Augmented Reality
    • AWS
    • Azure
    • Big Data
    • Blockchain
    • Business Intelligence
    • Cloud
    • Code Combat
    • Cognitive Services
    • Communicatie
    • Containers
    • Continuous Delivery
    • CQRS
    • Cyber Security
    • Dapr
    • Data
    • Data & Analystics
    • Data Science
    • Data Warehousing
    • Databricks
    • DevOps
    • Digital Days
    • Docker
    • eHealth
    • Enterprise Architecture
    • Hacking
    • Infrastructure & Hosting
    • Innovatie
    • Integration
    • Internet of Things
    • Java
    • Machine Learning
    • Microservices
    • Microsoft
    • Microsoft Bot Framework
    • Microsoft Data Platform
    • Mobile Development
    • Mutation Testing
    • Open source
    • Pepper
    • Power BI
    • Privacy & Ethiek
    • Python
    • Quality Assistance & Test
    • Quality Assurance & Test
    • Requirements Management
    • Scala
    • Scratch
    • Security
    • SharePoint
    • Software Architecture
    • Software development
    • Software Factory
    • SQL Server
    • SSL
    • Start-up
    • Startup thinking
    • Stryker
    • Test Quality
    • Testing
    • TLS
    • TypeScript
    • Various
    • Web Development
    • Web-scale IT
    • Xamarin
    • Alles
    • Bloggers
    • Speakers
Home » Silverlight 4 vs Flex 4: Accessing the Clipboard
  • Silverlight 4 vs Flex 4: Accessing the Clipboard

    • By Alex van Beek
    • Java 12 years ago
    • Java 0 comments
    • Java Java
    Silverlight 4 vs Flex 4: Accessing the Clipboard

    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: 

    image

     

    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: 

       1: <?xml version="1.0" encoding="utf-8"?>

       2: <s:Application >"http://ns.adobe.com/mxml/2009" 

       3:                >"library://ns.adobe.com/flex/spark" 

       4:                >"library://ns.adobe.com/flex/halo" minWidth="1024" minHeight="768" creationComplete="setContextMenuPasteOnly(event)"

       5:                 paste="handleApplicationPaste(event)">

       6:&nbsp; 

       7:     <fx:Script>

       8:         <![CDATA[

       9:             import mx.events.FlexEvent;

      10:             import flash.desktop.ClipboardFormats;

      11:             import flash.desktop.Clipboard;

      12:             protected function handleCopy(event:MouseEvent):void

      13:             {

      14:                 Clipboard.generalClipboard.setData(ClipboardFormats.TEXT_FORMAT, _txtCopy.text);

      15:             }

      16:&nbsp; 

      17:             protected function handlePaste(event:MouseEvent):void

      18:             {

      19:                 //throws error!

      20:                 _txtPaste.text = String(Clipboard.generalClipboard.getData(ClipboardFormats.TEXT_FORMAT));

      21:             }

      22:&nbsp; 

      23:&nbsp; 

      24:             protected function setContextMenuPasteOnly(event:FlexEvent):void

      25:             {

      26:                 var menu : ContextMenu = new ContextMenu();

      27:                 menu.hideBuiltInItems();

      28:                 menu.clipboardItems.paste = true;

      29:                 menu.clipboardItems.selectAll = false;

      30:                 menu.clipboardMenu = true;

      31:                 contextMenu = menu;

      32:             }

      33:&nbsp; 

      34:&nbsp; 

      35:             protected function handleApplicationPaste(event:Event):void

      36:             {

      37:                 _txtPaste.text = String(Clipboard.generalClipboard.getData(ClipboardFormats.TEXT_FORMAT));

      38:             }

      39:&nbsp; 

      40:         ]]>

      41:     </fx:Script>

      42:     

      43:&nbsp; 

      44:     <s:TextInput x="127" y="120" id="_txtCopy" />

      45:     <s:TextInput x="127" y="150" id="_txtPaste"/>

      46:     <s:Button x="263" y="120" label="Copy" click="handleCopy(event)"/>

      47:     <s:Button x="263" y="151" label="Paste" click="handlePaste(event)"/>

      48:     

      49:     

      50: </s:Application>

    &nbsp;

    The handleCopy() method on line 12 is&nbsp; the method that get&rsquo;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&rsquo;s called when a user clicks the paste button. Surprisingly enough, this method throws a SecurityError while I&rsquo;m certainly in a user initiated action. It turns out that you can only read the clipboard content in a “paste&rdquo; event, which is only fired when a user presses CTRL + V or clicks on the “paste&rdquo; 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&rdquo;. Of the “clipBoardMenu” items, only the “paste&rdquo; item is active. When the user right clicks the application (not the textbox) the following menu is shown:

    &nbsp;

    image

    When a user clicks “Paste&rdquo; or presses CTRL + V, the application fires the “paste&rdquo; 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&rdquo; 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&rsquo;t going to happen in Flash 10.

    &nbsp;

    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:

    image

    &nbsp;

    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:

    • SetText()
    • GetText()
    • ContainsText()

    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&rsquo;s permission:

    image

    If not granted, the accessing of the clipboard throws a SecurityException. If granted, Silverlight won&rsquo;t ask again until the next run of the application. Take a look at the code behind:&nbsp;&nbsp;

       1: using System;

       2: using System.Collections.Generic;

       3: using System.Linq;

       4: using System.Net;

       5: using System.Windows;

       6: using System.Windows.Controls;

       7: using System.Windows.Documents;

       8: using System.Windows.Input;

       9: using System.Windows.Media;

      10: using System.Windows.Media.Animation;

      11: using System.Windows.Shapes;

      12: using System.Security;

      13:&nbsp; 

      14: namespace CopyPaste

      15: {

      16:     public partial class MainPage : UserControl

      17:     {

      18:         public MainPage()

      19:         {

      20:             InitializeComponent();

      21:         }

      22:&nbsp; 

      23:         private void HandleCopy(object sender, RoutedEventArgs e)

      24:         {

      25:             try

      26:             {

      27:                 Clipboard.SetText(_txtCopy.Text);

      28:             }

      29:             catch (SecurityException)

      30:             {

      31:                 MessageBox.Show("You should allow clipboard access :)");

      32:             }

      33:         }

      34:&nbsp; 

      35:         private void HandlePaste(object sender, RoutedEventArgs e)

      36:         {

      37:             

      38:                 if (Clipboard.ContainsText())

      39:                 {

      40:                     try

      41:                     {

      42:                         _txtPaste.Text = Clipboard.GetText();

      43:                     }

      44:                     catch (SecurityException)

      45:                     {

      46:                         MessageBox.Show("You should allow clipboard access :)");

      47:                     }

      48:                 }

      49:         }

      50:     }

      51: }

    &nbsp;

    As I said, accessing the clipboard in Silverlight is very simple.&nbsp;

    Conclusion

    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.

    Share this

Alex van Beek

View profile

Related IT training

Go to training website

Related Consultancy solutions

Go to infosupport.com

Related blogs

  • Do you know Java? Care to prove it?

    Do you know Java? Care to prove it? Hanno Embregts - 3 months ago

  • Deploy je JDK 11 applicatie op je eigen Java 17 runtime

    Deploy je JDK 11 applicatie op je eigen Java 17 runtime Jelle Spoelders - 5 months ago

  • Explaining software development to a class of 6-year ol…

    Explaining software development to a class of 6-year ol… Hanno Embregts - 6 months ago

Related downloads

  • Beslisboom voor een rechtmatig ‘kopietje productie’

  • Klantreferentie: Remmicom zet wetgeving om in intellige…

  • Klantreferentie RDW: Samenwerken voor veilig en vertrou…

  • Klantreferentie BeFrank: Strategische IT voor een innov…

  • Wie durft te experimenteren met data in de zorg?

Related videos

  • mijnverzekeringenopeenrij.nl

    mijnverzekeringenopeenrij.nl

  • Winnaar | Innovation Projects 2017

    Winnaar | Innovation Projects 2017

  • Explore | Info Support & HAN & Poliskluis

    Explore | Info Support & HAN & Poliskluis

  • LifeApps bij HagaZiekenhuis

    LifeApps bij HagaZiekenhuis

  • Info Support | Bedrijfsfilm

    Info Support | Bedrijfsfilm

Nieuwsbrief

* verplichte velden

Contact

  • Head office NL
  • Kruisboog 42
  • 3905 TG Veenendaal
  • T +31 318 552020
  • Call
  • Mail
  • Directions
  • Head office BE
  • Generaal De Wittelaan 17
  • bus 30 2800 Mechelen
  • T +32 15 286370
  • Call
  • Mail
  • Directions

Follow us

  • Twitter
  • Facebook
  • Linkedin
  • Youtube

Newsletter

Sign in

Extra

  • Media Library
  • Disclaimer
  • Algemene voorwaarden
  • ISHBS Webmail
  • Extranet
Beheer cookie toestemming
Deze website maakt gebruik van Functionele en Analytische cookies voor website optimalisatie en statistieken.
Functioneel
Altijd actief
De technische opslag of toegang is strikt noodzakelijk voor het legitieme doel het gebruik mogelijk te maken van een specifieke dienst waarom de abonnee of gebruiker uitdrukkelijk heeft gevraagd, of met als enig doel de uitvoering van de transmissie van een communicatie over een elektronisch communicatienetwerk.
Voorkeuren
De technische opslag of toegang is noodzakelijk voor het legitieme doel voorkeuren op te slaan die niet door de abonnee of gebruiker zijn aangevraagd.
Statistieken
De technische opslag of toegang die uitsluitend voor statistische doeleinden wordt gebruikt. De technische opslag of toegang die uitsluitend wordt gebruikt voor anonieme statistische doeleinden. Zonder dagvaarding, vrijwillige naleving door uw Internet Service Provider, of aanvullende gegevens van een derde partij, kan informatie die alleen voor dit doel wordt opgeslagen of opgehaald gewoonlijk niet worden gebruikt om je te identificeren.
Marketing
De technische opslag of toegang is nodig om gebruikersprofielen op te stellen voor het verzenden van reclame, of om de gebruiker op een website of over verschillende websites te volgen voor soortgelijke marketingdoeleinden.
Beheer opties Beheer diensten Beheer leveranciers Lees meer over deze doeleinden
Voorkeuren
{title} {title} {title}