[原创] 在XP/2K 下实现 Win+Ctrl+Del 等键的屏蔽的方法,附源码与演示程序下载。
作者: 陆岛工作室
在 Windows 98 下我们知道用 SystemParametersInfo(SPI_SETFASTTASKSWITCH, 1, @temp, 0); 来实现 Win+Ctrl+Del 的屏蔽,但这种方法在NT的操作系统下如 Windows XP / Windows 2000等就不行了,这里我给大家提供一个新的方法,采用钩子拦截的方式,来实现屏蔽系统按键。本示例附详细的源码与演示程序。
源码与演示程序。
实现方法的代码如下:
在 Windows 98 下我们知道用 SystemParametersInfo(SPI_SETFASTTASKSWITCH, 1, @temp, 0); 来实现 Win+Ctrl+Del 的屏蔽,但这种方法在NT的操作系统下如 Windows XP / Windows 2000等就不行了,这里我给大家提供一个新的方法,采用钩子拦截的方式,来实现屏蔽系统按键。本示例附详细的源码与演示程序。
源码与演示程序。
实现方法的代码如下:
{*******************************************************************************
XOtecExpress Visual Component Library [陆岛工作室]
Copyright (c) 2008 XOtec Studio. [PengJunli]
By: PengJunLi Build: 2008-06
E-mail: iinsnian@126.com XOtec@vip.QQ.com QQ:442801172
*******************************************************************************}
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes;
type
tagKBDLLHOOKSTRUCT = packed record
vkCode: DWORD;
ScanCode: DWORD;
Flags: DWORD;
Time: DWORD;
dwExtraInfo: DWORD;
end;
KBDLLHOOKSTRUCT = tagKBDLLHOOKSTRUCT;
PKBDLLHOOKSTRUCT = ^KBDLLHOOKSTRUCT;
const
WH_KEYBOARD_LL = 13;
LLKHF_ALTDOWN = $20;
//采用钩子的方法屏蔽系统按键
function DisableTaskKeys(Disable: Boolean): Boolean;
implementation
var
hhkLowLevelKybd: HHOOK;
{ LowLevelKeyboardProc }
function LowLevelKeyboardProc(nCode: Integer; WParam: WPARAM; LParam: LPARAM): LRESULT; stdcall;
var
fEatKeystroke: BOOL;
p: PKBDLLHOOKSTRUCT;
begin
Result := 0;
fEatKeystroke := FALSE;
p := PKBDLLHOOKSTRUCT (lParam);
if (nCode = HC_ACTION) then //nCode值为HC_ACTION时表示WParam和LParam参数包涵了按键消息
begin
//拦截按键消息并测试是否是Ctrl+Esc、Alt+Tab、和Alt+Esc功能键。
case wParam of
WM_KEYDOWN, WM_SYSKEYDOWN, WM_KEYUP, WM_SYSKEYUP:
fEatKeystroke :=
((p.vkCode = VK_TAB) and ((p.flags and LLKHF_ALTDOWN) <> 0)) or // Alt+Tab
((p.vkCode = VK_ESCAPE) and ((p.flags and LLKHF_ALTDOWN) <> 0))or //
(p.vkCode = VK_Lwin) or (p.vkCode = VK_Rwin) or (p.vkCode = VK_apps) or //屏蔽WIN按键
((p.vkCode = VK_ESCAPE) and ((GetKeyState(VK_CONTROL) and $8000) <> 0)) or
((p.vkCode = VK_F4) and ((p.flags and LLKHF_ALTDOWN) <> 0)) or
((p.vkCode = VK_SPACE) and ((p.flags and LLKHF_ALTDOWN) <> 0)) OR
(((p.vkCode = VK_CONTROL) and (P.vkCode = LLKHF_ALTDOWN and p.flags) and (P.vkCode = VK_Delete)));
end;
end;
if fEatKeystroke = True then
Result := 1;
if nCode <> 0 then
Result := CallNextHookEx(0, nCode, wParam, lParam);
end;
{ DisableTaskKeys }
function DisableTaskKeys(Disable: Boolean): Boolean;
begin
Result := False;
if (hhkLowLevelKybd = 0) and Disable then
begin
hhkLowLevelKybd :=SetWindowsHookExW(WH_KEYBOARD_LL, LowLevelKeyboardProc, Hinstance, 0); //设置钩子
Result := hhkLowLevelKybd<>0; // 返回设置成功
end else if not Disable and (hhkLowLevelKybd<>0) then
begin
if UnhookWindowsHookEx(hhkLowLevelKybd) then //卸载键盘钩子
begin
Result := True;
hhkLowLevelKybd := 0;
end;
end;
end;
end.
XOtecExpress Visual Component Library [陆岛工作室]
Copyright (c) 2008 XOtec Studio. [PengJunli]
By: PengJunLi Build: 2008-06
E-mail: iinsnian@126.com XOtec@vip.QQ.com QQ:442801172
*******************************************************************************}
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes;
type
tagKBDLLHOOKSTRUCT = packed record
vkCode: DWORD;
ScanCode: DWORD;
Flags: DWORD;
Time: DWORD;
dwExtraInfo: DWORD;
end;
KBDLLHOOKSTRUCT = tagKBDLLHOOKSTRUCT;
PKBDLLHOOKSTRUCT = ^KBDLLHOOKSTRUCT;
const
WH_KEYBOARD_LL = 13;
LLKHF_ALTDOWN = $20;
//采用钩子的方法屏蔽系统按键
function DisableTaskKeys(Disable: Boolean): Boolean;
implementation
var
hhkLowLevelKybd: HHOOK;
{ LowLevelKeyboardProc }
function LowLevelKeyboardProc(nCode: Integer; WParam: WPARAM; LParam: LPARAM): LRESULT; stdcall;
var
fEatKeystroke: BOOL;
p: PKBDLLHOOKSTRUCT;
begin
Result := 0;
fEatKeystroke := FALSE;
p := PKBDLLHOOKSTRUCT (lParam);
if (nCode = HC_ACTION) then //nCode值为HC_ACTION时表示WParam和LParam参数包涵了按键消息
begin
//拦截按键消息并测试是否是Ctrl+Esc、Alt+Tab、和Alt+Esc功能键。
case wParam of
WM_KEYDOWN, WM_SYSKEYDOWN, WM_KEYUP, WM_SYSKEYUP:
fEatKeystroke :=
((p.vkCode = VK_TAB) and ((p.flags and LLKHF_ALTDOWN) <> 0)) or // Alt+Tab
((p.vkCode = VK_ESCAPE) and ((p.flags and LLKHF_ALTDOWN) <> 0))or //
(p.vkCode = VK_Lwin) or (p.vkCode = VK_Rwin) or (p.vkCode = VK_apps) or //屏蔽WIN按键
((p.vkCode = VK_ESCAPE) and ((GetKeyState(VK_CONTROL) and $8000) <> 0)) or
((p.vkCode = VK_F4) and ((p.flags and LLKHF_ALTDOWN) <> 0)) or
((p.vkCode = VK_SPACE) and ((p.flags and LLKHF_ALTDOWN) <> 0)) OR
(((p.vkCode = VK_CONTROL) and (P.vkCode = LLKHF_ALTDOWN and p.flags) and (P.vkCode = VK_Delete)));
end;
end;
if fEatKeystroke = True then
Result := 1;
if nCode <> 0 then
Result := CallNextHookEx(0, nCode, wParam, lParam);
end;
{ DisableTaskKeys }
function DisableTaskKeys(Disable: Boolean): Boolean;
begin
Result := False;
if (hhkLowLevelKybd = 0) and Disable then
begin
hhkLowLevelKybd :=SetWindowsHookExW(WH_KEYBOARD_LL, LowLevelKeyboardProc, Hinstance, 0); //设置钩子
Result := hhkLowLevelKybd<>0; // 返回设置成功
end else if not Disable and (hhkLowLevelKybd<>0) then
begin
if UnhookWindowsHookEx(hhkLowLevelKybd) then //卸载键盘钩子
begin
Result := True;
hhkLowLevelKybd := 0;
end;
end;
end;
end.
作者: 陆岛工作室