C# - Win32 PostMessage Ctrl-C (copy) to not focused window
<p>I want to send a keycombination like Ctrl-C to a non focused window (game). </p>

<p>I've tried a lot of different ways, like sending "WM_COPY", "WM_GETTEXT", "WM_CHAR"+ 0x03 or using keybd_event to press Ctrl.
Nothing of this is working, but i'm able to PostMessage with "WM_KEYDOWN" and "WM_KEYUP".</p>

<p>So I've tried now to SetKeyBoardState and modify the Ctrl keystate and then sending a PostMessage, but it doesn't work, too.</p>

<p>Test with Ctrl-V:</p>

<pre><code>//Some time to move mouse to the control
System.Threading.Thread.Sleep(3000); 

//Control pointer
IntPtr hWnd = WindowFromPoint(Control.MousePosition);

uint targetThreadId = GetWindowThreadProcessId(hWnd, IntPtr.Zero);
uint myThreadId = GetCurrentThreadId();
AttachThreadInput(myThreadId, targetThreadId, true);

var keys = new byte[256];
GetKeyboardState(keys);

var key = (int)Keys.ControlKey;
var prevState = keys[key];
keys[key] = 128;

/* Test if ctrl pressed
if ((keys[key] &amp; 0x80) != 0)
{
    MessageBox.Show("Ctrl pressed");
}
else
{
    MessageBox.Show("Ctrl not pressed");
}*/

SetKeyboardState(keys); 

PostMessage(hWnd, WM_KEYDOWN, Convert.ToInt32(Keys.V), 0);
PostMessage(hWnd, WM_KEYUP, Convert.ToInt32(Keys.V), 0);

keys[key] = prevState;
SetKeyboardState(keys);

AttachThreadInput(myThreadId, targetThreadId, false);
</code></pre>

<p>Maby someone has an idea?</p>

<p>Regards, Joie95</p>