SendKeys 并不总是将按键宏通过 ProcessId 发送到 AppActivate 到另一个进程

SendKeys does not always send the key macros to AppActivate via ProcessId to another process

任务:编写一个程序,点击时只显示 3 个按钮,模拟在另一个 window 中按下组合键(宏 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

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

问题我做错了什么?

首先,我不相信存在与 "send keys" 兼容的稳定应用程序。但如果这是进行自动化的唯一方法,请对您的代码进行一些额外检查。

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)")