刚学习写钩子程序,写了一个简单的判断TAB键按下的钩子函数
function myHookProc(nCode:integer;wParam:wParam;lParam:lParam):LResult;
begin
showmessage('start');
if nCode<0 then
begin
result:=CallNextHookEx(myHook,nCode,wParam,lParam);
end;
if wParam = VK_TAB then
showmessage('aaaa');
result:=CallNextHookEx(myHook,nCode,wParam,lParam);
end;
下面是开启钩子函数
procedure TForm1.Button1Click(Sender: TObject);
begin
myHook:=setwindowshookex(WH_JOURNALRECORD,@myHookProc,Hinstance,GetCurrentThreadID);
end;
取消钩子函数
procedure TForm1.Button2Click(Sender: TObject);
begin
if myHook<>0 then
begin
unhookwindowshookex(myHook);
myHook:=0;
end;
end;请问问题出在哪儿??谢谢~
function myHookProc(nCode:integer;wParam:wParam;lParam:lParam):LResult;
begin
showmessage('start');
if nCode<0 then
begin
result:=CallNextHookEx(myHook,nCode,wParam,lParam);
end;
if wParam = VK_TAB then
showmessage('aaaa');
result:=CallNextHookEx(myHook,nCode,wParam,lParam);
end;
下面是开启钩子函数
procedure TForm1.Button1Click(Sender: TObject);
begin
myHook:=setwindowshookex(WH_JOURNALRECORD,@myHookProc,Hinstance,GetCurrentThreadID);
end;
取消钩子函数
procedure TForm1.Button2Click(Sender: TObject);
begin
if myHook<>0 then
begin
unhookwindowshookex(myHook);
myHook:=0;
end;
end;请问问题出在哪儿??谢谢~
解决方案 »
- 疯了,Label怎么对连续的数字、英文自动换行啊啊
- Fastreprot 怎么汉化 设计器?
- 有趣的现象,showmessage(floattostr(int(strtofloat('2.1')*100)))结果是209,不知道有没有人知道原因?
- 跪求一个在SQL2000中存储图片地址和DELPHI读取图片的例子!
- 一只刚刚飞入Delphi的菜鸟
- 请问谁用的是正版的Delphi7.0?
- 请各位高手指教!
- 如何实现将灰度图像转换伪彩色图像????
- rs485校验和怎么计算
- 请问一个Delphi 中 ADO 参数的小问题!请大家多多观照!谢谢!
- 小弟的迷茫、疑惑?
- 高考失敗了,又不能再等一年,慘啊
begin
KeyValue:=pEventMSG(lParam)^.paramL mod 256;
if Keyvalue=9 then
showmessage('tab');
end;
没错误,但是没效果~
可以肯定的是我定义的挂钩函数没有被执行~
开始的showmessage都show不出来~
myHook:=setwindowshookex(WH_JOURNALRECORD,@myHookProc,Hinstance,GetCurrentThreadID);
改成
myHook:=setwindowshookex(WH_JOURNALRECORD,@myHookProc,Hinstance,0);
执行,show第一个start的时候,就出现错误,错误提示是:
系统错误码:120。此功能只被此系统支持~
Function KbHook( code: Integer; wparam: Word; lparam: LongInt ): LRESULT;stdcall;
oldHook定义为全局变量即在
var Form1:TForm1;
oldHook:=hHook;
implementation
.............
procedure TForm1.Button1Click(Sender: TObject);
begin
if oldHook=0 then
oldHook := SetWindowsHookEx( WH_JOURNALRECORD, @KbHook, HInstance, 0);end;
procedure TForm1.Button2Click(Sender: TObject);
begin
If oldHook <> 0 Then
Begin
UnhookWindowshookEx( oldHook );
oldHook := 0;
end;Function KbHook( code: Integer; wparam: Word; lparam: LongInt ): LRESULT;stdcall;
var KeyValue:UINT;
ks: TkeyBoardState;
Begin
showmessage('start');
result:=0;
If code < 0 Then
begin
result:= CallNextHookEx( oldHook, code, wparam, lparam );
end
Else begin
if pEventMSG(lParam)^.message=WM_KEYDOWN then
begin
KeyValue:=pEventMSG(lParam)^.paramL mod 256;
if Keyvalue=9 then
showmessage('tab');
end;
end;
end;
/////////////////////////////
再不行就不知道了,这个在我机上试过是可以的.
谢谢~~
我现在出的问题还是:
show第一个start的时候,就出现错误,错误提示是:
系统错误码:120。此功能只被此系统支持~我也不知道,请问:
KbHook定义为全局函数
Function KbHook( code: Integer; wparam: Word; lparam: LongInt ): LRESULT;stdcall;
函数声明是放在哪儿才算全局函数?
可能是我位置放错了~
谢谢了~
哪位兄弟能够帮我测试一下错在哪儿啊~
感激不尽:
unit Unit1;interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
myhook:HHook;implementation{$R *.dfm}
function myHookProc(nCode:integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;
var KeyValue:UINT;
ks: TkeyBoardState;
Begin
//showmessage('start');
result:=0;
If ncode < 0 Then
begin
result:= CallNextHookEx( myHook, ncode, wparam, lparam );
end
Else begin
if pEventMSG(lParam)^.message=WM_KEYDOWN then
begin
KeyValue:=pEventMSG(lParam)^.paramL mod 256;
if Keyvalue=9 then
showmessage('tab');
end;
end;end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if myhook = 0 then
myHook:=setwindowshookex(WH_JOURNALRECORD,@myHookProc,HInstance,0);
//myHook:=setwindowshookex(WH_KEYBOARD,@myHookProc,0,GetCurrentThreadId);
end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if myHook<>0 then
begin
unhookwindowshookex(myHook);
myHook:=0;
end;
end;end.
没办法,公司不用,我也快不会用dephi了...
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
function myHookProc(nCode:integer;wParam:wParam;lParam:lParam):LRESULT;stdcall;var
Form1: TForm1;
myhook:HHook;implementation{$R *.dfm}
function myHookProc(nCode:integer;wParam:wParam;lParam:lParam):LRESULT;
var KeyValue:UINT;
ks: TkeyBoardState;
Begin
result:=0;
If ncode < 0 Then
begin
result:= CallNextHookEx( myHook, ncode, wparam, lparam );
end
Else begin
if pEventMSG(lParam)^.message=WM_KEYDOWN then
begin
KeyValue:=pEventMSG(lParam)^.paramL mod 256;
if Keyvalue=9 then
Form1.Edit1.text:='OK';
end;
end;end;procedure TForm1.Button1Click(Sender: TObject);
begin
if myhook = 0 then
myHook:=setwindowshookex(WH_JOURNALRECORD,@myHookProc,HInstance,0);end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if myHook<>0 then
begin
unhookwindowshookex(myHook);
myHook:=0;
end;end;end.
谢谢了`
恩~