VB 利用键盘模拟进行自动录入操作(完整模块代码)_第1页
VB 利用键盘模拟进行自动录入操作(完整模块代码)_第2页
VB 利用键盘模拟进行自动录入操作(完整模块代码)_第3页
VB 利用键盘模拟进行自动录入操作(完整模块代码)_第4页
VB 利用键盘模拟进行自动录入操作(完整模块代码)_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、'=全功能模拟键盘鼠标操作(一)模拟键盘进行按键或信息录入操作。范围:任意数字、字母的组合,模拟过程:逐个发送连续字符串中键名信息= 作者:ghong3298226 (原创 作者QQ:87732850 Private Type POINTAPI 'API座标数据类型 X As Long Y As Long End Type '获得前台窗口句柄 Private Declare Function GetForegroundWindow Lib "user32" ( As Long '设置前台窗口 Private Declare Function

2、SetForegroundWindow Lib "user32" (ByVal hwnd As Long As Long '获得一个窗口线程的ID Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long As Long '获得当前程序线程的ID Private Declare Function GetCurrentThreadId Lib "kernel32" (

3、As Long 'AttachThreadInput:线程连接。通常,系统内的每个线程都有自己的输入队列。本函数(既“连接线程输入函数”) '允许线程和进程共享输入队列。连接了线程后,输入焦点、窗口激活、鼠标捕获、键盘状态以及输入队列状态都会进入共享状态。 '调用这个函数时,会重设键盘状态。 Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long As

4、 Long '该函数检取光标的位置,以屏幕坐标表示 。 Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI As Long '该函数改变一个子窗口,弹出式窗口式顶层窗口的尺寸,位置和Z序 Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As L

5、ong, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long As Long '获得具有输入焦点的窗口句柄 Private Declare Function GetFocus Lib "user32" ( As Long '设置焦点窗口。 Private Declare Function SetFocus Lib "user32" (ByVal hwnd As Long As Long '插入消息队列。 Private Declare Function PostMess

6、age Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long As Long '该函数获得包含指定点的窗口的句柄 Private Declare Function WindowFromPointXY Lib "user32" Alias "WindowFromPoint" (ByVal xPoint As Long, ByVal

7、 yPoint As Long As Long '该函数将256个虚拟键的状态拷贝到指定的缓冲区中。 Private Declare Function GetKeyboardState Lib "user32" (pbKeyState As Byte As Long Private Declare Function SetKeyboardState Lib "user32" (lppbKeyState As Byte As Long '该函数检取指定虚拟键的状态。该状态指定此键是UP状态,DOWN状态,还是被触发的(开关每次按下此键时进行

8、切换)。 Private Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long As Integer '该函数使过程休眠以毫秒计的用户定义时间。 Private Declare Sub Sl eep Lib "kernel32" (ByVal dwMilliseconds As Long Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (

9、ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any As Long Private Declare Function keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long Private Const KEYEVENTF_KEYUP = &H2 '键码常量表 Private Const V

10、K_ATTN = &HF6 Private Const VK_BACK = &H8 Private Const VK_CANCEL = &H3 Private Const VK_CAPITAL = &H14 Private Const VK_CLEAR = &HC Private Const VK_CONTROL = &H11 Private Const VK_CRSEL = &HF7 Private Const VK_DECIMAL = &H6E Private Const VK_DELETE = &H2E Privat

11、e Const VK_DOWN = &H28 Private Const VK_END = &H23 Private Const VK_EREOF = &HF9 Private Const VK_ESCAPE = &H1B Private Const VK_EXECUTE = &H2B Private Const VK_EXSEL = &HF8 Private Const VK_F1 = &H70 Private Const VK_F10 = &H79 Private Const VK_F11 = &H7A Private

12、 Const VK_F12 = &H7B Private Const VK_F13 = &H7C Private Const VK_F14 = &H7D Private Const VK_F15 = &H7E Private Const VK_F16 = &H7F Private Const VK_F17 = &H80 Private Const VK_F18 = &H81 Private Const VK_F19 = &H82 Private Const VK_F2 = &H71 Private Const VK_F20

13、 = &H83 Private Const VK_F21 = &H84 Private Const VK_F22 = &H85 Private Const VK_F23 = &H86 Private Const VK_F24 = &H87 Private Const VK_F3 = &H72 Private Const VK_F4 = &H73 Private Const VK_F5 = &H74 Private Const VK_F6 = &H75 Private Const VK_F7 = &H76 Priva

14、te Const VK_F8 = &H77 Private Const VK_F9 = &H78 Private Const VK_HELP = &H2F Private Const VK_HOME = &H24 Private Const VK_INSERT = &H2D Private Const VK_LBUTTON = &H1 Private Const VK_LCONTROL = &HA2 Private Const VK_LEFT = &H25 Private Const VK_LMENU = &HA4 Pri

15、vate Const VK_LSHIFT = &HA0 Private Const VK_MBUTTON = &H4 ' NOT contiguous with L RBUTTON Private Const VK_MENU = &H12 Private Const VK_NEXT = &H22 Private Const VK_NONAME = &HFC Private Const VK_NUMLOCK = &H90 Private Const VK_NUMPAD0 = &H60 Private Const VK_NUMPAD1

16、 = &H61 Private Const VK_NUMPAD2 = &H62 Private Const VK_NUMPAD3 = &H63 Private Const VK_NUMPAD4 = &H64 Private Const VK_NUMPAD5 = &H65 Private Const VK_NUMPAD6 = &H66 Private Const VK_NUMPAD7 = &H67 Private Const VK_NUMPAD8 = &H68 Private Const VK_NUMPAD9 = &H69

17、Private Const VK_OEM_CLEAR = &HFE Private Const VK_PA1 = &HFD Private Const VK_PAUSE = &H13 Private Const VK_PLAY = &HFA Private Const VK_PRINT = &H2A Private Const VK_PRIOR = &H21 Private Const VK_PROCESSKEY = &HE5 Private Const VK_RBUTTON = &H2 Private Const VK_RCON

18、TROL = &HA3 Private Const VK_RETURN = &HD Private Const VK_RIGHT = &H27 Private Const VK_RMENU = &HA5 Private Const VK_RSHIFT = &HA1 Private Const VK_SCROLL = &H91 Private Const VK_SELECT = &H29 Private Const VK_SEPARATOR = &H6C Private Const VK_SHIFT = &H10 Priva

19、te Const VK_SNAPSHOT = &H2C Private Const VK_SPACE = &H20 Private Const VK_TAB = &H9 Private Cons t VK_UP = &H26 Private Const VK_ZOOM = &HFB Private Const VK_ADD = &H6B '加键 Private Const VK_SUBTRACT = &H6D '减键 Private Const VK_MULTIPLY = &H6A '乘键 Private

20、Const VK_DIVIDE = &H6F '除键 Private Const WM_KEYDOWN = &H100 '按下键盘按键 Private Const WM_KEYUP = &H101 '弹起键盘按键 Private Const WM_LBUTTONDOWN = &H201 '鼠标左键按下 Private Const WM_LBUTTONUP = &H202 '鼠标左键弹起 Private Const WM_LBUTTONDBLCLK = &H203 '鼠标左键双击 Private Co

21、nst WM_RBUTTONDOWN = &H204 '鼠标右键按下 Private Const WM_RBUTTONUP = &H205 '鼠标右键弹起 Private Const WM_RBUTTONDBLCLK = &H206 '鼠标右键双击 Private Const WM_MBUTTONDBLCLK = &H209 '鼠标中间键双击 Private Const WM_MBUTTONDOWN = &H207 '鼠标中间键按下 Private Const WM_MBUTTONUP = &H208 &#

22、39;鼠标中间键弹起 Private Const WM_MOUSEMOVE = &H200 '鼠标移动常数 '-自定义键盘状态常量- Private Const GAKB_LOCKED = &HFF80 '按键锁定状态。 Private Const GAKB_KEYDOWN = &H80 '按键按下状态。 Public Function SimKey(ByVal cKeyName As String, nKey As Boolean '-模拟单个键盘按键操作,cKeyName:要操作的按键名(如若模拟键为小键盘上的按键应用此格式:

23、"小键盘,8"- End Function Public Function SimMouse(ByVal nByWM_Const As Long, Optional ByVal nX As Long, Optional ByVal nY As Long As Boolean '-模拟鼠标按键操作:nByWM_Const:鼠标动作,参见WM开头的常量- '-nX,nY 指定鼠标操作的座标,若为0则模拟操作座标为当前鼠标光标所在座标。- End Function Public Function Sim( As String CreateIExprSrvObj 0

24、, 4, 0 '-模拟输入文本:cText 进行模拟录入的字符串。函数成功返回一个空字符串,失败返回模拟失败的具体信息- Dim nFocusHwnd As Long, cLoseMsg As String, nWinID As Long, nMeID As Long, cSent As String '获得具有输入焦点的窗口句柄。 nWinID = GetWindowThreadProcessId(GetForegroundWindow(, vbNull '获取并创建前台窗口的线程。 nMeID = GetCurrentThreadId '获得本程序线程ID

25、Call AttachThreadInput(nWinID, nMeID, True '进行线程贴附 nFocusHwnd = GetFocus( '获得具有输入焦点窗口的句柄。 For i = 1 To 255 Step 1 '发送信息。 DoEvents '发送的字母只能为大写,所以字母一律转成大写再发送。输出的大小写由大写锁定键来决定。且这个转换对其它字符无影响。 If PostMessage(nFocusHwnd, WM_KEYDOWN, i, 0 = 0 Then cLoseMsg = cLoseMsg & cSent '函数失败时收集

26、失败信息。 End If Sleep 1 Next Call AttachThreadInput(nW inID, nMeID, False '解除线程贴附。 Call LockedVK_KEY(VK_CAPITAL, False End Function Public Function SimInputText(ByVal cText As String As String '-模拟输入文本:cText 进行模拟录入的字符串。函数成功返回一个空字符串,失败返回模拟失败的具体信息- Dim nFocusHwnd As Long, cLoseMsg As String, nWin

27、ID As Long, nMeID As Long, cSent As String '获得具有输入焦点的窗口句柄。 nWinID = GetWindowThreadProcessId(GetForegroundWindow(, vbNull '获取并创建前台窗口的线程。 nMeID = GetCurrentThreadId '获得本程序线程ID Call AttachThreadInput(nWinID, nMeID, True '进行线程贴附 nFocusHwnd = GetFocus( '获得具有输入焦点窗口的句柄。 Call AttachThre

28、adInput(nWinID, nMeID, False For i = 1 To Len(cText Step 1 '发送信息。 cSent = Mid(cText, i, 1 If UCase(cSent = cSent Then '當cSent為一個大寫字母时设置大写锁定键实现大写输出。 Call LockedVK_KEY(VK_CAPITAL, True Else Call LockedVK_KEY(VK_CAPITAL, False End If Sleep 1 '发送的字母只能为大写,所以字母一律转成大写再发送。输出的大小写由大写锁定键来决定。且这个转换对其

29、它字符无影响。 If InStr(1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", UCase(cSent > 0 Then If PostMessage(nFocusHwnd, WM_KEYDOWN, Asc(UCase(cSent, 0 = 0 Then cLoseMsg = cLoseMsg & cSent '函数失败时收集失败信息。 End If Else MsgBox "向焦点发送信息时,其内容只能是字母及数字的任意组合。" Exit For End If Sleep 1 Next &#

30、39;解除线程贴附。 Call LockedVK_KEY(VK_CAPITAL, False '解除虚拟大写键锁定。 SimInputText = cLoseMsg End Function Private Function GetMousePointWindowHwnd( As Long '获得鼠标所在座标下窗口的位置。 Dim xy As POINTAPI '声明变量类型 GetCursorPos xy '获得当前鼠标光标座标。 aHwnd = WindowFromPointXY(xy.X, xy.Y '取得当前鼠标坐标下窗口的句柄 GetMouse

31、PointWindowHwnd = aHwnd End Function Public Function GetAKeyBuff(ByVal VK_Keys As Long As Long '单一键盘键位状态判断。默认状态返回0,否则返回二个状态中的一个。 Dim nBuff As Long nBuff = GetKeyState(VK_Keys '获得键位状态。 If nBuff Mod GAKB_LOCKED = 1 Then GetAKeyBuff = GAKB_LOCKED '该键位处于锁定状态 If nBuff And GAKB_KEYDOWN Then Ge

32、tAKeyBuff = GAKB_KEYDOWN '该键位处于按下状态 End Function Private Function LoadArrKeyNames(Optional ByVal vFindKeyNameOrKeyVal As Variant As Variant '函数失败返回未初始化变量, '成功:vFindKeyNameOrKeyVal 为可选参数。 'vFindKeyNameOrKeyVal 为一个键值常量,函数返回该常量对应的键名; 'vFindKeyNameOrKeyVal 为一个字符串键名时,函数返回该键名对应的常量值。 &#

33、39;vFindKeyNameOrKeyVal 为未初始化的变量时,返回初始化完成的以键值为数组元素座标,键名为数组元素值的字符类型数组。 '-定义键码数组,用于键码常量与键码对应字符名的转换- Dim ArrVKKeys(1 To 255 As String '定义数组,指定范围 '-键码值键名转换- ArrVKKeys(&H70 = "F1" 'F1 ArrVKKeys(&H71 = "F2" 'F2 ArrVKKeys(&H72 = "F3" 'F3 Arr

34、VKKeys(&H73 = "F4" 'F4 ArrVKKeys(&H74 = "F5" 'F5 ArrVKKeys(&H75 = "F6" 'F6 ArrVKKeys(&H76 = "F7" 'F7 ArrVKKeys(&H77 = "F8" 'F8 ArrVKKeys(&H78 = "F9" 'F9 ArrVKKeys(&H79 = "F10" &#

35、39;F10 ArrVKKeys(&H7A = "F11" 'F11 ArrVKKeys(&H7B = "F12" 'F12 ArrVKKeys(&H7C = "F13" 'F13 ArrVKKeys(&H7D = "F14" 'F14 ArrVKKeys(&H7E = "F15" 'F15 ArrVKKeys(&H7F = "F16" 'F16 ArrVKKeys(&H8

36、0 = "F17" 'F17 ArrVKKeys(&H81 = "F18" 'F18 ArrVKKeys(&H82 = "F19" 'F19 ArrVKKeys(&H83 = "F20" 'F20 ArrVKKeys(&H84 = "F21" 'F21 ArrVKKeys(&H85 = "F22" 'F22 ArrVKKeys(&H86 = "F23" '

37、;F23 ArrVKKeys(&H87 = "F24" 'F24 ArrVKKeys(&HA0 = "LSHIFT" '左上档 ArrVKKeys(&HA2 = "LCONTROL" '左控制 ArrVKKeys(&HA4 = "LMENU" '左菜单 ArrVKKeys(&H13 = "PAUSE" '暂停 ArrVKKeys(&HA1 = "RSHIFT" '右上档 ArrVK

38、Keys(&HA3 = "RCONTROL" '右控制 ArrVKKeys(&HA5 = "RMENU" '右菜单 ArrVKKeys(&H29 = "SELECT" '选择 ArrVKKeys(&H69 = "NUMPAD9" '小键盘9 ArrVKKeys(&H68 = "NUMPAD8" '小键盘8 ArrVKKeys(&H67 = "NUMPAD7" '小键盘7 ArrVK

39、Keys(&H66 = "NUMPAD6" '小键盘6 ArrVKKeys(&H65 = "NUMPAD5" '小键盘5 ArrVKKeys(&H64 = "NUMPAD4" '小键盘4 ArrVKKeys(&H63 = "NUMPAD3" '小键盘3 ArrVKKeys(&H62 = "NUMPAD2" '小键盘2 ArrVKKeys(&H61 = "NUMPAD1" '小键盘1

40、 ArrVKKeys(&H60 = "NUMPAD0" '小键盘0 ArrVKKeys(&H25 = "LEFT" '向左 ArrVKKeys(&H27 = "RIGHT" '向右 ArrVKKeys(&H28 = "DOWN" '向下 ArrVKKeys(&H26 = "UP" '向上 ArrVKKeys(&H22 = "NEXT" '下一个 ArrVKKeys(&H8

41、= "BACK" '退格 ArrVKKeys(&H1B = "ESCAPE" '退出 ArrVKKeys(&HFB = "ZOOM" '缩放 ArrVKKeys(&H10 = "SHIFT" '上档 ArrVKKeys(&H2E = "DELETE" '删除 ArrVKKeys(&H3 = "CANCEL" '取消 ArrVKKeys(&HC = "CLEAR"

42、 '清除 ArrVKKeys(&HFE = "OEM_CLEAR" '清除 ArrVKKeys(&H11 = "CONTROL" '控制 ArrVKKeys(&H20 = "SP ACE" '空格 ArrVKKeys(&H23 = "END" '结尾 ArrVKKeys(&H6D = "SUBTRACT" '减键 ArrVKKeys(&H6B = "ADD" '加键 Arr

43、VKKeys(&H91 = "SCROLL" '滚动 ArrVKKeys(&HD = "RETURN" '返回 ArrVKKeys(&H14 = "CAPITAL" '大写锁定 ArrVKKeys(&H2A = "PRINT" '打印 ArrVKKeys(&H6F = "DIVIDE" '除键 ArrVKKeys(&H6A = "MULTIPLY" '乘键 ArrVKKeys(&a

44、mp;H2D = "INSERT" '插入 ArrVKKeys(&H12 = "MENU" '菜单 ArrVKKeys(&HFA = "PLAY" '播放 ArrVKKeys(&H2F = "HELP" '帮助 ArrVKKeys(&H9 = "TAB" 'TAB ArrVKKeys(&H90 = "NUMLOCK" '小键盘锁定 ArrVKKeys(&H4 = "MBU

45、TTON" 'NOTcontiguouswithLRBUTTON ArrVKKeys(&H24 = "HOME" 'HOME ArrVKKeys(&HF6 = "ATTN" 'Do't Know ArrVKKeys(&HF7 = "CRSEL" 'Do't Know ArrVKKeys(&H6E = "DECIMAL" 'Do't Know ArrVKKeys(&HF9 = "EREOF&qu

46、ot; 'Do't Know ArrVKKeys(&H2B = "EXECUTE" 'Do't Know ArrVKKeys(&HF8 = "EXSEL" 'Do't Know ArrVKKeys(&H1 = "LBUTTON" 'Do't Know ArrVKKeys(&HFC = "NONAME" 'Do't Know ArrVKKeys(&HFD = "PA1" '

47、;Do't Know ArrVKKeys(&H21 = "PRIOR" 'Do't Know ArrVKKeys(&HE5 = "PROCESSKEY" 'Do't Know ArrVKKeys(&H2 = "RBUTTON" 'Do't Know ArrVKKeys(&H6C = "SEPARATOR" 'Do't Know ArrVKKeys(&H2C = "SNAPSHOT" &

48、#39;系统快照键 Select Case TypeName(vFindKeyNameOrKeyVal '根据传入参数类型选择处理方式。 Case Is = "Integer" '-根据键值常量的值返回键名 If vFindKeyNameOrKeyVal > 0 And vFindKeyNameOrKeyVal <= 255 Then LoadArrKeyNames = ArrVKKeys(vFindKeyNameOrKeyVal Else MsgBox "参数值超出范围:应为1255,但实际只有88个参数可用。" End If Case Is = "String&

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论