[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[技术讨论] [已解决]VBS重写实现给后台程序发送快捷键

本帖最后由 bat_noob 于 2023-7-3 16:40 编辑

需要实现的功能:
制作在windows 10系统下的可执行文件,vbs或exe程序,启动以后给非顶部激活窗口的其他后台程序发送一个快捷键,快捷键为CTRL+Y组合键。
因为VBS自带的sendkeys只能把快捷键输给当前活动窗口,需要VBS使用DynamicWrapperX调用windows api,有参考文档,但版本过旧,不少报错,所以需要重写。可提供DynamicWrapperX下载地址和参考文档地址。
可议价。




网上找到的代码
'==========================================================================
'
' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 4.0
'
' NAME:
'
' AUTHOR: Microsoft , Microsoft
' DATE : 2014/8/10
'
' COMMENT:
'
'===================定义变量,注册API对象==================================
Dim UserWrap,hWnd

Set UserWrap = CreateObject("DynamicWrapperX")
Set ws=WScript.CreateObject("wscript.shell")

WScript.Sleep 500

ws.Run "calc",0

WScript.Sleep 500

'Declare Function ShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
UserWrap.Register "USER32.DLL", "ShowWindow", "I=hl", "f=s", "R=l"

'Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

UserWrap.Register "USER32.DLL", "FindWindow", "I=ss", "f=s", "R=l"

'Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (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

UserWrap.Register "USER32.DLL", "SetWindowPos", "I=Hllllll", "f=s", "R=l"

'Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)
UserWrap.Register "USER32.DLL", "PostMessage", "I=hlls", "f=s", "R=l"

'Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long
UserWrap.Register "USER32.DLL", "SetWindowText", "I=Hs", "f=s", "R=l"

'Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
UserWrap.Register "USER32.DLL", "FindWindowEx", "I=llss", "f=s", "R=l"

'Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long

UserWrap.Register "USER32.DLL", "SetCursorPos", "I=ll", "f=s", "R=l"

'===================查找控件或者窗体句柄===============================


hWnd = UserWrap.FindWindow(vbNullString,"微信")
'hWnd = UserWrap.FindWindow("test",vbNullString)

hWnd1 = UserWrap.FindWindowEx(hWnd,0,vbNullString,Edit)

'UserWrap.ShowWindow hWnd,SW_HIDE

'UserWrap.SetWindowText hWnd,"hello world"

' MsgBox hWnd
' MsgBox hWnd1

'UserWrap.SetWindowPos hWnd, -1, 0, 0, 0, 0, 3

'MsgBox "将鼠标移到左上角"


'UserWrap.SetCursorPos 0,0


'=================定义系统常量===========================

Private Const WM_KEYDOWN = &H100
Private Const wm_keyup= &H101
Private Const WM_CHAR = &H102
Public Const WM_SYSKEYDOWN = &H104
Public Const WM_SYSKEYUP = &H105



'=================发送F1按键=====================
UserWrap.PostMessage hWnd, WM_KEYDOWN, 112, 0

'=================发送1002========================
UserWrap.PostMessage hWnd,WM_KEYDOWN ,97,0
UserWrap.PostMessage hWnd,WM_KEYDOWN ,96,0
UserWrap.PostMessage hWnd,WM_KEYDOWN ,96,0
UserWrap.PostMessage hWnd,WM_KEYDOWN ,98,0

不如用VB vbs搞api纯属歪门邪道

TOP

自己写个EXE也不难嘛
http://tieba.baidu.com/p/932891654

TOP

已经用PYTHON解决了,但只能发送虚拟按键,无法实现实体键盘的快捷键。无法解决我的问题。
又换了其他思路,还是用python,彻底解决了我的问题。

TOP

本帖最后由 bat_noob 于 2023-7-3 16:45 编辑

这个踩过不少坑的代码发上来吧,我也用不到了

给后台非活动窗口发送虚拟快捷键  python代码(无法实现实体键盘快捷键,这个代码发送的快捷键是 ctrl+y)

import win32api,win32gui,win32con,sys,time

self="MozillaWindowClass"
hwnd = win32gui.FindWindow(self,None)

win32api.PostMessage(hwnd,win32con.WM_SYSKEYDOWN,win32con.VK_LCONTROL,0)
win32api.PostMessage(hwnd,win32con.WM_SYSKEYDOWN,0x59,0)
time.sleep(1)
win32api.PostMessage(hwnd,win32con.WM_SYSKEYUP,win32con.VK_LCONTROL,0)
win32api.PostMessage(hwnd,win32con.WM_SYSKEYUP,0x59,0)

sys.exit(0)
1

评分人数

TOP

返回列表