如何得到按鍵的訊息?(200分)

  • 主题发起人 主题发起人 nickmatch99
  • 开始时间 开始时间
N

nickmatch99

Unregistered / Unconfirmed
GUEST, unregistred user!
首先我有一張forma<br>按了button之後就會等待一組按鍵的輸入,和那時window handle中程式<br>之後收細<br>再來...<br>當我在另一個程式(window,例如我的文件)<br>按下我之前在forma中設定的那一組按鍵(例如:ctrl+4,alt+tab,alt+ctrl+3等...)<br>那麼我在forma中應該用什麼東西去接收那一組按鍵和那一個視窗?<br>請詳細說明(因為我找了很多貼子都只是用WindowFromPoint,sendmessage等...)<br>真正沒有教如何用...希望指教一下
 
键盘钩子可以了,可以得到你的按键是什么!
 
键盘钩子?<br>要用什麼東西才可以接收到?
 
你看看这点资料。<br>一、Hook(钩子)的实现:<br>Hook是应用程序在Microsoft Windows 消息处理过程中设置的用来监控消息流并且<br>处理系统中尚未到达目的窗口的某一类型消息过程的机制。如果Hook过程在应用程<br>序中实现,若应用程序不是当前窗口时,该Hook就不起作用;如果Hook在DLL中实现,<br>程序在运行中动态调用它,它能实时对系统进行监控。根据需要,我们采用的是在DLL<br>中实现Hook的方式。<br><br>1.新建一个导出两个函数的DLL文件,在hookproc.pas中定义了钩子具体实现过<br>程。代码如下:<br>library keyspy;<br>uses<br>windows, messages, hookproc in 'hookproc.pas';<br>exports<br>setkeyhook,<br>endkeyhook;<br>begin<br>nexthookproc:=0;<br>procsaveexit:=exitproc;<br>exitproc:=@keyhookexit;<br>end.<br><br>2.在Hookproc.pas中实现了钩子具体过程:<br>unit hookproc;<br>interface<br>uses<br>Windows, Messages, SysUtils, Controls, StdCtrls;<br>var<br>nexthookproc:hhook;<br>procsaveexit:pointer;<br>function keyboardhook(icode:integer;wparam:wparam;<br>lparam:lparam):lresult;stdcall;export;<br>function setkeyhook:bool;export;//加载钩子<br>function endkeyhook:bool;export;//卸载钩子<br>procedure keyhookexit;far;<br>const<br>afilename='c:/debug.txt';//将键盘输入动作写入文件中<br>var<br>debugfile:textfile;<br>implementation<br>function keyboardhookhandler(icode:integer;wparam:wparam;<br>lparam:lparam):lresult;stdcall;export;<br>begin<br>if icode&lt;0 then<br>begin<br>result:=callnexthookex(hnexthookproc,icode,wparam,lparam);<br>exit;<br>end;<br>assignfile(debugfile,afilename);<br>append(debugfile);<br>if getkeystate(vk_return)&lt;0 then<br>begin<br>writeln(debugfile,'');<br>write(debugfile,char(wparam));<br>end<br>else<br>write(debugfile,char(wparam));<br>closefile(debugfile);<br>result:=0;<br>end;<br>function endkeyhook:bool;export;<br>begin<br>if nexthookproc&lt;&gt;0 then begin<br>unhookwindowshookex(nexthookproc);<br>nexthookproc:=0;<br>messagebeep(0); end;<br>result:=hnexthookproc=0;<br>end;<br>procedure keyhookexit;far;<br>begin<br>if nexthookproc&lt;&gt;0 then endkeyhook;<br>exitproc:=procsaveexit; end;<br>end.
 
to zywcd<br>請恕小弟很菜...<br>dll和pas要如何使用...<br>小弟只慬得用form(即是delphi開始的那個...)<br>去寫一些code<br>但...我應該如何用那一張from去call我所寫的dll和pas<br>希望再為我解答一下
 
关于dll的调用,建议你找本delphi的书来看看,一般都有如何调用一个dll的.
 
那麼等我研究一下<br>有問題再在這裡問...
 
唔好意思...<br>我真的不明白如何可以得到dll檔案= =<br>可能我用那本書比較...菜吧= =
 
需要別人的幫助...<br>dll建立不成...= =<br>需要增援!!
 
還不慬得如果寫hook= =
 
放个Action大部分键也可以了.
 
来自:LanderLiu, 时间:2006-10-24 15:20:46, ID:3604856<br>放个Action大部分键也可以了. &nbsp;<br>不是太明白...請再詳細講解...
 
OnCreate事件中处理:Application.OnMessage:=MyOnMessage; <br>procedure TForm1.MyOnMessage(var MSG:TMSG;var Handle:Boolean); <br>begin <br>if msg.message=256 then ... //ANY键 <br>if msg.message=112 then ... //F1 <br>if msg.message=113 then ... //F2 <br>end;
 
关于键盘常量名VK_BACK/VK_TAB/VK_RETURN/VK_SHIFT/VK_CONTROL/VK_MENU/VK_PAUSE/VK_ESCAPE <br>/VK_SPACE/VK_LEFT/VK_RIGHT/VK_UP/VK_DOWN <br>F1--F12:$70(112)--$7B(123) <br>A-Z:$41(65)--$5A(90) <br>0-9:$30(48)--$39(57)
 
网络上大把鼠标键盘钩子的代码
 
不好意思,我的帐号还不能提问,在这里问个差不多的问题<br>我要做一个(热血江湖)会加自动加血加蓝的外挂,<br>具体代码如下:<br>unit Mainf;<br><br>interface<br><br>uses<br> &nbsp;Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> &nbsp;Dialogs, ComCtrls,StdCtrls, ExtCtrls;<br><br>type<br> &nbsp;TMainForm = class(TForm)<br> &nbsp; &nbsp;TimerSM: TTimer;<br> &nbsp; &nbsp;TimerNG: TTimer;<br> &nbsp; &nbsp;TimerJN: TTimer;<br> &nbsp; &nbsp;CheckBoxSM: TCheckBox;<br> &nbsp; &nbsp;CheckBoxNG: TCheckBox;<br> &nbsp; &nbsp;EditSM: TEdit;<br> &nbsp; &nbsp;EditNG: TEdit;<br> &nbsp; &nbsp;CheckBoxJN: TCheckBox;<br> &nbsp; &nbsp;Button1: TButton;<br> &nbsp; &nbsp;Button2: TButton;<br> &nbsp; &nbsp;Label1:TLabel;<br> &nbsp; &nbsp;procedure CheckBoxSMClick(Sender: TObject);<br> &nbsp; &nbsp;procedure TimerSMTimer(Sender: TObject);<br> &nbsp; &nbsp;procedure Button2Click(Sender: TObject);<br> &nbsp; &nbsp;procedure Button1Click(Sender: TObject);<br> &nbsp; &nbsp;procedure CheckBoxNGClick(Sender: TObject);<br> &nbsp; &nbsp;procedure TimerNGTimer(Sender: TObject);<br> &nbsp; &nbsp;procedure CheckBoxJNClick(Sender: TObject);<br> &nbsp; &nbsp;procedure TimerJNTimer(Sender: TObject);<br> &nbsp;private<br> &nbsp; &nbsp;{ Private declarations }<br> &nbsp;public<br> &nbsp; &nbsp;{ Public declarations }<br> &nbsp;end;<br>const<br> &nbsp;SHENGMING: dword = $01453318; //红<br> &nbsp;NEIGONG: dword = $0145331C; // 蓝<br><br>var<br> &nbsp;MainForm: TMainForm;<br><br> &nbsp;hw: HWND;<br> &nbsp;pid: dword;//游戏句柄ID<br> &nbsp;h: THandle;//游戏句柄<br> &nbsp;tt: Cardinal;<br> &nbsp;procedure KeyF2;//按键<br> &nbsp;procedure KeyF3;<br> &nbsp;procedure KeyF4;<br><br>implementation<br><br>{$R *.dfm}<br>procedure TMainForm.Button1Click(Sender: TObject);<br>begin<br> &nbsp;if &nbsp;h = 0 then //如果句柄没打开<br> &nbsp; begin<br> &nbsp; &nbsp;hw:=FindWindow(nil,'YB_OnlineClient'); &nbsp; &nbsp;///<br> &nbsp; &nbsp;if hw = 0 then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;/// &nbsp; &nbsp; &nbsp; 获得句柄ID<br> &nbsp; &nbsp; &nbsp;Exit; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;///<br> &nbsp; &nbsp;GetWindowThreadProcessId(hw, @pid); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;///<br> &nbsp; &nbsp;h := OpenProcess(PROCESS_ALL_ACCESS, false, pid); &nbsp; &nbsp;///<br> &nbsp; &nbsp;if h = 0 then &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;///打开句柄<br> &nbsp; &nbsp; &nbsp;Exit;<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;///<br> &nbsp; end;<br> &nbsp;TimerNG.Interval := 1000 div trunc(5);<br> &nbsp;TimerSM.Interval := 1000 div trunc(5);<br> &nbsp;TimerJN.Interval := 1000 div trunc(2);///每秒钟运行次数<br>end;<br>procedure TMainForm.CheckBoxSMClick(Sender: TObject);<br>begin<br> &nbsp; if CheckBoxSM.Checked then &nbsp;//控制Timer是否运行<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; EditSM.Enabled := false;<br> &nbsp; &nbsp; TimerSM.Enabled := true;<br> &nbsp; &nbsp;end<br> &nbsp; else<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; EditSM.Enabled := true;<br> &nbsp; &nbsp; TimerSM.Enabled := false;<br> &nbsp;end;<br>end;<br><br>procedure TMainForm.TimerSMTimer(Sender: TObject);<br>var<br> &nbsp;Sm: integer;<br> // tt: DWORD;<br>begin<br> &nbsp;ReadProcessMemory(h,pointer( SHENGMING), @Sm,sizeof(Sm), tt);<br>//扫描内存数据<br> &nbsp;<br> &nbsp;if ((Sm &lt;= StrToInt(EditSM.Text))and(Sm&lt;&gt;0)) and (h&lt;&gt;0) then<br>//如果血量值少于设定值并且不等于0<br> &nbsp; &nbsp; begin<br> &nbsp; &nbsp; &nbsp; &nbsp;KeyF3;<br> &nbsp; &nbsp; //按F3键<br> &nbsp; &nbsp; Label1.Caption :='F3运行中';<br> &nbsp; &nbsp; end;<br><br>end;<br><br>procedure KeyF2;<br>begin<br> &nbsp; keybd_event(VK_F2,mapvirtualkey(VK_F2,0),0,0);<br> &nbsp; keybd_event(VK_F2,mapvirtualkey(VK_F2,0),keyeventf_keyup,0);//按F2键<br>end;<br>procedure KeyF3;<br>begin<br> &nbsp; keybd_event(VK_F3,mapvirtualkey(VK_F3,0),0,0);<br> &nbsp; keybd_event(VK_F3,mapvirtualkey(VK_F3,0),keyeventf_keyup,0);//按F3键<br>end;<br>procedure KeyF4;<br>begin<br> &nbsp; keybd_event(VK_F4,mapvirtualkey(VK_F4,0),0,0);<br> &nbsp; keybd_event(VK_F4,mapvirtualkey(VK_F4,0),keyeventf_keyup,0);//按F4键<br>end;<br>procedure TMainForm.Button2Click(Sender: TObject);<br>begin<br> &nbsp;if h &lt;&gt; 0 then<br> &nbsp; begin<br> &nbsp; &nbsp;MessageBeep(0);<br> &nbsp; &nbsp;CloseHandle(h);<br> &nbsp; end;<br> &nbsp; close;<br>end;<br>end.<br><br>没有进游戏时,Label没有反应,<br>进入游戏了,按下按钮,当血量少于设定值时Label改变,说明Timer在运行,<br>可是游戏里不会自动加血,于是我改变了一下按键,把F3改成了F4,<br>游戏里还是没有反应,但我切到外挂窗口,按一下Alt键,外挂窗口关闭,<br>由此证明模拟键盘F4键一直进行中<br>后来我又换了一个模拟键盘的方法,<br>改变如下:<br>procedure KeyF3;<br>begin<br> &nbsp; PostMessage(Application.Handle, WM_KEYDOWN, VK_F4, 0);<br> &nbsp; PostMessage(Application.Handle, WM_KEYUP, VK_F4, 0);<br>end;<br>和<br>procedure KeyF3;<br>begin<br> &nbsp; PostMessage(h, WM_KEYDOWN, VK_F4, 0);<br> &nbsp; PostMessage(h, WM_KEYUP, VK_F4, 0);<br>end;<br>在游戏里还是没有反应,并且外挂窗口也没有反应了,但是Label标签改变<br>由是我再改成<br>procedure KeyF3;<br>begin<br> &nbsp; PostMessage(h, WM_KEYDOWN, VK_F4, 0);<br> &nbsp; PostMessage(h, WM_KEYUP, VK_F4, 0);<br> &nbsp; setcursorpos(500,400);<br> &nbsp; mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);<br> &nbsp; mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);<br>end;<br>这时鼠标在游戏里点击正常,键盘没有反应<br><br>在此麻烦大侠帮下忙,找下问题所在,<br>或再给几个模拟键盘的方法<br>非常感谢!!!!!
 
都沒有人教我如何建立dll...悶~[:(]
 
算了..搞不成...<br>結結發分!
 
多人接受答案了。
 

Similar threads

后退
顶部