1

Is there another way to send keystrokes to an application?

I have this Powershell script, after pressing the backtick key a console should open in the application but this doesn't happen, but pressing the keyboard the phisical way it works perfectly.

After I open the console by myself the sendkeys do work but the first sendkeys (backtick) doesn't work.

What i need is that the script opens the console by sending the backtick 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}")
Dany
  • 119
  • 2
  • 5
  • 11

3 Answers3

0

First issue I see is you are not referencing System.Windows.Forms and you have an extra single quote in your VisualBasic reference that can't be good.

add-type -AssemblyName System.Windows.Forms

#or

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

Extra single quote here.

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

"Wait" or 'Wait3' is not a ps1 method, try Start-Sleep 1

Start-Sleep 1
Start-Sleep 3
Knuckle-Dragger
  • 6,644
  • 4
  • 26
  • 41
  • as for the backtick, I cannot seem to find any documentation ATM to sendkeys that button. But here is a good read on concerning how we might have to escape it. http://www.rlmueller.net/PowerShellEscape.htm – Knuckle-Dragger Dec 21 '13 at 19:55
  • 1
    I think SendKeys depends on your region-setings(keyboard-layout), so this may vary. I create backtick by holding shift, pressing ` \ ` and hiting space, so to generate it I'll use: `[System.Windows.Forms.SendKeys]::SendWait("+\ ")` . `+` means holding down shift – Frode F. Dec 21 '13 at 21:04
0

as backtick is a special character, try using double backticks. one for escaping the other, this way:

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

0

Backtick is the escape character within double-quoted strings. You should use a single-quoted string instead, since the contents of single-quoted strings are interpreted literally. Also, you don't need braces around the string.

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

See also:

help about_Quoting_Rules
help about_Escape_Characters
favo
  • 69
  • 1
  • 2