你不能这样做。
让我们首先重新表述问题陈述,以便更容易理解,为什么会出现这种情况:
“我如何说服一个选择忽略鼠标输入消息(无论是决定还是巧合)的程序来确认鼠标输入消息?”
事实证明,这部分实际上已经解决了。作为文档WM_LBUTTONDOWN https://learn.microsoft.com/en-us/windows/win32/inputdev/wm-lbuttondown notes:
如果应用程序处理此消息,它应该返回零。
您得到的结果为零,因此没有理由质疑消息已按照应用程序认为必要的程度进行处理这一事实。这大概会掉下来“巧合”分支,应用程序对鼠标消息不感兴趣,而只是将它们传递给DefWindowProc https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-defwindowprocw,厨房水槽,用于存放所有相关性不足以忽略的消息。
Key insight here is: A program that needs to process and respond to mouse input can decide to ignore mouse input messages1. (And clients that are based on mouse message handling can easily identify fake input messages https://devblogs.microsoft.com/oldnewthing/20050530-11/?p=35513, too, and respond by, y'know, not responding altogether.)
因此,从本质上讲,发送(或发布)假鼠标消息是行不通的。可靠。曾经。
这基本上给你留下了 3 种选择:
- 用户界面自动化 https://learn.microsoft.com/en-us/windows/win32/winauto/entry-uiauto-win32
- 访问自定义自动化界面
-
SendInput https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-sendinput(合并版本
keybd_event
and mouse_event
)
列出前两个选项只是为了完整性。它们通常可用于积极支持自动化的应用程序。游戏通常不会,并且防范这些途径既简单又便宜:应用程序不必这样做anything.
SendInput
也行不通。就系统而言,注入的输入的处理方式与任何其他输入相同(这博客文章 https://devblogs.microsoft.com/oldnewthing/20140213-00/?p=1773提供了一个有用的说明)。具体来说,当在窗口上单击鼠标时,该窗口将进入前台。所以这不符合申请的要求“在后台”.
即使情况并非如此,注入的输入也可以轻松可靠地识别。 A低级鼠标钩子 https://learn.microsoft.com/en-us/previous-versions/windows/desktop/legacy/ms644986(v=vs.85)就是获得一个所需的全部MSLLHOOKSTRUCT https://learn.microsoft.com/en-us/windows/win32/api/winuser/ns-winuser-msllhookstruct, whose flags
字段很容易就泄露了这些信息。由于低级钩子能够阻止输入传递到系统,因此return 1;
这就是过滤掉这些输入事件所需的全部内容。
这涵盖了所有支持的外部应用程序自动化方法。这是一条死胡同,死得不值得。
现在,如果使用虚假输入自动化在后台运行的应用程序总结了要求,那么您唯一的选择就是在虚拟化环境中运行应用程序(这确保单击保持在虚拟环境内,并且不会使应用程序到前面来)。请记住上述所有限制still申请,并且您不能使用上述任何方法。您必须实现并安装一个自定义鼠标驱动程序,该驱动程序生成的输入与真正的硬件源输入事件无法区分。
但即便如此,应用程序仍然有办法发现它们正在虚拟化环境中运行,并在运行时拒绝运行。
底线是:作弊很难。Really难的。而且您的问题没有简单的解决方案。
1 Mouse input messages are generated by the system as a convenience. They represent a useful (and lossy) abstraction over hardware input events. The full fidelity of those hardware input events is generally not required by "standard" applications.
Games, on the other hand, will usually use lower-level input processing infrastructure, such as Raw Input https://learn.microsoft.com/en-us/windows/win32/inputdev/raw-input, and not even look at any of the higher-level processing artifacts.