【问题标题】:Sendkeys Powershell not working properlySendkeys Powershell 无法正常工作
【发布时间】:2013-12-21 19:32:53
【问题描述】:

还有其他方法可以将击键发送到应用程序吗?

我有这个 Powershell 脚本,在按下反引号键后,控制台应该在应用程序中打开,但这不会发生,但是按键盘的物理方式它可以完美地工作。

在我自己打开控制台后,sendkeys 确实有效,但第一个 sendkeys(反引号)不起作用。

我需要的是脚本通过发送反引号键打开控制台。

    # startpwsscrip.ps1

function wait {
  param([int]$stop = 8)
  Start-Sleep -seconds $stop
}

[void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
& "$env:C:\Program Files (x86)\Steam\SteamApps\common\Half-Life\cstrike.exe"
$a = Get-Process | Where-Object {$_.Name -eq "Counter-strike"}
wait3
$Cursor = [system.windows.forms.cursor]::Clip
[System.Windows.Forms.Cursor]::Position = New-Object system.drawing.point(509,763)
wait
[System.Windows.Forms.SendKeys]::SendWait({"ENTER"})
wait
[System.Windows.Forms.SendKeys]::SendWait({"`"})
wait
[System.Windows.Forms.SendKeys]::SendWait("connect ")
wait
[System.Windows.Forms.SendKeys]::SendWait("192.168.1.1")
wait
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")

【问题讨论】:

    标签: powershell


    【解决方案1】:

    我看到的第一个问题是您没有引用 System.Windows.Forms 并且您的 VisualBasic 引用中有一个额外的单引号,这不是很好。

    add-type -AssemblyName System.Windows.Forms
    
    #or
    
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    

    此处多单引号。

    ("'Microsoft.VisualBasic")
    # should be one or the other
    ('Microsoft.VisualBasic')
    ("Microsoft.VisualBasic")
    

    "Wait" 或 'Wait3' 不是 ps1 方法,试试 Start-Sleep 1

    Start-Sleep 1
    Start-Sleep 3
    

    【讨论】:

    • 至于反引号,我似乎找不到任何文档 ATM 来发送该按钮的密钥。但这里有一本关于我们如何可能不得不逃避它的好书。 rlmueller.net/PowerShellEscape.htm
    • 我认为 SendKeys 取决于您的区域设置(键盘布局),所以这可能会有所不同。我通过按住 shift,按 ` \ ` 并点击空格来创建反引号,所以要生成它,我将使用:[System.Windows.Forms.SendKeys]::SendWait("+\ ")+ 表示按住 shift
    【解决方案2】:

    由于反引号是一个特殊字符,请尝试使用双反引号。 一个用来逃避另一个,这样:

    [System.Windows.Forms.SendKeys]::SendWait("``")

    【讨论】:

      【解决方案3】:

      反引号是双引号字符串中的转义字符。您应该改用单引号字符串,因为单引号字符串的内容是按字面意思解释的。此外,字符串周围不需要大括号。

      [System.Windows.Forms.SendKeys]::SendWait('`')
      

      另见:

      help about_Quoting_Rules
      help about_Escape_Characters
      

      【讨论】:

        猜你喜欢
        • 2023-01-21
        • 2016-10-03
        • 1970-01-01
        • 2022-11-13
        • 1970-01-01
        • 1970-01-01
        • 2014-05-13
        • 2021-10-30
        • 2015-12-21
        相关资源
        最近更新 更多