如何用键盘钩子 KeyBoard HOOK 来拦截和屏蔽某些按键,这是我的代码,但是不能正常拦截!!(200分)

  • 主题发起人 主题发起人 LiChaoHui
  • 开始时间 开始时间
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;
 
if Found then
Result := -1;
试一下。
 
有没有兴趣用控件啊。
http://service.lonetear.com/delphi/dispdoc.asp?id=1328
对了,在你下包的时候看看下面的我的回复会好一些。
 
多谢楼上的兄弟,

你说的那些控件我早下载下来看过了,
可是好像也是不管用的,不知道为什么,
当窗口焦点在浏览器上时,按下 F1 总是出帮助,我也没办法了
不管用控件也好,API也好,只要能完全屏蔽 F1 - F6 这几个按键,分数就给他
还可以再加上100分,
我试了一下,用注册系统级热键的方法倒是可以屏蔽这些按键
但是,
用钩子就做不到吗?
 
to zw84611:

试了一下,对于 F1 还是不能阻止显示帮助
我的钩子回调函数,好像总是使 Found 为 False
好像我的过滤表不起作用似的
 
http://www.iligia.com/dev/pub/download/nthotkey/ligia_nthotkey_source.rar
 
来自:LiChaoHui, 时间:2003-4-3 22:12:00, ID:1736852
多谢楼上的兄弟,

>>可是好像也是不管用的,不知道为什么,
>>当窗口焦点在浏览器上时,按下 F1 总是出帮助,我也没办法了
>>不管用控件也好,API也好,只要能完全屏蔽 F1 - F6 这几个按键,分数就给他
>>用
我的意思是你在用那些控件的时候把那个Dll Project编译一下然后
再用就Ok了,我用过了没问题的。
 
这个我是知道的,能够拦截到,但是却不能屏蔽,
其他的一些动态连接库也是这样子的

多谢 zw84611 兄提供的源代码,我试了一下,是可以的
但是不知道我的代码为什么不行,原理和方法都差不多的
 
有必要细心研究一下了,落后了。
 
后退
顶部