2

Solved! See [SOLUTION]

Thanks for any help you can provide. It's much appreciated!

In a nutshell: I'm trying to send Ctrl+V to SSMS 2012 with SendKeys.Send("^{v}"), but it doesn't work. It's working fine with Notepad, UltraEdit, Word, Excel, Chrome, you name it. It even works in Microsoft Visual Studio 2010.

In details: I have an application that runs in the background. Using a keyboard shortcut, this application displays a popup window with options. Depending on the option I choose it saves what's related to it into the clipboard. I then close that popup window, get the new foreground window (which should be the one I had before displaying the popup) and try to paste what's in my clipboard with SendKeys.

  1. It works with pretty much every application I try it with, except SSMS
  2. If I manually press Ctrl+V it pastes what I have in my clipboard (text usually)
  3. I've added some code to display the title of the window I got with the GetForegroundWindow and it does give me the correct SSMS window
  4. What's sad about all this is that once in a while (very rarely), the text is correctly pasted in SSMS, but it doesn't work the second after.
  5. I never get the MessageBox saying the SetForegroundWindow failed.
  6. If I replace the single SendKey with 3 SendKeys to send "A", "B" and "C", B and C are sent but not A. Yes I've tried using a sleep thinking it needed time to write the first SendKey, but that didn't change anything.
  7. I did try SendKeys.SendWait instead, but didn't get different results.

Here's the code from the moment I close the popup

    this.Close();

    IntPtr handle = GetForegroundWindow();

    if (!handle.Equals(IntPtr.Zero))
    {
        if (SetForegroundWindow(handle))
        {
            //Optionnal just to show the window title
            const int nChars = 256;
            StringBuilder Buff = new StringBuilder(nChars);

            if (GetWindowText(handle, Buff, nChars) > 0)
            {
                MessageBox.Show(Buff.ToString());
            }

            //[SOLUTION] Sending a useless key seems to solve my SSMS problem without affecting the other applications.
            SendKeys.Send("{F14}");

            //Sending Ctrl+V
            SendKeys.Send("^{v}");
        }
        else
        {
            MessageBox.Show("SetForegroundWindow failed");
        }
    }

Hope someone can help. Thanks in advance!

Vinster
  • 21
  • 2
  • Oh well... I just came up with a "solution". Since the first send key doesn't seem to register on SSMS, I added a useless "SendKeys.Send("{F14}");" before my Ctrl+V. My text is now correctly pasted in SSMS while in Notepad the F14 key doesn't do anything which is good. I started by using {LEFT}, but I was changing lines in Notepad and UltraEdit so I opted for F14 instead. – Vinster Jun 06 '13 at 02:33

1 Answers1

0

See http://msdn.microsoft.com/en-us/library/system.windows.forms.sendkeys.aspx

... The SendKeys class is susceptible to timing issues, which some developers have had to work around. The updated implementation is still susceptible to timing issues, but is slightly faster and may require changes to the workarounds. The SendKeys class tries to use the previous implementation first, and if that fails, uses the new implementation. As a result, the SendKeys class may behave differently on different operating systems. Additionally, when the SendKeys class uses the new implementation, the SendWait method will not wait for messages to be processed when they are sent to another process. ...

Ross Bush
  • 14,648
  • 2
  • 32
  • 55