通过记录键盘和鼠标位置和输入信息,然后模拟发送,就能够创建一个按键精灵!

主要代码如下:

  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 along
  10. with your DLL. To avoid using BORLNDMM.DLL, pass string information
  11. using PChar or ShortString parameters. }
  12.  
  13. uses
  14. SysUtils,
  15. Classes,
  16. Windows,
  17. Messages;
  18.  
  19. type
  20. TCallBackFun=procedure(info:PChar);
  21. TKeyBoardHook=record
  22. isrun:Bool;
  23. hook:HHook;
  24. callBackFun:TCallBackFun;
  25. end;
  26.  
  27. var
  28. myKeyBoardHook:TKeyBoardHook;
  29. {$R *.res}
  30.  
  31. function GetKeyBoardInfo(code:Integer;wp:WPARAM;lp:LPARAM):LRESULT;stdcall;
  32. var
  33. info:string;
  34. begin
  35. if code< then
  36. begin
  37. Result:=CallNextHookEx(myKeyBoardHook.hook,code,wp,lp);
  38. Exit;
  39. end;
  40. info:='';
  41. if ((DWord(lp) shr )=) and (code=HC_ACTION) then
  42. if ((DWord(lp) shr )=) then
  43. info:='WM_SYSKEYUP'
  44. else
  45. info:='WM_KEYUP'
  46. else
  47. if ((DWord(lp) shr )=) then
  48. info:='WM_SYSKEYDOWN'
  49. else
  50. info:='WM_KEYDOWN';
  51. info:=info+','+inttostr(wp)+','+inttostr(lp);
  52. if Assigned(myKeyBoardHook.callbackFun) then
  53. myKeyBoardHook.callbackFun(pchar(info));
  54. Result := CallNextHookEx(myKeyBoardHook.hook,code,wp,lp);
  55. end;
  56.  
  57. procedure InstallKeyBoardHook(callback:TCallBackFun);stdcall;
  58. begin
  59. if not myKeyBoardHook.isrun then
  60. begin
  61. myKeyBoardHook.hook:=SetWindowsHookEx(WH_KEYBOARD,@GetKeyBoardInfo,HInstance,);
  62. myKeyBoardHook.callBackFun:=callBack;
  63. myKeyBoardHook.isrun:=not myKeyBoardHook.isrun;
  64. end;
  65. end;
  66.  
  67. procedure UninstallKeyBoardHook();stdcall;
  68. begin
  69. if myKeyBoardHook.isrun then
  70. begin
  71. UnHookWindowsHookEx(myKeyBoardHook.hook);
  72. myKeyBoardHook.callBackFun:=nil;
  73. myKeyBoardHook.isrun:=not myKeyBoardHook.isrun;
  74. end;
  75. end;
  76.  
  77. Procedure DLLEntryPoint(dwReason:DWord);
  78. begin
  79. Case dwReason of
  80. DLL_PROCESS_ATTACH:begin
  81. myKeyBoardHook.isrun:=false;
  82. end;
  83. DLL_PROCESS_DETACH:;
  84. DLL_THREAD_ATTACH:;
  85. DLL_THREAD_DETACH:;
  86. End;
  87. end;
  88.  
  89. exports
  90. InstallKeyBoardHook,
  91. UninstallKeyBoardHook;
  92.  
  93. begin
  94. DLLProc := @DLLEntryPoint;
  95. DLLEntryPoint(DLL_PROCESS_ATTACH);
  96. end.

以上是创建一个全局钩子函数的Dll来记录按键信息

  1. library Mousehook;
  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 along
  10. with your DLL. To avoid using BORLNDMM.DLL, pass string information
  11. using PChar or ShortString parameters. }
  12.  
  13. uses
  14. SysUtils,
  15. Classes,
  16. Windows,
  17. Messages,
  18. ShellAPI;
  19.  
  20. type
  21. TCallbackFun=procedure(info:pchar);
  22. TMouseHook=record
  23. isrun:Bool;
  24. hook:HHook;
  25. callbackFun:TCallbackFun;
  26. end;
  27.  
  28. var
  29. myMouseHook:TMouseHook;
  30.  
  31. {$R *.res}
  32. //.定义自定义的HOOK函数,函数必须和需要HOOK的钩子类型保持同样的参数列表
  33. function GetHookInfo(code:Integer;wp:WPARAM;lp:LPARAM):LResult;stdcall;
  34. var
  35. info:String;
  36. begin
  37. if code< then
  38. begin
  39. Result:=CallNextHookEx(myMouseHook.hook,code,wp,lp);
  40. Exit;
  41. end;
  42. info:='';
  43. case wp of
  44. //鼠标消息共有21种,其中10种点击是客户区,种是非客户区也就是消息名以NC开头的消息。和一个命中测试消息
  45. WM_LBUTTONDOWN:begin
  46. info:='WM_LBUTTONDOWN';
  47. end;
  48. WM_LBUTTONUP:begin
  49. info:='WM_LBUTTONUP';
  50. end;
  51. WM_LBUTTONDBLCLK:begin
  52. info:='WM_LBUTTONDBLCLK';
  53. end;
  54. WM_RBUTTONDOWN:begin
  55. info:='WM_RBUTTONDOWN';
  56. end;
  57. WM_RBUTTONUP:begin
  58. info:='WM_RBUTTONUP';
  59. end;
  60. WM_RBUTTONDBLCLK:begin
  61. info:='WM_RBUTTONDBLCLK';
  62. end;
  63. WM_MBUTTONDOWN:begin
  64. info:='WM_MBUTTONDOWN';
  65. end;
  66. WM_MBUTTONUP:begin
  67. info:='WM_MBUTTONUP';
  68. end;
  69. WM_MBUTTONDBLCLK:begin
  70. info:='WM_MBUTTONDBLCLK';
  71. end;
  72. WM_MOUSEMOVE:begin
  73. info:='WM_MOUSEMOVE';
  74. end;
  75. WM_NCMouseMove:begin
  76. info:='WM_NCMouseMove';
  77. end;
  78. WM_MOUSEWHEEL:
  79. begin
  80. info:='WM_MOUSEWHEEL';
  81. end;
  82. WM_NCHITTEST:begin
  83. info:='WM_NCHITTEST';
  84. end;
  85. WM_NCLBUTTONDOWN:BEGIN
  86. info:='WM_NCLBUTTONDOWN';
  87. end;
  88. WM_NCLBUTTONUP:BEGIN
  89. info:='WM_NCLBUTTONUP';
  90. end;
  91. WM_NCLBUTTONDBLCLK:BEGIN
  92. info:='WM_NCLBUTTONDBLCLK';
  93. end;
  94. WM_NCRBUTTONDOWN:BEGIN
  95. info:='WM_NCRBUTTONDOWN';
  96. end;
  97. WM_NCRBUTTONUP:BEGIN
  98. info:='WM_NCRBUTTONUP';
  99. end;
  100.  
  101. WM_NCRBUTTONDBLCLK:BEGIN
  102. info:='WM_NCRBUTTONDBLCLK';
  103. end;
  104. end;
  105. info:=info+','+inttostr(PMouseHookStruct(lp)^.wHitTestCode)+ ','+inttostr(MakeLParam(PMouseHookStruct(lp)^.pt.x,PMouseHookStruct(lp)^.pt.Y));
  106. if Assigned(myMouseHook.callbackFun) then
  107. myMouseHook.callbackFun(pchar(info));
  108. Result := CallNextHookEx(myMouseHook.hook,code,wp,lp);
  109. end;
  110.  
  111. procedure InstallMouseHook(callbackF:Tcallbackfun);stdcall;
  112. begin
  113. if not myMouseHook.isrun then
  114. begin
  115. {2.设置钩子函数
  116. setwindowhookEx参数说明
  117. 参数idHook指定建立的监视函数类型。
  118. 参数lpfn指定消息函数,在相应的消息产生后,系统会调用该函数并将消息值传递给该函数供处理。函数的一般形式为:
  119. Hookproc (code: Integer; wparam: WPARAM; lparam: LPARAM): LRESULT stdcall;
  120. 其中code为系统指示标记(对应于idHook),wParamlParam为附加参数,根据不同的消息监视类型而不同。
  121. 只要在程序中建立这样一个函数再通过SetwindowsHookEx函数将它加入到消息监视链中就可以处理消息了。
  122. }
  123. myMouseHook.hook:=setwindowshookex(WH_MOUSE,@gethookinfo,HInstance,);
  124. myMouseHook.callbackfun:=callbackf;
  125. myMouseHook.isrun:=not mymousehook.isrun;
  126. end;
  127. end;
  128.  
  129. procedure UninstallMouseHook();stdcall;
  130. begin
  131. if myMouseHook.isrun then
  132. begin
  133. UnHookWindowsHookEx(mymousehook.hook);
  134. myMouseHook.callbackfun :=nil;
  135. myMouseHook.isrun:=not myMouseHook.isrun;
  136. end;
  137. end;
  138.  
  139. Procedure DLLEntryPoint(dwReason:DWord);
  140. begin
  141. Case dwReason of
  142. DLL_PROCESS_ATTACH:begin
  143. myMouseHook.isrun:=false;
  144. end;
  145. DLL_PROCESS_DETACH:;
  146. DLL_THREAD_ATTACH:;
  147. DLL_THREAD_DETACH:;
  148. End;
  149. end;
  150.  
  151. exports
  152. InstallMouseHook,
  153. UninstallMouseHook;
  154.  
  155. begin
  156. DLLProc := @DLLEntryPoint;
  157. DLLEntryPoint(DLL_PROCESS_ATTACH);
  158. end.

以上是捕获鼠标消息的全局钩子DLL

使用一个新的线程来模拟发送消息

  1. procedure TPlayThread.Execute;
  2. var
  3. directive:string;
  4. i:integer;
  5. ForgroundForm:TForm;
  6. procedure ExecuteDir(directive:string);
  7. var
  8. tempList:TStringList;
  9. Wp,Lp:integer;
  10. wmtype:String;
  11. focusControl:string;
  12. duration:Cardinal;
  13. winCtl:TWinControl;
  14. tempHandle,focusHandle:THandle;
  15. classname:String;
  16. mousPoint:TPOINT;
  17. procedure findFocus;
  18. var
  19. temp:TWinControl;
  20. finded:Boolean;
  21. begin
  22. if ((wmtype='WM_MOUSEMOVE') or (wmtype='WM_NCMouseMove')) then Exit;
  23. winCtl:=TWinControl(ForgroundForm.FindChildControl(focusControl));
  24.  
  25. if winCtl<>nil then
  26. begin
  27. focusHandle:= winCtl.Handle;
  28. AttachThreadInput(GetWindowThreadProcessId(ForgroundForm.Handle,nil),Self.ThreadID,True);
  29. Ferrorinfo:=SysErrorMessage(GetLastError);
  30. winCtl.SetFocus;
  31. AttachThreadInput(GetWindowThreadProcessId(ForgroundForm.Handle,nil),Self.ThreadID,False);
  32. Ferrorinfo:=SysErrorMessage(GetLastError);
  33. Exit;
  34. end;
  35. temp:=nil;
  36. finded:=False;
  37. while not finded do
  38. begin
  39. GetCursorPos(mousPoint);
  40. tempHandle := WindowFromPoint(mousPoint);
  41. if tempHandle = then
  42. begin
  43. Sleep();
  44. Continue;
  45. end;
  46. temp:=FindControl(tempHandle);
  47. if temp=nil then
  48. begin
  49. Sleep();
  50. Continue;
  51. end;
  52. if (temp.Name = focusControl) or (classname=temp.ClassName) then
  53. finded:=True;
  54. end;
  55. focusHandle := temp.Handle;
  56. AttachThreadInput(GetWindowThreadProcessId(ForgroundForm.Handle,nil),Self.ThreadID,True);
  57. Ferrorinfo:=SysErrorMessage(GetLastError);
  58. temp.SetFocus;
  59. AttachThreadInput(GetWindowThreadProcessId(ForgroundForm.Handle,nil),Self.ThreadID,False);
  60. Ferrorinfo:=SysErrorMessage(GetLastError);
  61. end;
  62. begin
  63. tempList:=TStringList.Create;
  64. try
  65. tempList.CommaText:=directive;
  66. tempList.Delimiter:=',';
  67. wmtype:=tempList[];
  68. focusHandle:=;
  69. Wp:=StrToIntDef(tempList[],); //wParam
  70. Lp:=StrToIntDef(tempList[],); //Lparam
  71.  
  72. duration:= StrToIntDef(tempList[],);
  73. if (duration=) and (wmtype='WM_NCMouseMove') then Exit; //小于线程调度时间片的话就不延时---以免 sleep()直接放弃时间进入内核态
  74. if (wmtype='') or (tempList.Count<) then Exit;
  75. focusControl :=tempList[];
  76. classname := tempList[];
  77.  
  78. findFocus;
  79. //鼠标消息
  80. if wmtype='WM_LBUTTONDOWN' then TInputHelper.MouseLButtonDown(focusHandle,Wp,Lp)
  81. else if wmtype='WM_LBUTTONUP' then TInputHelper.MouseLButtonUp(focusHandle,Wp,Lp,True)
  82. else if wmtype='WM_LBUTTONDBLCLK' then TInputHelper.MouseLButtonDbClick(focusHandle,Wp,Lp,True)
  83. else if wmtype='WM_RBUTTONDOWN' then TInputHelper.MouseRButtonDown(focusHandle,Wp,Lp,True)
  84. else if wmtype='WM_RBUTTONUP' then TInputHelper.MouseRButtonUp(focusHandle,Wp,Lp,True)
  85. else if wmtype='WM_RBUTTONDBLCLK' then TInputHelper.MouseRButtonDbClick(focusHandle,Wp,Lp,True)
  86. else if wmtype='WM_MBUTTONDOWN' then TInputHelper.MouseMButtonDown(focusHandle,Wp,Lp,True)
  87. else if wmtype='WM_MBUTTONUP' then TInputHelper.MouseMButtonUp(focusHandle,Wp,Lp,True)
  88. else if wmtype='WM_MBUTTONDBLCLK' then TInputHelper.MouseMButtonDbClick(focusHandle,Wp,Lp,True)
  89. else if wmtype='WM_MOUSEMOVE' then TInputHelper.MouseMove(focusHandle,Wp,Lp,True)
  90. else if wmtype='WM_MOUSEWHEEL' then TInputHelper.MouseWHEEL(focusHandle,Wp,Lp,True)
  91. //鼠标非客户区
  92. else if wmtype='WM_NCMouseMove' then TInputHelper.MouseNCMouseMove(focusHandle,Wp,Lp,True)
  93. else if wmtype='WM_NCHITTEST' then TInputHelper.MouseNCHITTEST(focusHandle,Wp,Lp,True)
  94. else if wmtype='WM_NCLBUTTONDOWN' then TInputHelper.MouseNCLBUTTONDOWN(focusHandle,Wp,Lp,True)
  95. else if wmtype='WM_NCLBUTTONUP' then TInputHelper.MouseNCLBUTTONUP(focusHandle,Wp,Lp,True)
  96. else if wmtype='WM_NCLBUTTONDBLCLK' then TInputHelper.MouseNCLBUTTONDBLCLK(focusHandle,Wp,Lp,True)
  97. else if wmtype='WM_NCRBUTTONDOWN' then TInputHelper.MouseNCRBUTTONDOWN(focusHandle,Wp,Lp,True)
  98. else if wmtype='WM_NCRBUTTONUP' then TInputHelper.MouseNCRBUTTONUP(focusHandle,Wp,Lp,True)
  99. else if wmtype='WM_NCRBUTTONDBLCLK' then TInputHelper.MouseRButtonDbClick(focusHandle,Wp,Lp,True)
  100. //键盘消息
  101. else if wmtype='WM_KEYDOWN' then TInputHelper.KeyDown(focusHandle,Wp,Lp,True)
  102. else if wmtype='WM_KEYUP' then TInputHelper.KEYUP(focusHandle,Wp,Lp,True)
  103. else if wmtype='WM_SYSKEYDOWN' then TInputHelper.KeySYSKEYDOWN(focusHandle,Wp,Lp,True)
  104. else if wmtype='WM_SYSKEYUP' then TInputHelper.KeySYSKEYUP(focusHandle,Wp,Lp,True);
  105. Application.ProcessMessages;
  106. Sleep(duration);
  107. finally
  108. tempList.Free;
  109. end;
  110. end;
  111. begin
  112. Sleep();
  113. try
  114. ForgroundForm :=InputRecord.ForgroundForm;
  115. for i:= to PosList.Count- do
  116. begin
  117. directive:=PosList[i];
  118. ExecuteDir(directive);
  119. end;
  120. finally
  121. InputRecord.FIsPlay:=False;
  122. end;
  123.  
  124. end;

点击这里下载代码

【笨嘴拙舌WINDOWS】实践检验之按键精灵【Delphi】的更多相关文章

  1. 按键精灵 句柄 获得句柄 控制windows窗口 后台

    新建一个文本文档,打开,Windows就会给这个文本文档的窗口临时分配唯一的一串数字来标识这个窗体,以区别于其他窗口,这串数字就叫句柄.   因为句柄是临时随机分配的,所以每次虽然是打开同一个文件,但 ...

  2. [教程] 以本论坛为例,手把手教你使用按键精灵POST登陆网页

    本帖最后由 isaacc 于 2012-2-26 11:08 编辑 整个操作,很无脑.只要你够勤快,你学不会,你来咬我.懒人和伸手党就直接复制代码去玩吧,但我不是叫你拿去干坏事. 准备工具:WPE和I ...

  3. 按键精灵对APP自动化测试(下)

    上一篇介绍了安卓app上使用按键精灵的实践,这里再来说说苹果上的app. 由于iOS相关工具对操作系统的限制,目前在iOS10.0.2系统上应用成功. 二.       苹果手机按键精灵APP录制 适 ...

  4. QQ2008自动聊天精灵delphi源码

    QQ2008自动聊天精灵delphi源码   unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Grap ...

  5. 【按键精灵篇】如何做一个自动打开APP进入注册页面自动输入自己手机号

    按键精灵,虽然很早听过,但是一直没有真正使用过,所以最近有点时间也简单试一下,通过脚本自动清理APP缓存,打开百家号并自动进入注册页面输入自己的手机号. 软件清单 1. 雷电手机模拟器:https:/ ...

  6. 转:Android随笔之——使用Root权限实现后台模拟全局按键、触屏事件方法(类似按键精灵)

    本文转载自CSDN的jzj1993,原文连接:http://blog.csdn.net/jzj1993/article/details/39158865 有时我们需要使用安卓实现在后台模拟系统按键,比 ...

  7. 按键精灵*ff

    Function gethttp(URL) Set objXML=CreateObject("Microsoft.XMLHTTP") objXML.Open "Get&q ...

  8. 按键精灵对APP自动化测试(上)

    简单介绍下应用背景:测试安卓app时发现重复点击某一按钮的时候会出现报错,开发修复后提交测试.如果采用手动点击按钮,效率不高,在领导提示下使用按键精灵实现自动操作. 一.       安卓手机按键精灵 ...

  9. 按键精灵与逍遥安卓ADB连接重键方法

    1.按键精灵与逍遥安卓ADB连接安装按键精灵与逍遥安卓这两个软件我不用多说了.安装好后把逍遥安卓安装目录下的三个文件adb.exe,AdbWinApi.dll,AdbWinUsbApi.dll 全部复 ...

随机推荐

  1. Linux 配置网络

    1.vi  /etc/sysconfig/network-scripts/ifcfg-eth0 2. # Advanced Micro Devices [AMD] 79c970 [PCnet32 LA ...

  2. 强力重置ASP.NET membership加密后的密码![转]

    公司网站的用户管理采用的是ASP.NET内置的membership管理,在web.config文件中的密码格式配置是加密了的,passwordFormat="Hashed",这样在 ...

  3. ZOJ 3555 Ice Climber(dp)

    晦涩的题意+各种傻逼害我调了那么久,实际上题目就是一个dp[i][j],dp[i][j]表示第i层第j个最少需要多少时间,当我们去更新dp[i][j]的时候,考虑的是从第i+1层的某一个dp[i+1] ...

  4. Activity学习(五)——Bundle机制

    上一篇文章我简单介绍了Activity之间简单切换,很简单,这一篇文章我们继续聊Android中程序页面互相跳转的Activity,不过这一次我们在Activity跳转时,携带一些简单的数据,然后在新 ...

  5. 深入浅出ES6(八):Symbols

    作者 Jason Orendorff  github主页  https://github.com/jorendorff 你是否知道ES6中的Symbols是什么,它有什么作用呢?我相信你很可能不知道, ...

  6. group_concat

    分割字符串 group_concat(a.EvidencesID separator ',') as EvidencesID #在MySQL配置文件(my.ini)中默认无该配置项,使用默认值时,值为 ...

  7. Codeforces Round #263 (Div. 2) D. Appleman and Tree(树形DP)

    题目链接 D. Appleman and Tree time limit per test :2 seconds memory limit per test: 256 megabytes input ...

  8. 传说中的WCF(3):多个协定

    我们知道,WCF服务端是先定义服务协定,其实就是一个接口,然后通过实现接口来定义服务类.那么,有一个问题,如果一个服务类同时实现N个接口(也就是有N个协定)呢?结果会如何? 不必猜,我们还是通过实验来 ...

  9. Learn CSS

    韩顺平老师的CSS讲的还是很简单的,仅作入门. div+css的介绍    div+css是什么. div元素是用来为HTML文档内大块(block-level)的内容提供结构和背景的元素. css是 ...

  10. MongoDB (六) MongoDB 集合操作

    一. MongoDB 创建集合 createCollection() 方法 MongoDB db.createCollection(name, options) 是用来创建集合. 语法: 基本的 cr ...