【问题标题】:How to use wscript.shell to ALT+Tab?如何使用 wscript.shell 到 ALT+Tab?
【发布时间】:2018-10-22 04:39:08
【问题描述】:

是否可以使用wscript.shell 发送 ALT+TAB ?

$wshell = New-Object -ComObject wscript.shell;
$wshell.SendKeys('\t')

【问题讨论】:

标签: powershell windows-scripting comobject wscript.shell


【解决方案1】:

我改用System.Windows.Forms.SendKeys

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.SendKeys]::SendWait('%{TAB}')

不知道为什么,但是 wscript.shell 不能与 %{TAB} 一起使用。

【讨论】:

    【解决方案2】:

    因为它不需要使用 % 或使用 \t。

    例子:

    Set WshShell = WScript.CreateObject("WScript.Shell")
    WshShell.Run "notepad", 9 
    
    ' Give Notepad time to load
    WScript.Sleep 500 
    
    Dim Msg: Msg = "Hello, World!"
    
    ' Type in One Character at a time
    For i = 1 To Len(Msg)
        WScript.Sleep 200
        WshShell.SendKeys Mid(Msg, i, 1)
    Next
    
    WshShell.SendKeys "{ENTER}"
    WshShell.SendKeys "^{HOME}"
    WshShell.SendKeys "{TAB}"
    

    但既然我们都喜欢这里的 PowerShell,那么 StephenP 给你的是 PowerShell 方法来做到这一点。

    【讨论】: