L
LiChaoHui
Unregistered / Unconfirmed
GUEST, unregistred user!
此单元被一个动态连接库包含,后面所附为调用的代码
unit UKBHook;
interface
uses
Windows, SysUtils;
var
htKeyboard: THandle = 0;
Filters: array[0..200] of Integer;
FilterCount: Integer = 0;
function StartFilter: Integer; stdcall;
function StopFilter: Integer; stdcall;
function SetFilter(Key: Integer): Integer; stdcall;
function ResetFilter: Integer; stdcall;
function KbdHookCallback(code: Integer; wp: WPARAM; lp: LPARAM): LRESULT; stdcall;
implementation
{ StartFilter }
function StartFilter: Integer;
begin
if htKeyboard = 0 then
begin
htKeyboard := SetWindowsHookEx(WH_KEYBOARD,
KbdHookCallback, HInstance, 0);
end;
FilterCount := 0;
Result := htKeyboard;
end;
{ StopFilter }
function StopFilter: Integer;
begin
if htKeyboard <> 0 then
begin
UnhookWindowsHookEx(htKeyboard);
htKeyboard := 0;
FilterCount := 0;
end;
Result := 1;
end;
{ SetFilter }
function SetFilter(Key: Integer): Integer; stdcall;
begin
if FilterCount < 200 then
begin
Filters[FilterCount] := Key;
FilterCount := FilterCount + 1;
Result := FilterCount;
end
else
Result := 0;
end;
{ ResetFilter }
function ResetFilter: Integer; stdcall;
begin
FilterCount := 0;
Result := 1;
end;
{ KbdHookCallback }
function KbdHookCallback(code: Integer; wp: WPARAM; lp: LPARAM): LRESULT;
var
i: Integer;
Found: Boolean;
begin
Found := False;
for i := 0 to FilterCount - 1 do
begin
if wp = Filters then
begin
Found := True;
Break;
end;
end;
//过滤并抛弃
if Found then
Result := 10
else
Result := CallNextHookEx(htKeyboard,code,wp,lp);
//MessageBox(0, PChar(IntToStr(Result)), '', 0);
end;
exports
StartFilter,
StopFilter,
SetFilter,
ResetFilter,
KbdHookCallback;
initialization
finalization
StopFilter;
end.
调用的代码
function StartFilter: Integer; stdcall; external 'KBFilter.dll';
function StopFilter: Integer; stdcall; external 'KBFilter.dll';
function SetFilter(Key: Integer): Integer; stdcall; external 'KBFilter.dll';
function ResetFilter: Integer; stdcall; external 'KBFilter.dll';
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
StartFilter;
SetFilter(VK_F1);
SetFilter(VK_F2);
SetFilter(VK_F5);
SetFilter(VK_F6);
ShowMessage('OK');
StopFilter;
end;
unit UKBHook;
interface
uses
Windows, SysUtils;
var
htKeyboard: THandle = 0;
Filters: array[0..200] of Integer;
FilterCount: Integer = 0;
function StartFilter: Integer; stdcall;
function StopFilter: Integer; stdcall;
function SetFilter(Key: Integer): Integer; stdcall;
function ResetFilter: Integer; stdcall;
function KbdHookCallback(code: Integer; wp: WPARAM; lp: LPARAM): LRESULT; stdcall;
implementation
{ StartFilter }
function StartFilter: Integer;
begin
if htKeyboard = 0 then
begin
htKeyboard := SetWindowsHookEx(WH_KEYBOARD,
KbdHookCallback, HInstance, 0);
end;
FilterCount := 0;
Result := htKeyboard;
end;
{ StopFilter }
function StopFilter: Integer;
begin
if htKeyboard <> 0 then
begin
UnhookWindowsHookEx(htKeyboard);
htKeyboard := 0;
FilterCount := 0;
end;
Result := 1;
end;
{ SetFilter }
function SetFilter(Key: Integer): Integer; stdcall;
begin
if FilterCount < 200 then
begin
Filters[FilterCount] := Key;
FilterCount := FilterCount + 1;
Result := FilterCount;
end
else
Result := 0;
end;
{ ResetFilter }
function ResetFilter: Integer; stdcall;
begin
FilterCount := 0;
Result := 1;
end;
{ KbdHookCallback }
function KbdHookCallback(code: Integer; wp: WPARAM; lp: LPARAM): LRESULT;
var
i: Integer;
Found: Boolean;
begin
Found := False;
for i := 0 to FilterCount - 1 do
begin
if wp = Filters then
begin
Found := True;
Break;
end;
end;
//过滤并抛弃
if Found then
Result := 10
else
Result := CallNextHookEx(htKeyboard,code,wp,lp);
//MessageBox(0, PChar(IntToStr(Result)), '', 0);
end;
exports
StartFilter,
StopFilter,
SetFilter,
ResetFilter,
KbdHookCallback;
initialization
finalization
StopFilter;
end.
调用的代码
function StartFilter: Integer; stdcall; external 'KBFilter.dll';
function StopFilter: Integer; stdcall; external 'KBFilter.dll';
function SetFilter(Key: Integer): Integer; stdcall; external 'KBFilter.dll';
function ResetFilter: Integer; stdcall; external 'KBFilter.dll';
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
StartFilter;
SetFilter(VK_F1);
SetFilter(VK_F2);
SetFilter(VK_F5);
SetFilter(VK_F6);
ShowMessage('OK');
StopFilter;
end;