C# - Win32 PostMessage Ctrl-C(复制)到非聚焦窗口

时间:2016-09-10 20:09:55

标签: c# copy copy-paste sendmessage postmessage

我想将像Ctrl-C这样的键组合发送到非聚焦窗口(游戏)。

我尝试了很多不同的方法,例如发送“WM_COPY”,“WM_GETTEXT”,“WM_CHAR”+ 0x03或使用keybd_event按Ctrl键。 这一切都没有用,但我可以使用“WM_KEYDOWN”和“WM_KEYUP”进行PostMessage。

所以我现在尝试使用SetKeyBoardState并修改Ctrl keystate,然后发送PostMessage,但它也不起作用。

使用Ctrl-V测试:

//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] & 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);

Maby有人有想法吗?

问候,Joie95

0 个答案:

没有答案