title: C# SendKeys and Microsoft SQL Server Management Studio 2012 (SSMS)
body: <p>Solved! See <strong>[SOLUTION]</strong></p>
  
  <p>Thanks for any help you can provide. It's much appreciated!</p>
  
  <p><strong>In a nutshell</strong>: 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.</p>
  
  <p><strong>In details</strong>: 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.</p>
  
  <ol>
  <li>It works with pretty much every application I try it with, except SSMS</li>
  <li>If I manually press Ctrl+V it pastes what I have in my clipboard (text usually)</li>
  <li>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</li>
  <li>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.</li>
  <li>I never get the MessageBox saying the SetForegroundWindow failed.</li>
  <li>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.</li>
  <li>I did try SendKeys.SendWait instead, but didn't get different results.</li>
  </ol>
  
  <p>Here's the code from the moment I close the popup</p>
  
  <pre><code>    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) &gt; 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");
          }
      }
  </code></pre>
  
  <p>Hope someone can help.
  Thanks in advance!</p>