RSS订阅 | 匿名投稿
您的位置:网站首页 > 系统 > 正文

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

作者:admin 来源:未知 日期:2010/5/3 22:39:27 人气: 标签:

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

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.

读完这篇文章后,您心情如何?
0
0
0
0
0
0
0
0
本文网址: