这是我在delphi精要中抄来的钩子的讲解,但是我的问题不是要把按下的键记录下来,而是要屏蔽掉某个按键,譬如CTRL+ALT+DEL、或ALT+F4等键,如何改呢?
谢谢!!!
unit HKProc;interfaceuses
  Windows;      var
  F: File of Char; 
  function EnableHotKeyHook: BOOL; stdcall;
  function DisableHotKeyHook: BOOL; stdcall;
  procedure HotKeyHookExit;implementationconst
  _KeyPressMask = $80000000 ; var
  C: Char;
  ShiftDown,CapsDown: Boolean;
  hNextHookProc: HHook;function KeyboardHookHandler(
  iCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
begin
  Result := 0;
  if iCode < 0 then
  begin
    Result := CallNextHookEx(hNextHookProc,iCode,wParam,lParam);
    Exit;
  end;
{  if (lParam and _KeyPressMask) = 0 then //第32位为0,表示KeyDown状态
  begin
    ShiftDown := (GetKeyState($10) and _KeyPressMask) = _KeyPressMask;
    CapsDown := (GetKeyState($14) and 1) = 1;    if wParam < 65 then
    begin
      if ShiftDown then
        C := Chr(wParam-16)
      else
        C := Chr(wParam);
    end else
    begin
      if wParam in [96..105] then
        C := Chr(wParam-48)  //数字键盘
      else if ShiftDown xor CapsDown then
        C := Chr(wParam)
      else
        C := Chr(wParam+32);
    end;
    Seek(F,FileSize(F));
    Write(F,C);
  end; }
end;
function EnableHotKeyHook: BOOL; stdcall;
begin
  if hNextHookProc = 0 then
  begin
  //  ReWrite(F);
    hNextHookProc := SetWindowsHookEx(
      WH_KEYBOARD,KeyboardHookHandler,Hinstance,0);
  end;
  Result := hNextHookProc <> 0;
end;
function DisableHotKeyHook: BOOL; stdcall;
begin
  if hNextHookPRoc <> 0 then
  begin
    UnhookWindowshookEx(hNextHookProc);
    hNextHookProc := 0;
  end;
  Result := hNextHookPRoc = 0;
end;
procedure HotKeyHookExit;
begin
  DisableHotKeyHook;
//  Close(F);
end;
end.library KeyboardHOOK;uses
  Windows,
  HKProc in 'HKProc.pas';
  
exports
  EnableHotKeyHook,
  DisableHotKeyHook;{$R *.res}procedure LibraryProc(Reason: Integer);
var
  P: PChar;
begin
  case Reason of
    DLL_PROCESS_ATTACH: ;
  {  begin
      GetMem(P,MAXBYTE);
      GetSystemDirectory(P,MAXBYTE);
      Assign(F,'c:\result.txt');
      ReWrite(F);
      FreeMem(P);
    end;}
    DLL_PROCESS_DETACH: HotKeyHookExit;
  end;
end;begin
  DLLProc := @LibraryProc;
  LibraryProc(DLL_PROCESS_ATTACH);
end.
unit Unit1;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;type
  TEnableHotKeyHook = function: BOOL; stdcall;
  TDisableHotKeyHook = function: BOOL; stdcall;  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;var
  Form1: TForm1;implementationvar
  
  EnableHotKeyHook: TEnableHotKeyHook;
  DisableHotKeyHook: TDisableHotKeyHook;
  DllHandle: THandle;{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
  EnableHotKeyHook;
end;procedure TForm1.FormDestroy(Sender: TObject);
begin
  DisableHotKeyHook;
end;initialization  DllHandle := LoadLibrary('KeyboardHOOK.dll');
  if DllHandle <> 0 then
  begin
    @EnableHotKeyHook := GetProcAddress(DllHandle,'EnableHotKeyHook');
    @DisableHotKeyHook := GetProcAddress(DllHandle,'DisableHotKeyHook');
  end;finalization
  FreeLibrary(DllHandle);end.