Sendkeys Powershell not working properly

Is there any other way to send keystrokes in an application?

I have a Powershell script, after pressing the backtick key, the console should open in the application, but this does not happen, but pressing the keyboard physically, how it works fine.

After I open the console myself, sendkeys work, but the first sendkeys (backtick) do not work.

I need the script to open the console by sending the return line key.

    # 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}")
0
source share
3 answers

The first problem, I see you are not referring to System.Windows.Forms, and you have an extra single quote in your VisualBasic link, which may not be good.

add-type -AssemblyName System.Windows.Forms

#or

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

An extra single quote is here.

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

"" "3" ps1, "- 1"

Start-Sleep 1
Start-Sleep 3
0

, . , :

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

0

Backtick is an escape character in double-quoted strings. Instead, you should use a single-quoted string, as the contents of single-quoted strings are interpreted literally. Also, you do not need parentheses around the string.

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

See also:

help about_Quoting_Rules
help about_Escape_Characters
0
source

All Articles