Search code examples
powershellsendkeys

Escaping braces doesn't seem to work in SendKeys


I do not get {} when sending keys to VSCode:

$wshell = New-Object -ComObject wscript.shell;
$wshell.AppActivate('Untitled-1')
Sleep 1
$wshell.SendKeys('`{`}')
Sleep 1
$wshell.SendKeys('Hello World 2~')

enter image description here


Solution

  • It's not PowerShell you have to escape your braces from in this case. It's SendKeys.

    To use literal braces in SendKeys you have surround them in braces like other special characters.

    $wshell.SendKeys('{{}{}}')
    

    From MSDN

    The plus sign (+), caret (^), percent sign (%), tilde (~), and parentheses () have special meanings to SendKeys. To specify one of these characters, enclose it within braces ({}). For example, to specify the plus sign, use "{+}". To specify brace characters, use "{{}" and "{}}". Brackets ([ ]) have no special meaning to SendKeys, but you must enclose them in braces. In other applications, brackets do have a special meaning that might be significant when dynamic data exchange (DDE) occurs.

    emphasis mine.