Search code examples
silverlight-4.0user-interactioncopy-paste

How to detect Ctrl+V in Silverlight 4?


What is the best way to detect Ctrl+V in Silverlight?

I want to detect Ctrl+V, to get access to the Clipboard.


Solution

  • EDIT

    To capture the CTRL+V keypress globally in your silverlight application, is fraught with difficulty. Events start at the child elements and bubble down to the parent controls, so simply handling KeyDown on your root UIElement will not work. Any text input control will first get the event and smother it (by setting Handled to true on the event args.) I think that if you use the DOM bridge and subscribe a handler to the browser KeyDown event for the silverlight element itself that you may actually be able to get to it first, and even handle it completely before any silverlight controls can. I think that would be the easiest way to intercept CTRL+V, but I have not tested it.

    Original Answer

    You should use the System.Windows.Clipboard class.

    • GetText, which retrieves text from the clipboard
    • SetText, which places text on the clipboard
    • ContainsText, which indicates whether the clipboard currently contains text