【笨嘴拙舌WINDOWS】实践检验之按键精灵【Delphi】
通过记录键盘和鼠标位置和输入信息,然后模拟发送,就能够创建一个按键精灵!
主要代码如下:
1 library KeyBoardHook; 2 3 { Important note about DLL memory management: ShareMem must be the 4 first unit in your library's USES clause AND your project's (select 5 Project-View Source) USES clause if your DLL exports any procedures or 6 functions that pass strings as parameters or function results. This 7 applies to all strings passed to and from your DLL--even those that 8 are nested in records and classes. ShareMem is the interface unit to 9 the BORLNDMM.DLL shared memory manager, which must be deployed along10 with your DLL. To avoid using BORLNDMM.DLL, pass string information11 using PChar or ShortString parameters. }12 13 uses14 SysUtils,15 Classes,16 Windows,17 Messages;18 19 type20 TCallBackFun=procedure(info:PChar);21 TKeyBoardHook=record22 isrun:Bool;23 hook:HHook;24 callBackFun:TCallBackFun;25 end;26 27 var28 myKeyBoardHook:TKeyBoardHook;29 {$R *.res}30 31 function GetKeyBoardInfo(code:Integer;wp:WPARAM;lp:LPARAM):LRESULT;stdcall;32 var33 info:string;34 begin35 if code
以上是创建一个全局钩子函数的Dll来记录按键信息
library Mousehook;{ Important note about DLL memory management: ShareMem must be the first unit in your library's USES clause AND your project's (select Project-View Source) USES clause if your DLL exports any procedures or functions that pass strings as parameters or function results. This applies to all strings passed to and from your DLL--even those that are nested in records and classes. ShareMem is the interface unit to the BORLNDMM.DLL shared memory manager, which must be deployed along with your DLL. To avoid using BORLNDMM.DLL, pass string information using PChar or ShortString parameters. }uses SysUtils, Classes, Windows, Messages, ShellAPI; type TCallbackFun=procedure(info:pchar); TMouseHook=record isrun:Bool; hook:HHook; callbackFun:TCallbackFun; end;var myMouseHook:TMouseHook;{$R *.res}//1.定义自定义的HOOK函数,函数必须和需要HOOK的钩子类型保持同样的参数列表function GetHookInfo(code:Integer;wp:WPARAM;lp:LPARAM):LResult;stdcall;var info:String;begin if code
以上是捕获鼠标消息的全局钩子DLL
使用一个新的线程来模拟发送消息
procedure TPlayThread.Execute;var directive:string; i:integer; ForgroundForm:TForm; procedure ExecuteDir(directive:string); var tempList:TStringList; Wp,Lp:integer; wmtype:String; focusControl:string; duration:Cardinal; winCtl:TWinControl; tempHandle,focusHandle:THandle; classname:String; mousPoint:TPOINT; procedure findFocus; var temp:TWinControl; finded:Boolean; begin if ((wmtype='WM_MOUSEMOVE') or (wmtype='WM_NCMouseMove')) then Exit; winCtl:=TWinControl(ForgroundForm.FindChildControl(focusControl)); if winCtlnil then begin focusHandle:= winCtl.Handle; AttachThreadInput(GetWindowThreadProcessId(ForgroundForm.Handle,nil),Self.ThreadID,True); Ferrorinfo:=SysErrorMessage(GetLastError); winCtl.SetFocus; AttachThreadInput(GetWindowThreadProcessId(ForgroundForm.Handle,nil),Self.ThreadID,False); Ferrorinfo:=SysErrorMessage(GetLastError); Exit; end; temp:=nil; finded:=False; while not finded do begin GetCursorPos(mousPoint); tempHandle := WindowFromPoint(mousPoint); if tempHandle =0 then begin Sleep(0); Continue; end; temp:=FindControl(tempHandle); if temp=nil then begin Sleep(0); Continue; end; if (temp.Name = focusControl) or (classname=temp.ClassName) then finded:=True; end; focusHandle := temp.Handle; AttachThreadInput(GetWindowThreadProcessId(ForgroundForm.Handle,nil),Self.ThreadID,True); Ferrorinfo:=SysErrorMessage(GetLastError); temp.SetFocus; AttachThreadInput(GetWindowThreadProcessId(ForgroundForm.Handle,nil),Self.ThreadID,False); Ferrorinfo:=SysErrorMessage(GetLastError); end; begin tempList:=TStringList.Create; try tempList.CommaText:=directive; tempList.Delimiter:=','; wmtype:=tempList[0]; focusHandle:=0; Wp:=StrToIntDef(tempList[1],0); //wParam Lp:=StrToIntDef(tempList[2],0); //Lparam duration:= StrToIntDef(tempList[3],0); if (duration=0) and (wmtype='WM_NCMouseMove') then Exit; //小于线程调度时间片的话就不延时---以免 sleep(0)直接放弃时间进入内核态 if (wmtype='') or (tempList.Count
点击这里下载代码
分类: WINDOWS地基
绿色通道:好文要顶关注我收藏该文与我联系
Pavkoo
关注 - 1
粉丝 - 7
+加关注
0
0
(请您对文章做出评价)
? 上一篇:【笨嘴拙舌WINDOWS】键盘消息,鼠标消息
? 下一篇:【笨嘴拙舌WINDOWS】计时器精度
posted @ 2013-09-12 11:48 Pavkoo 阅读(95) 评论(0) 编辑 收藏
刷新评论刷新页面返回顶部
注册用户登录后才能发表评论,请 登录 或 注册,访问网站首页。
博客园首页博问新闻闪存程序员招聘知识库
最新IT新闻:
· 看互联巨头table的投资万象
· GoPro上线视频交易平台,要开始在内容上挣点钱了
· eBay分拆PayPal背后有什么故事
· 京东的麻烦:成长烦恼or中年危机?
· 如何建立企业文化,看前Facebook高管给创业者提供的几点干货
? 更多新闻...
最新知识库文章:
· RESTful架构详解
· 优秀程序员眼中的整洁代码
· 怎样看待比自己强的人
· 编程王道,唯“慢”不破
· 程序员要有持续产出
? 更多知识库文章...
历史上的今天:
2010-09-12 Asp.net高级程序设计之WEB窗体(3)
公告
昵称:Pavkoo
园龄:5年2个月
粉丝:7
关注:1+加关注
日一二三四五六
2829301234
567891011
[1**********]718
[1**********]425
[1**********]11
2345678
搜索
【笨嘴拙舌WINDOWS】实践检验之按键精灵【Delphi】
通过记录键盘和鼠标位置和输入信息,然后模拟发送,就能够创建一个按键精灵!
主要代码如下:
1 library KeyBoardHook; 2 3 { Important note about DLL memory management: ShareMem must be the 4 first unit in your library's USES clause AND your project's (select 5 Project-View Source) USES clause if your DLL exports any procedures or 6 functions that pass strings as parameters or function results. This 7 applies to all strings passed to and from your DLL--even those that 8 are nested in records and classes. ShareMem is the interface unit to 9 the BORLNDMM.DLL shared memory manager, which must be deployed along10 with your DLL. To avoid using BORLNDMM.DLL, pass string information11 using PChar or ShortString parameters. }12 13 uses14 SysUtils,15 Classes,16 Windows,17 Messages;18 19 type20 TCallBackFun=procedure(info:PChar);21 TKeyBoardHook=record22 isrun:Bool;23 hook:HHook;24 callBackFun:TCallBackFun;25 end;26 27 var28 myKeyBoardHook:TKeyBoardHook;29 {$R *.res}30 31 function GetKeyBoardInfo(code:Integer;wp:WPARAM;lp:LPARAM):LRESULT;stdcall;32 var33 info:string;34 begin35 if code
以上是创建一个全局钩子函数的Dll来记录按键信息
library Mousehook;{ Important note about DLL memory management: ShareMem must be the first unit in your library's USES clause AND your project's (select Project-View Source) USES clause if your DLL exports any procedures or functions that pass strings as parameters or function results. This applies to all strings passed to and from your DLL--even those that are nested in records and classes. ShareMem is the interface unit to the BORLNDMM.DLL shared memory manager, which must be deployed along with your DLL. To avoid using BORLNDMM.DLL, pass string information using PChar or ShortString parameters. }uses SysUtils, Classes, Windows, Messages, ShellAPI; type TCallbackFun=procedure(info:pchar); TMouseHook=record isrun:Bool; hook:HHook; callbackFun:TCallbackFun; end;var myMouseHook:TMouseHook;{$R *.res}//1.定义自定义的HOOK函数,函数必须和需要HOOK的钩子类型保持同样的参数列表function GetHookInfo(code:Integer;wp:WPARAM;lp:LPARAM):LResult;stdcall;var info:String;begin if code
以上是捕获鼠标消息的全局钩子DLL
使用一个新的线程来模拟发送消息
procedure TPlayThread.Execute;var directive:string; i:integer; ForgroundForm:TForm; procedure ExecuteDir(directive:string); var tempList:TStringList; Wp,Lp:integer; wmtype:String; focusControl:string; duration:Cardinal; winCtl:TWinControl; tempHandle,focusHandle:THandle; classname:String; mousPoint:TPOINT; procedure findFocus; var temp:TWinControl; finded:Boolean; begin if ((wmtype='WM_MOUSEMOVE') or (wmtype='WM_NCMouseMove')) then Exit; winCtl:=TWinControl(ForgroundForm.FindChildControl(focusControl)); if winCtlnil then begin focusHandle:= winCtl.Handle; AttachThreadInput(GetWindowThreadProcessId(ForgroundForm.Handle,nil),Self.ThreadID,True); Ferrorinfo:=SysErrorMessage(GetLastError); winCtl.SetFocus; AttachThreadInput(GetWindowThreadProcessId(ForgroundForm.Handle,nil),Self.ThreadID,False); Ferrorinfo:=SysErrorMessage(GetLastError); Exit; end; temp:=nil; finded:=False; while not finded do begin GetCursorPos(mousPoint); tempHandle := WindowFromPoint(mousPoint); if tempHandle =0 then begin Sleep(0); Continue; end; temp:=FindControl(tempHandle); if temp=nil then begin Sleep(0); Continue; end; if (temp.Name = focusControl) or (classname=temp.ClassName) then finded:=True; end; focusHandle := temp.Handle; AttachThreadInput(GetWindowThreadProcessId(ForgroundForm.Handle,nil),Self.ThreadID,True); Ferrorinfo:=SysErrorMessage(GetLastError); temp.SetFocus; AttachThreadInput(GetWindowThreadProcessId(ForgroundForm.Handle,nil),Self.ThreadID,False); Ferrorinfo:=SysErrorMessage(GetLastError); end; begin tempList:=TStringList.Create; try tempList.CommaText:=directive; tempList.Delimiter:=','; wmtype:=tempList[0]; focusHandle:=0; Wp:=StrToIntDef(tempList[1],0); //wParam Lp:=StrToIntDef(tempList[2],0); //Lparam duration:= StrToIntDef(tempList[3],0); if (duration=0) and (wmtype='WM_NCMouseMove') then Exit; //小于线程调度时间片的话就不延时---以免 sleep(0)直接放弃时间进入内核态 if (wmtype='') or (tempList.Count
点击这里下载代码
分类: WINDOWS地基
绿色通道:好文要顶关注我收藏该文与我联系
Pavkoo
关注 - 1
粉丝 - 7
+加关注
0
0
(请您对文章做出评价)
? 上一篇:【笨嘴拙舌WINDOWS】键盘消息,鼠标消息
? 下一篇:【笨嘴拙舌WINDOWS】计时器精度
posted @ 2013-09-12 11:48 Pavkoo 阅读(95) 评论(0) 编辑 收藏
刷新评论刷新页面返回顶部
注册用户登录后才能发表评论,请 登录 或 注册,访问网站首页。
博客园首页博问新闻闪存程序员招聘知识库
最新IT新闻:
· 看互联巨头table的投资万象
· GoPro上线视频交易平台,要开始在内容上挣点钱了
· eBay分拆PayPal背后有什么故事
· 京东的麻烦:成长烦恼or中年危机?
· 如何建立企业文化,看前Facebook高管给创业者提供的几点干货
? 更多新闻...
最新知识库文章:
· RESTful架构详解
· 优秀程序员眼中的整洁代码
· 怎样看待比自己强的人
· 编程王道,唯“慢”不破
· 程序员要有持续产出
? 更多知识库文章...
历史上的今天:
2010-09-12 Asp.net高级程序设计之WEB窗体(3)
公告
昵称:Pavkoo
园龄:5年2个月
粉丝:7
关注:1+加关注
日一二三四五六
2829301234
567891011
[1**********]718
[1**********]425
[1**********]11
2345678
搜索