views:

195

answers:

2

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

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

A: 

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
Eloff
The problem is not to get access to the clipboard, but to get access when the user uses the 'paste-function'.
eflles
+1  A: 
if (e.Key == Key.V)
        {
            if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
            {
                //do what you want on paste
            }
        }

You have to use this on the keyUp event. More details can be found here: http://msdn.microsoft.com/en-us/library/cc189015%28VS.95%29.aspx