【问题标题】:SendKeys does not always send the key macros to AppActivate via ProcessId to another processSendKeys 并不总是通过 ProcessId 将密钥宏发送到 AppActivate 到另一个进程
【发布时间】:2020-04-14 17:42:32
【问题描述】:

任务:编写一个程序,单击时仅显示 3 个按钮,模拟在另一个窗口中按下组合键(宏 Ctrl+Z)。

我做了什么:我的代码:

Public Class Form1
    Dim FigmaWindowName As String
    Dim Processes

    Public Sub New()
        'init form
        InitializeComponent()

        'set top position
        TopMost = True


    End Sub

    Private Sub ButtonUndo_Click(sender As Object, e As EventArgs) Handles ButtonUndo.Click

        Processes = GetObject("winmgmts:").InstancesOf("Win32_Process")

        For Each Process In Processes
            If StrComp(Process.Name, "Figma.exe", vbTextCompare) = 0 Then

                ' Activate the window using its process ID...
                With CreateObject("WScript.Shell")
                    .AppActivate(Process.ProcessId)
                    .SendKeys("(^z)")

                End With

                ' We found our process. No more iteration required...
                Exit For

            End If
        Next

        '....other buttons
    End Sub
End Class

问题:不稳定。如果它不起作用,则单击时两个窗口都会闪烁,并且组合键不会发送到其他应用程序。 https://www.loom.com/share/89c47dfa38bc4bb6b1d3ff8b97b84ac8

问题我做错了什么?

【问题讨论】:

    标签: vba vb.net visual-studio visual-studio-2019 sendkeys


    【解决方案1】:

    首先,我不相信存在与“发送密钥”一起使用的稳定应用程序。但是,如果这是实现自动化的唯一方法,请在此处对您的代码进行一些额外的检查。

    Dim wShell = CreateObject("WScript.Shell")
    
    Dim isWindowActive As Boolean = False
    Do 
        'AppActivate will return true when window is really active'
        isWindowActive = wShell.AppActivate(Process.ProcessId) 
        WScript.Sleep 100
    Loop Until isWindowActive 
    wShell.sendKeys("(^z)")
    

    【讨论】:

    • 不,这不是唯一的方法。但我不知道其他方式...您能描述其他方式或发送示例吗?
    • 您可以尝试使用 AutoIt,使用发送键非常稳定,使用简单。或者更复杂——如果你熟悉 win API,你可以尝试使用 UIAutomationClient 来解决你的任务。
    • 哇,我尝试使用 AutoItX3 结束它工作正常,但我不确定这段代码是否正确(我说的是点击后运行):joxi.ru/KAxMgkzhZXX1dr
    • 睡眠并不是真正需要的。 AutoIt 应该处理等待。其余的看起来不错。
    • 还有一个……你知道的。 AutoItX3 的这个 func .Run() .WinWaitActive() 在 x64 Windows 中不起作用,我的应用程序崩溃了。路径和名称是正确的。 joxi.ru/KAxMgkzhZXXdOrmaby 你上次可以帮帮我
    猜你喜欢
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-16
    • 2013-09-30
    相关资源
    最近更新 更多