在 C++ 中阻止窗口的键盘/鼠标输入。

huangapple go评论110阅读模式
英文:

Block input in hwnd keyboard/mouse input in C++

问题

我正在尝试创建一个类似于ReWASD的程序,我需要在应用程序中阻止键盘/鼠标输入,以避免两个设备之间的冲突。

我尝试使用BlockInput() / SetWindowsHookEx()函数。

英文:

I'm trying making a program similar to ReWASD, I need to block keyboard/mouse input in app to avoid a conflict between two devices

I tried use functions BlockInput()/SetWindowsHookEx()

答案1

得分: 2

阻止输入(无需编写自定义驱动程序)的唯一方法是使用SetWindowsHookEx()键盘/鼠标钩子。 其文档告诉您如何针对每种类型的钩子来阻止输入:

KeyboardProc回调函数

返回值

类型:LRESULT

如果代码小于零,则钩子过程必须返回由CallNextHookEx返回的值。

如果代码大于或等于零,且钩子过程未处理消息,强烈建议您调用CallNextHookEx并返回其返回值;否则,已安装WH_KEYBOARD钩子的其他应用程序将不会接收到钩子通知,并可能因此表现不正常。 如果钩子过程处理了消息,它可以返回一个非零值,以阻止系统将消息传递给钩子链或目标窗口过程的其余部分。

LowLevelKeyboardProc回调函数

返回值

类型:LRESULT

如果nCode小于零,则钩子过程必须返回由CallNextHookEx返回的值。

如果nCode大于或等于零,且钩子过程未处理消息,强烈建议您调用CallNextHookEx并返回其返回值;否则,已安装WH_KEYBOARD_LL钩子的其他应用程序将不会接收到钩子通知,并可能因此表现不正常。 如果钩子过程处理了消息,它可以返回一个非零值,以阻止系统将消息传递给钩子链或目标窗口过程的其余部分。

MouseProc回调函数

返回值

类型:LRESULT

如果nCode小于零,则钩子过程必须返回由CallNextHookEx返回的值。

如果nCode大于或等于零,且钩子过程未处理消息,强烈建议您调用CallNextHookEx并返回其返回值;否则,已安装WH_MOUSE钩子的其他应用程序将不会接收到钩子通知,并可能因此表现不正常。 如果钩子过程处理了消息,它可以返回一个非零值,以阻止系统将消息传递给目标窗口过程。

LowLevelMouseProc回调函数

返回值

类型:LRESULT

如果nCode小于零,则钩子过程必须返回由CallNextHookEx返回的值。

如果nCode大于或等于零,且钩子过程未处理消息,强烈建议您调用CallNextHookEx并返回其返回值;否则,已安装WH_MOUSE_LL钩子的其他应用程序将不会接收到钩子通知,并可能因此表现不正常。 如果钩子过程处理了消息,它可以返回一个非零值,以阻止系统将消息传递给钩子链或目标窗口过程的其余部分。

但请注意,只有WH_MOUSE钩子会告诉您鼠标输入发送到的HWND。其他钩子不提供HWND。事件将发送到在处理事件时具有输入焦点的任何HWND

另外,这些钩子都不会告诉您发送输入的设备(如果要阻止来自特定设备的输入),对此,您必须使用原始输入API,并将其事件与SetWindowHookEx()钩子事件协调(因为原始输入无法阻止输入)。参见:组合原始输入和键盘钩子以有选择地阻止多个键盘的输入 以获取详细信息(以及可能出现的问题)。

英文:

The only way to block input (without writing custom drivers) is to use SetWindowsHookEx() keyboard/mouse hooks. Its documentation tells you how to block input for each type of hook:

KeyboardProc callback function

> Return value
>
> Type: LRESULT
>
> If code is less than zero, the hook procedure must return the value returned by CallNextHookEx.
>
> If code 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 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.

LowLevelKeyboardProc callback function

> Return value
>
> Type: LRESULT
>
> If nCode is less than zero, the hook procedure must return the value returned by 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.

MouseProc callback function

> Return value
>
> Type: LRESULT
>
> If nCode is less than zero, the hook procedure must return the value returned by 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_MOUSE 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 target window procedure.

LowLevelMouseProc callback function

> Return value
>
> Type: LRESULT
>
> If nCode is less than zero, the hook procedure must return the value returned by 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_MOUSE_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.

Note, however, that only a WH_MOUSE hook will tell you the HWND that the mouse input is being sent to. The other hooks do not give you an HWND. The events will go to whichever HWND has the input focus when the events are processed.

Also, none of the hooks tell you the device that is sending the input (if you want to block input from only a specific device), for that you have to use the Raw Input API and coordinate its events with the SetWindowHookEx() hook events (as Raw Input can't block input). See: Combining Raw Input and keyboard Hook to selectively block input from multiple keyboards for details (and gotchas) about doing that.

huangapple
  • 本文由 发表于 2023年3月4日 02:06:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630483.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定