我试图使用InjectTouchInput发送触摸事件到特定的窗口。 POINTER_INFO结构有hwndTarget
字段,据我hwndTarget
,它指定目标窗口发送input。
但是当我尝试使用这个参数时,注入失败,代码为ERROR_INVALID_PARAMETER (87)
码:
void MakePointerTouchInfo(POINTER_TOUCH_INFO& contact, int x, int y, int radius, int id, int orientation = 90, int pressure = 32000) { ZeroMemory(&contact, sizeof(POINTER_TOUCH_INFO)); contact.pointerInfo.pointerType = PT_TOUCH; contact.pointerInfo.ptPixelLocation.x = x; contact.pointerInfo.ptPixelLocation.y = y; contact.pointerInfo.pointerId = id; contact.touchFlags = TOUCH_FLAG_NONE; contact.pointerInfo.pointerFlags = POINTER_FLAG_DOWN | POINTER_FLAG_INRANGE | POINTER_FLAG_INCONTACT; contact.touchMask = TOUCH_MASK_NONE; } POINTER_TOUCH_INFO contacts[2]; // to start injection in separate thread. DWORD makeInjection_(LPVOID p) { InitializeTouchInjection(5, TOUCH_FEEDBACK_DEFAULT); MakePointerTouchInfo(contacts[0], 200, 300, 2, 1); MakePointerTouchInfo(contacts[1], 250, 300, 2, 6); HWND hwnd = hWnd; // if I don't set hwnd (default 0), it works contacts[0].pointerInfo.hwndTarget = contacts[1].pointerInfo.hwndTarget = hwnd; if (!InjectTouchInput(1, contacts)) { // //here it fails // LPWSTR str = new wchar_t[255]; auto t = GetLastError(); wsprintf(str, L"%d\n", t); OutputDebugString(str); } contacts[0].pointerInfo.pointerFlags = POINTER_FLAG_UPDATE | POINTER_FLAG_INRANGE | POINTER_FLAG_INCONTACT; contacts[1].pointerInfo.pointerFlags = POINTER_FLAG_UPDATE | POINTER_FLAG_INRANGE | POINTER_FLAG_INCONTACT; //drag them from/to each other for (int i = 0; i < 100; i++) { contacts[0].pointerInfo.ptPixelLocation.x += 1; contacts[1].pointerInfo.ptPixelLocation.x -= 1; bool s = InjectTouchInput(1, contacts); Sleep(10); } //release them contacts[0].pointerInfo.pointerFlags = POINTER_FLAG_UPDATE; contacts[1].pointerInfo.pointerFlags = POINTER_FLAG_UPDATE; bool success2 = InjectTouchInput(1, contacts); return 0;
}
我认为问题是, hwndTarget
不是你发送输入的窗口,它是一个输出字段,它告诉你哪个窗口有输入捕捉和/或在你指定的(x,y)位置下注射接触。 正如MSDN所说:
注入的输入不会发送到特定的窗口,而是从MSDN引用到桌面:
注入的输入被发送到注入进程正在运行的会话的桌面。
也许你需要探索RegisterPointerInputTarget
函数,该函数“允许调用者注册一个指定类型的所有指针输入被重定向到的目标窗口” 。 但是,在所有情况下都不能这样做重定向输入: