键盘钩子,高分回报(0分)

  • 主题发起人 主题发起人 JWXLI
  • 开始时间 开始时间
J

JWXLI

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

Similar threads

I
回复
0
查看
561
import
I
I
回复
0
查看
603
import
I
后退
顶部