unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls
type
TForm1 = class(TForm)
Button1: TButton
procedure FormCreate(Sender: TObject)
procedure FormDestroy(Sender: TObject)
procedure Button1Click(Sender: TObject)
private
{ Private declarations }
public
{ Public declarations }
end
KBDLLHOOKSTRUCT=record
vkCode
WORD
scanCode
WORD
flags
WORD
time
WORD
dwExtraInfo:ulong
end
LPKBDLLHOOKSTRUCT=^KBDLLHOOKSTRUCT
var
Form1: TForm1
//定义句柄
OldHook:HHOOK;// 钩子 句柄
implementation
{$R *.dfm}
const
LLKHF_ALTDOWN=$20
WH_KEYBOARD_LL= 13
//键盘钩子
function KeyProc(code: Integer
wparam: WPARAM
lparam: LPARAM): LRESULT stdcall
var
pkbhs:LPKBDLLHOOKSTRUCT
begin
Result:=1
if (code < 0) then
begin
Result:= CallNextHookEx(OldHook,code, wParam, lParam)
exit
end
pkbhs := LPKBDLLHOOKSTRUCT( lParam)
if pkbhs.flags and LLKHF_ALTDOWN>0 then
begin
exit
//封锁 ALT
end
Result:=CallNextHookEx(OldHook, code, wParam, lParam)
end
//---------------------------------------------------------------------------
procedure TForm1.FormCreate(Sender: TObject)
begin
OldHook:=0
end
procedure TForm1.FormDestroy(Sender: TObject);
begin
if OldHook>0 then
UnhookWindowsHookEx(OldHook)
end
procedure TForm1.Button1Click(Sender: TObject)
begin
//注册 钩子
OldHook := SetWindowsHookEx(WH_KEYBOARD_LL {WH_KEYBOARD_LL},
KeyProc, HInstance, 0)
if (Sender is TButton) then
TButton(Sender).Enabled:=not (OldHook>0)
end
end.