'=============全功能模拟键盘鼠标操作(一)----模拟键盘进行按键或信息录入操作。范围:任意数字、字母的组合,模拟过程:逐个发送连续字符串中键名信息================================
作者: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 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" () As Long
'▼AttachThreadInput:线程连接。通常,系统内的每个线程都有自己的输入队列。本函数(既“连接线程输入函数”)
'允许线程和进程共享输入队列。连接了线程后,输入焦点、窗口激活、鼠标捕获、键盘状态以及输入队列状态都会进入共享状态。
'调用这个函数时,会重设键盘状态。
Private Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As 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 Long, 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 PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByV
al 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 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状态,还是被触发的(开关每次按下此键时进行切换)。
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" (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 VK_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
Private 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 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 = &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
Private 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
Private 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 = &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
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_RCONTROL = &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
Private 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 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 Const 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 '鼠标中间键弹起
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:要操作的按键名(如若模拟键为小键盘上的按键应用此格式:"小键盘,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, 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
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 '函数失败时收集失败信息。
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, nWinID As Long, nMeID As Long, cSent As String
'获得具有输入焦点的窗口句柄。
nWinID = GetWindowThreadProcessId(GetForegroundWindow(), vbNull) '获取并创建前台窗口的线程。
nMeID = GetCurrentThreadId '获得本程序线程ID
Call AttachThreadInput(nWinID, nMeID, True) '进行线程贴附
nFocusHwnd = GetFocus() '获得具有输入焦点窗口的句柄。
Call AttachThreadInput(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
'发送的字母只能为大写,所以字母一律转成大写再发送。输出的大小写由大写锁定键来决定。且这个转换对其它字符无影响。
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
'解除线程贴附。
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) '取得当前鼠标坐标下窗口的句柄
GetMousePointWindowHwnd = 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 GetAKeyBuff = GAKB_KEYDOWN '该键位处于按下状态
End Function
Private Function LoadArrKeyNames(Optional ByVal
vFindKeyNameOrKeyVal As Variant) As Variant
'函数失败返回未初始化变量,
'成功:vFindKeyNameOrKeyVal 为可选参数。
'vFindKeyNameOrKeyVal 为一个键值常量,函数返回该常量对应的键名;
'vFindKeyNameOrKeyVal 为一个字符串键名时,函数返回该键名对应的常量值。
'vFindKeyNameOrKeyVal 为未初始化的变量时,返回初始化完成的以键值为数组元素座标,键名为数组元素值的字符类型数组。
'----定义键码数组,用于键码常量与键码对应字符名的转换------
Dim ArrVKKeys(1 To 255) As String '定义数组,指定范围
'--------------键码值键名转换-------------------------------
ArrVKKeys(&H70) = "F1" 'F1
ArrVKKeys(&H71) = "F2" 'F2
ArrVKKeys(&H72) = "F3" 'F3
ArrVKKeys(&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" '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(&H80) = "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" 'F23
ArrVKKeys(&H87) = "F24" 'F24
ArrVKKeys(&HA0) = "LSHIFT" '左上档
ArrVKKeys(&HA2) = "LCONTROL" '左控制
ArrVKKeys(&HA4) = "LMENU" '左菜单
ArrVKKeys(&H13) = "PAUSE" '暂停
ArrVKKeys(&HA1) = "RSHIFT" '右上档
ArrVKKeys(&HA3) = "RCONTROL" '右控制
ArrVKKeys(&HA5) = "RMENU" '右菜单
ArrVKKeys(&H29) = "SELECT" '选择
ArrVKKeys(&H69) = "NUMPAD9" '小键盘9
ArrVKKeys(&H68) = "NUMPAD8" '小键盘8
ArrVKKeys(&H67) = "NUMPAD7" '小键盘7
ArrVKKeys(&H66) = "NUMPAD6" '小键盘6
ArrVKKeys(&H65) = "NUMPAD5" '小键盘5
ArrVKKeys(&H64) = "NUMPAD4" '小键盘4
ArrVKKeys(&H63) = "NUMPAD3" '小键盘3
ArrVKKeys(&H62) = "NUMPAD2" '小键盘2
ArrVKKeys(&H61) = "NUMPAD1" '小键盘1
ArrVKKeys(&H60) = "NUMPAD0" '小键盘0
ArrVKKeys(&H25) = "LEFT" '向左
ArrVKKeys(&H27) = "RIGHT" '向右
ArrVKKeys(&H28) = "DOWN" '向下
ArrVKKeys(&H26) = "UP" '向上
ArrVKKeys(&H22) = "NEXT" '下一个
ArrVKKeys(&H8) = "BACK" '退格
ArrVKKeys(&H1B) = "ESCAPE" '退出
ArrVKKeys(&HFB) = "ZOOM" '缩放
ArrVKKeys(&H10) = "SHIFT" '上档
ArrVKKeys(&H2E) = "DELETE" '删除
ArrVKKeys(&H3) = "CANCEL" '取消
ArrVKKeys(&HC) = "CLEAR" '清除
ArrVKKeys(&HFE) = "OEM_CLEAR" '清除
ArrVKKeys(&H11) = "CONTROL" '控制
vb设计模拟密码输入器ArrVKKeys(&H20) = "SP
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论