delphi 在XP/2K 下实现 Win+Ctrl+Del 等键的屏蔽的方法


本文整理自网络,侵删。

 {*******************************************************************************

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.

相关阅读 >>

Delphi中带缓存的数据更新技术

Delphi 如何使用sendmessage发送后台组合键消息(ctrl+xxx)

Delphi中messagebox用法

Delphi 的字符串在 firemonkey 模式下

Delphi程序带参数运行

Delphi xe安装后配置android的sdk的方法

键盘钩子,鼠标钩子,Delphi环境下编写的键盘鼠标钩子的程序示例

Delphi xe 共享一个之前封装的多进程共享内存的memorystream

Delphi 对比时间的函数

Delphi调试服务程序的两种方法

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...