有没有一种方法可以在 C++ 中以一种没有软件可以告诉我我实际上没有按下键的方式来模拟按键
Is there a method to simulate a key press in C++ in a way that no software could tell that i'm not actually pressing the key
我试过 SendInput 和 keybd_event,有时它们工作正常,但某些 运行 程序无法识别我想使用 C++ 代码按键。 c++ 中是否有一个信号与我机械按键时发出的信号相同(因此没有程序可以区分)?
或者,如果在 C++ 中不可行,那么在任何其他编程语言中是否可行?
Is there a signal in c++ that is the same as the signal that emits when i press a key mechanically (so that no program can tell the difference)?
这正是 SendInput()
和 keybd_event()
的用途。 They inject events into the same hardware input queue that the actual keyboard driver inserts its events into。它们以相同的方式进行处理和路由,因此大多数系统不知道物理事件和注入事件之间的区别。
但是,系统的一个区域 确实 知道其中的区别:通过 SetWindowsHookEx(WH_KEYBOARD_LL)
的低级键盘挂钩。 callback function receives a pointer to a KBDLLHOOKSTRUCT
结构,其 flags
成员包含用于注入事件的 LLKHF_INJECTED
标志。回调可以选择丢弃事件,这样它们就不会到达应用程序。
因此很可能任何对您的模拟击键没有反应的应用都经过专门编码以忽略注入的输入。
防止这种情况(无需编写自己的键盘驱动程序)的唯一方法是编写自己的低级键盘挂钩并在安装其他低级键盘挂钩后安装它。这会将您的钩子放在钩子链的顶部。然后它可以通过简单地 not 调用 CallNextHookEx()
来跳过其他钩子。挂钩按从后到前的顺序调用。根据文档:
If nCode is greater than or equal to zero, and the hook procedure did not process the message, it is highly recommended that you call CallNextHookEx and return the value it returns; otherwise, other applications that have installed WH_KEYBOARD_LL hooks will not receive hook notifications and may behave incorrectly as a result. If the hook procedure processed the message, it may return a nonzero value to prevent the system from passing the message to the rest of the hook chain or the target window procedure.
但是,如果某个应用不厌其烦地使用键盘挂钩来忽略注入的事件,它很可能还会考虑挂钩顺序,例如定期重新安装其挂钩以将其重新打开挂钩链的顶端,避免其他挂钩的干扰。
我试过 SendInput 和 keybd_event,有时它们工作正常,但某些 运行 程序无法识别我想使用 C++ 代码按键。 c++ 中是否有一个信号与我机械按键时发出的信号相同(因此没有程序可以区分)?
或者,如果在 C++ 中不可行,那么在任何其他编程语言中是否可行?
Is there a signal in c++ that is the same as the signal that emits when i press a key mechanically (so that no program can tell the difference)?
这正是 SendInput()
和 keybd_event()
的用途。 They inject events into the same hardware input queue that the actual keyboard driver inserts its events into。它们以相同的方式进行处理和路由,因此大多数系统不知道物理事件和注入事件之间的区别。
但是,系统的一个区域 确实 知道其中的区别:通过 SetWindowsHookEx(WH_KEYBOARD_LL)
的低级键盘挂钩。 callback function receives a pointer to a KBDLLHOOKSTRUCT
结构,其 flags
成员包含用于注入事件的 LLKHF_INJECTED
标志。回调可以选择丢弃事件,这样它们就不会到达应用程序。
因此很可能任何对您的模拟击键没有反应的应用都经过专门编码以忽略注入的输入。
防止这种情况(无需编写自己的键盘驱动程序)的唯一方法是编写自己的低级键盘挂钩并在安装其他低级键盘挂钩后安装它。这会将您的钩子放在钩子链的顶部。然后它可以通过简单地 not 调用 CallNextHookEx()
来跳过其他钩子。挂钩按从后到前的顺序调用。根据文档:
If nCode is greater than or equal to zero, and the hook procedure did not process the message, it is highly recommended that you call CallNextHookEx and return the value it returns; otherwise, other applications that have installed WH_KEYBOARD_LL hooks will not receive hook notifications and may behave incorrectly as a result. If the hook procedure processed the message, it may return a nonzero value to prevent the system from passing the message to the rest of the hook chain or the target window procedure.
但是,如果某个应用不厌其烦地使用键盘挂钩来忽略注入的事件,它很可能还会考虑挂钩顺序,例如定期重新安装其挂钩以将其重新打开挂钩链的顶端,避免其他挂钩的干扰。