例如在Test.Dll中: function CallWndHook(Code: Integer; WParam: wParam; Msg: PCWPStruct): Longint; stdcall; begin //你对键盘消息的处理... end;procedure TestHook; begin SetWindowsHookEx(WH_KEYBOARD,@CallWndHook,0,GetCurrentThreadID); end; 在某个进程中调用该DLL中的TestHook函数。
function CallWndHook(Code: Integer; WParam: wParam; Msg: PCWPStruct): Longint; stdcall; begin //你对键盘消息的处理... end;procedure TestHook; begin SetWindowsHookEx(WH_KEYBOARD,@CallWndHook,0,GetCurrentThreadID); end; 这两个程序,你可以写在你的单元里,看情况有没有必要写在dll文件里
可以使用低级键盘HOOK,用这种方法不需要用DLL就可以获得所有进程的消息 给你一个例子:unit LowLevelHook;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormClose(Sender: TObject; var Action: TCloseAction); private { Private declarations } public { Public declarations } end; TagKBDLLHookStruct = Packed Record Vkcode:Dword; Scancode:Dword; Flags:Dword; Time:Dword; DwExtraInfo:Dword; end; KBDllHookStruct = TagKBDllHookStruct; PKBDllHookStruct = ^KBDllHookStruct;const WH_KEYBOARD_LL =13; Const LLKHF_ALTDOWN=$20;var Form1: TForm1; WinHook:HHook; implementationFunction LowLevelKeyBoardProc(nCode:integer;Wparam:Wparam;Lparam:Lparam):LResult;StdCall; var F:Bool; P:PKBDllHOOKStruct; begin Result:=0; F:=False; P:=PkBDllHookStruct(Lparam); if (nCode=HC_ACTION) then begin Case Wparam of WM_KEYDOWN,WM_SYSKEYDOWN,WM_KEYUP,WM_SYSKEYUP: if (P.Vkcode in [65..90]) then F:=((GetKeyState(vk_capital)and $01)=0); end; end; IF F=True then begin showmessage(''); Result:=1; end; If nCode<>0 then Result:=CallNextHookEx(WinHook,Ncode,Wparam,Lparam); end;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject); begin If WinHook=0 then begin WinHook:=SetWindowsHookEx(WH_KEYBOARD_LL,LowLevelKeyBoardProc,Hinstance,0); If WinHook<>0 then messageBox(Handle,'低级键盘钩子设置成功','提示',MB_OK) Else MessageBox(Handle,'低级键盘钩子设置失败','提示',MB_OK); end else MessageBox(Handle,'低级键盘钩子已经设置','提示',MB_OK); end;procedure TForm1.Button2Click(Sender: TObject); begin If WinHook<>0 then begin if UnHookWindowsHookEx(WinHook)<>False then begin MessageBox(Handle,'低级键盘钩子成功卸载','提示',MB_OK); WinHook:=0; end else MessageBox(Handle,'低级键盘钩子卸载失败','提示',MB_OK) end Else MessageBox(Handle,'没有设置低级键盘钩子','提示',MB_OK);end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin If WinHook<>0 then UNHookWindowsHookEx(WinHook); end;end.
function CallWndHook(Code: Integer; WParam: wParam; Msg: PCWPStruct): Longint; stdcall;
begin
//你对键盘消息的处理...
end;procedure TestHook;
begin
SetWindowsHookEx(WH_KEYBOARD,@CallWndHook,0,GetCurrentThreadID);
end;
在某个进程中调用该DLL中的TestHook函数。
begin
//你对键盘消息的处理...
end;procedure TestHook;
begin
SetWindowsHookEx(WH_KEYBOARD,@CallWndHook,0,GetCurrentThreadID);
end;
这两个程序,你可以写在你的单元里,看情况有没有必要写在dll文件里
给你一个例子:unit LowLevelHook;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
TagKBDLLHookStruct = Packed Record
Vkcode:Dword;
Scancode:Dword;
Flags:Dword;
Time:Dword;
DwExtraInfo:Dword;
end;
KBDllHookStruct = TagKBDllHookStruct;
PKBDllHookStruct = ^KBDllHookStruct;const WH_KEYBOARD_LL =13;
Const LLKHF_ALTDOWN=$20;var
Form1: TForm1;
WinHook:HHook;
implementationFunction LowLevelKeyBoardProc(nCode:integer;Wparam:Wparam;Lparam:Lparam):LResult;StdCall;
var
F:Bool;
P:PKBDllHOOKStruct;
begin
Result:=0;
F:=False;
P:=PkBDllHookStruct(Lparam);
if (nCode=HC_ACTION) then
begin
Case Wparam of
WM_KEYDOWN,WM_SYSKEYDOWN,WM_KEYUP,WM_SYSKEYUP:
if (P.Vkcode in [65..90]) then
F:=((GetKeyState(vk_capital)and $01)=0);
end;
end;
IF F=True then
begin
showmessage('');
Result:=1;
end; If nCode<>0 then Result:=CallNextHookEx(WinHook,Ncode,Wparam,Lparam);
end;{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
If WinHook=0 then
begin
WinHook:=SetWindowsHookEx(WH_KEYBOARD_LL,LowLevelKeyBoardProc,Hinstance,0);
If WinHook<>0 then
messageBox(Handle,'低级键盘钩子设置成功','提示',MB_OK)
Else
MessageBox(Handle,'低级键盘钩子设置失败','提示',MB_OK); end
else
MessageBox(Handle,'低级键盘钩子已经设置','提示',MB_OK);
end;procedure TForm1.Button2Click(Sender: TObject);
begin
If WinHook<>0 then
begin
if UnHookWindowsHookEx(WinHook)<>False then
begin
MessageBox(Handle,'低级键盘钩子成功卸载','提示',MB_OK);
WinHook:=0;
end
else
MessageBox(Handle,'低级键盘钩子卸载失败','提示',MB_OK)
end
Else
MessageBox(Handle,'没有设置低级键盘钩子','提示',MB_OK);end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
If WinHook<>0 then UNHookWindowsHookEx(WinHook);
end;end.
1.WH_KEYBOARD_LL只能在NT/2000中使用,在Win98下必须用WH_KEYBOARD,此时Hook函数必须放在DLL文件中。2.B++ Builder中已有WH_KEYBOARD_LL和pKBDLLHookStruct的定义,但在Delphi就必须自已定义:
const WH_KEYBOARD_LL =13;
TagKBDLLHookStruct = Packed Record
Vkcode:Dword;
Scancode:Dword;
Flags:Dword;
Time:Dword;
DwExtraInfo:Dword;
end;
KBDllHookStruct = TagKBDllHookStruct;
PKBDllHookStruct = ^KBDllHookStruct;3.使用WH_KEYBOARD时,按键的码值就是键盘消息的wParam,使用WH_KEYBOARD_LL时,按键是通过将键盘消息的lParam强制类型转换成PKBDllHookStruct,再取出Vcode。