J
JWXLI
Unregistered / Unconfirmed
GUEST, unregistred user!
我想编写一个键盘钩子,日的是取得系统内任何键盘输入,<br>但是总是只能得到本线程内的键盘输入,不知为什么。(注明:我已将钩子函数放入动态链接<br><br>库中),请指点,高分回报。<br>程序源码如下:<br>动态库程序:<br>library test;<br>uses<br> SysUtils,<br> Classes,<br> windows,<br> messages,<br> hookproc2 in 'hookproc2.pas';<br><br> exports<br> setkeyhook,<br> unkeyhook;<br><br>end.<br><br>unit hookproc2;<br><br>interface<br> uses<br> windows,messages;<br><br> var<br> hook:Thandle;<br> keyfile:textfile;<br> hinst:thandle;<br><br> function <br><br>prohook(icode:integer;wparam:wparam;lparam:lparam):lresult;stdcall;export;<br> function setkeyhook:bool;export;<br> function unkeyhook:bool;export;<br>implementation<br><br>function prohook(icode:integer;wparam:wparam;lparam:lparam):lresult;stdcall;<br>begin<br> if icode < 0 then<br> result:= callnexthookex(hook,icode,wparam,lparam)<br> else<br> begin<br> if icode= HC_ACTION then<br> writeln(keyfile,chr(wparam));<br> end;<br>end;<br><br>function setkeyhook:bool;export;<br> begin<br> try<br> hinst:=getModuleHandle('test.dll');<br> {得到test.dll的模块局柄,即该动态连接库自己的模块局柄}<br> assignfile(keyfile,'日志.txt');<br> rewrite(keyfile);<br> hook:=setwindowshookex(WH_KEYBOARD,prohook,hinst,0);//HInstance<br> result:=true;<br> except<br> result:=false;<br> end;<br>end;<br><br>function unkeyhook:bool;export;<br>begin<br> try<br> UnHookWindowsHookEx(Hook);<br> closefile(keyfile);<br> hook:=0;<br> result:=true;<br> except<br> result:=false;<br> end;<br>end;<br>end.<br><br>主程序<br>unit main;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> StdCtrls,shellapi, Menus;<br><br>type<br> TForm1 = class(TForm)<br> Button1: TButton;<br> Button2: TButton;<br> Button3: TButton;<br> Button4: TButton;<br> Memo1: TMemo;<br> PopupMenu1: TPopupMenu;<br> N1: TMenuItem;<br> N2: TMenuItem;<br> N3: TMenuItem;<br> N4: TMenuItem;<br> procedure Button1Click(Sender: TObject);<br> procedure Button2Click(Sender: TObject);<br> procedure Button3Click(Sender: TObject);<br> procedure Button4Click(Sender: TObject);<br> procedure N1Click(Sender: TObject);<br> procedure N2Click(Sender: TObject);<br> procedure N3Click(Sender: TObject);<br> procedure N4Click(Sender: TObject);<br> private<br> procedure wmuser(var message:Tmessage);message WM_USER;<br> procedure wmsyscommand(VAR message:Tmessage);message WM_SYSCOMMAND;<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br> function setkeyhook:bool;external'test.dll' ;<br> function unkeyhook:bool;external'test.dll';<br>var<br> Form1: TForm1;<br> TID:TNotifyIconDataA;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> if setkeyhook then<br> begin<br> button2.Enabled:=true;<br> button3.Enabled :=false;<br> button1.Enabled :=false;<br> end;<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br> if unkeyhook then<br> begin<br> button2.Enabled :=false;<br> button1.Enabled :=true;<br> button3.Enabled :=true;<br> end;<br>end;<br><br>procedure TForm1.Button3Click(Sender: TObject);<br>var<br> keyfile:textfile;<br> s:string;<br>begin<br> assignfile(keyfile,'日志.txt');<br> reset(keyfile);<br> while not Eof(keyfile) do<br> begin<br> readln(keyfile,s);<br> memo1.Lines.add(s);<br> end;<br><br>end;<br><br>procedure TForm1.Button4Click(Sender: TObject);<br>begin<br> close;<br>end;<br><br>procedure TForm1.WMSysCommand(var Message: TMessage);<br>const st='键盘信息' ;<br>begin<br> if message.wparam =SC_ICON then<br> begin<br> tid.cbSize := sizeof (tid);<br> tid.Wnd := handle;<br> tid.uID := 1;<br> tid.uFlags := nif_Message or nif_Icon or nif_Tip;<br> tid.uCallBackMessage := WM_USER;<br> tid.hIcon := LoadIcon (hInstance, 'MAINICON');<br> lstrcpy (tid.szTip,st);<br> Shell_NotifyIcon (nim_Add,@tid);<br> form1.visible:=false;<br> end<br> else<br> begin<br> DefWindowProc(Form1.Handle,Message.Msg,Message.WParam,Message.LParam);<br> end;<br>end;<br><br>procedure TForm1.wmuser(var message: Tmessage);<br>var<br> pt:tpoint;<br>begin<br> case Message.lparam of<br> WM_LBUTTONDBLCLK:<br> begin<br> form1.Visible :=true;<br> Shell_NotifyIcon(Nim_Delete,@tid);<br> end;<br> WM_RBUTTONDOWN:<br> begin<br> GetCursorPos (pt);<br> SetForegroundWindow (form1.handle);<br> PopupMenu1.Popup(pt.x,pt.y);<br> end;<br> WM_DESTROY:<br> begin<br> Shell_NotifyIcon(Nim_Delete,@tid);<br> halt;<br> end;<br> end;<br>end;<br><br><br>procedure TForm1.N1Click(Sender: TObject);<br>begin<br>button1click(self);<br>end;<br><br>procedure TForm1.N2Click(Sender: TObject);<br>begin<br> button2click(self);<br>end;<br><br>procedure TForm1.N3Click(Sender: TObject);<br>begin<br> form1.Visible :=true;<br>end;<br><br>procedure TForm1.N4Click(Sender: TObject);<br>begin<br> close;<br>end;<br><br>end.<br><br>