在非活动应用程序中模拟鼠标移动/单击/按键
我知道如何模拟鼠标和键盘事件,但它们的行为就像用户执行这些事件一样,因此它们会影响活动的窗口。 我需要的是模拟这些输入之一,但在一个不活动的窗口中。
我并不是说它被最小化,想象一下,例如,你有 msPaint 和记事本。 记事本在油漆前面。 并且您希望在绘画窗口的某些坐标中模拟鼠标单击,但不将其设置为活动状态,从而使用户可以继续使用绘画前的记事本。
这有可能吗? 谢谢!
我已经尝试过这个:
const int WM_KEYDOWN = 0x100;
Thread.Sleep(5000);
Process x = Process.Start(@"C:\WINDOWS\NOTEPAD.EXE");
PInvokes.PlatformInvokeUSER32.SendMessage(x.MainWindowHandle, WM_KEYDOWN, ((int)Keys.W), 0);
但它不起作用=(没有做任何事情:(
I know how to simulate mouse and keyboard events, but they act as if the user did them, so they will affect the window that is active. What I need is to simulate one of those inputs, but in a Window that is not active.
I'm not saying that it is minimized, imagine for example, you have msPaint, and notepad. Notepad is in front of paint. And you want to simulate mouse clicks in certain coordinates of the paint window, but without setting it active, making it possible for the user to keep using notepad which is in fron of paint.
Is this possible at all? Thanks!
I've tried this:
const int WM_KEYDOWN = 0x100;
Thread.Sleep(5000);
Process x = Process.Start(@"C:\WINDOWS\NOTEPAD.EXE");
PInvokes.PlatformInvokeUSER32.SendMessage(x.MainWindowHandle, WM_KEYDOWN, ((int)Keys.W), 0);
but it doesn't work =( Doesn't do anything :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试
You can try the UI automation API. It supports also legacy applications.
也许可以尝试使用 PostMessage 函数:
Maybe try the PostMessage function instead:
根据目标应用程序的不同,可能会出现很多问题。 某些应用程序在 WM_KEYDOWN 或 WM_KEYUP 而不是 WM_CHAR 上触发。 此外,该键可能已经按下并被忽略。 对于像网络浏览器这样的目标,您需要获取正确的窗口句柄。 Winspector 可以为您提供。
Depending on the target app, there could be lots of issues. Some apps trigger on WM_KEYDOWN or WM_KEYUP instead of WM_CHAR. Also, the key might already be down and is being ignored. For targets like webbrowsers, you need to get the right window handle. Winspector can get that for you.
您的代码将无法工作,因为 SendMessage 要求您发送的窗口处于活动状态。
尝试发布消息。
Your code wont work because SendMessage requires that the window you're sending to be active.
Try PostMessage.