Delphi 阻止键盘和鼠标
Delphi block Keyboard & Mouse
如何分别屏蔽键盘和鼠标?我尝试使用 BlockInput,它在 Windows 10 中不起作用,还尝试了以下方法:
function KBHookHandler(ACode: Integer; WParam: WParam; LParam: LParam)
: LResult; stdcall;
begin
if ACode < 0 then
// Immediately pass the event to next hook
Result := CallNextHookEx(Hook, ACode, WParam, LParam)
else
// by setting Result to values other than 0 means we drop/erase the event
Result := 1;
end;
function DisableKeyboard : boolean;
begin
if Hook = 0 then
// install the hook
// Hook := SetWindowsHookEx(WH_KEYBOARD, @KBHookHandler, HINSTANCE, 0);
Hook := SetWindowsHookEx(WH_KEYBOARD, @KBHookHandler, 0, 0);
Result := Hook <> 0;
end;
我的要求是在 Windows 7、Windows 8 和 Windows 10 中分别屏蔽键盘和鼠标。
你在 Embarcadero 的 Delphi NativeAPI forum 上发布了同样的问题,所以我会给你我在那里发布的相同答案。
阻止鼠标和键盘分离的唯一方法是使用单独的键盘和鼠标挂钩。
BlockInput() 适用于 Windows 10。但它会阻止所有输入,您不能选择性地使用它。
当挂钩除您自己的进程之外的其他进程时,您的挂钩必须在 DLL 中,并且您必须将 DLL 的实例传递给 SetWindowsHookEx()
的第三个参数:
An error may occur if the hMod parameter is NULL and the dwThreadId parameter is zero or specifies the identifier of a thread created by another process.
通过为 hMod
指定 0,回调将只在调用进程中工作,因为这是唯一可以访问回调的进程。当你设置dwThreadId
参数为0全局hook多个线程时,hMod
必须指向一个可以注入其他进程的DLL。
此外,您可能需要单独的 32 位和 64 位 DLL,以便分别正确挂接 32 位和 64 位进程。但是一定要确保安装钩子的线程有一个消息循环:
This hook may be called in the context of the thread that installed it. The call is made by sending a message to the thread that installed the hook. Therefore, the thread that installed the hook must have a message loop.
Because hooks run in the context of an application, they must match the "bitness" of the application. If a 32-bit application installs a global hook on 64-bit Windows, the 32-bit hook is injected into each 32-bit process (the usual security boundaries apply). In a 64-bit process, the threads are still marked as "hooked." However, because a 32-bit application must run the hook code, the system executes the hook in the hooking app's context; specifically, on the thread that called SetWindowsHookEx. This means that the hooking application must continue to pump messages or it might block the normal functioning of the 64-bit processes.
If a 64-bit application installs a global hook on 64-bit Windows, the 64-bit hook is injected into each 64-bit process, while all 32-bit processes use a callback to the hooking application.
To hook all applications on the desktop of a 64-bit Windows installation, install a 32-bit global hook and a 64-bit global hook, each from appropriate processes, and be sure to keep pumping messages in the hooking application to avoid blocking normal functioning. If you already have a 32-bit global hooking application and it doesn't need to run in each application's context, you may not need to create a 64-bit version. {quote}
您无法锁定整个系统的原因是您没有正确地挂接整个系统。
如何分别屏蔽键盘和鼠标?我尝试使用 BlockInput,它在 Windows 10 中不起作用,还尝试了以下方法:
function KBHookHandler(ACode: Integer; WParam: WParam; LParam: LParam)
: LResult; stdcall;
begin
if ACode < 0 then
// Immediately pass the event to next hook
Result := CallNextHookEx(Hook, ACode, WParam, LParam)
else
// by setting Result to values other than 0 means we drop/erase the event
Result := 1;
end;
function DisableKeyboard : boolean;
begin
if Hook = 0 then
// install the hook
// Hook := SetWindowsHookEx(WH_KEYBOARD, @KBHookHandler, HINSTANCE, 0);
Hook := SetWindowsHookEx(WH_KEYBOARD, @KBHookHandler, 0, 0);
Result := Hook <> 0;
end;
我的要求是在 Windows 7、Windows 8 和 Windows 10 中分别屏蔽键盘和鼠标。
你在 Embarcadero 的 Delphi NativeAPI forum 上发布了同样的问题,所以我会给你我在那里发布的相同答案。
阻止鼠标和键盘分离的唯一方法是使用单独的键盘和鼠标挂钩。
BlockInput() 适用于 Windows 10。但它会阻止所有输入,您不能选择性地使用它。
当挂钩除您自己的进程之外的其他进程时,您的挂钩必须在 DLL 中,并且您必须将 DLL 的实例传递给 SetWindowsHookEx()
的第三个参数:
An error may occur if the hMod parameter is NULL and the dwThreadId parameter is zero or specifies the identifier of a thread created by another process.
通过为 hMod
指定 0,回调将只在调用进程中工作,因为这是唯一可以访问回调的进程。当你设置dwThreadId
参数为0全局hook多个线程时,hMod
必须指向一个可以注入其他进程的DLL。
此外,您可能需要单独的 32 位和 64 位 DLL,以便分别正确挂接 32 位和 64 位进程。但是一定要确保安装钩子的线程有一个消息循环:
This hook may be called in the context of the thread that installed it. The call is made by sending a message to the thread that installed the hook. Therefore, the thread that installed the hook must have a message loop.
Because hooks run in the context of an application, they must match the "bitness" of the application. If a 32-bit application installs a global hook on 64-bit Windows, the 32-bit hook is injected into each 32-bit process (the usual security boundaries apply). In a 64-bit process, the threads are still marked as "hooked." However, because a 32-bit application must run the hook code, the system executes the hook in the hooking app's context; specifically, on the thread that called SetWindowsHookEx. This means that the hooking application must continue to pump messages or it might block the normal functioning of the 64-bit processes.
If a 64-bit application installs a global hook on 64-bit Windows, the 64-bit hook is injected into each 64-bit process, while all 32-bit processes use a callback to the hooking application.
To hook all applications on the desktop of a 64-bit Windows installation, install a 32-bit global hook and a 64-bit global hook, each from appropriate processes, and be sure to keep pumping messages in the hooking application to avoid blocking normal functioning. If you already have a 32-bit global hooking application and it doesn't need to run in each application's context, you may not need to create a 64-bit version. {quote}
您无法锁定整个系统的原因是您没有正确地挂接整个系统。