' sendkey.vbs — waits, escapes safely, types, optional Enter
Option Explicit
Dim sh, text, enterAfter, title, delayMs, debugMode, i, arg
Set sh = CreateObject("WScript.Shell")
delayMs = 5000
' --- Parse args ---
For i = 0 To WScript.Arguments.Count - 1
arg = WScript.Arguments(i)
If LCase(Left(arg, 7)) = "/title:" Then
title = Mid(arg, 8)
ElseIf LCase(Left(arg, 7)) = "/delay:" Then
delayMs = CLng(Mid(arg, 8))
ElseIf LCase(arg) = "/enter" Then
enterAfter = True
ElseIf LCase(arg) = "/debug" Then
debugMode = True
Else
If text = "" Then text = arg Else text = text & " " & arg
End If
Next
If text = "" Then
WScript.Echo "Usage: cscript //nologo sendkey.vbs ""your text"" [/enter] [/title:""Window Title""] [/delay:5000] [/debug]"
WScript.Quit 1
End If
If Len(title) > 0 Then
On Error Resume Next
sh.AppActivate title
On Error GoTo 0
WScript.Sleep 300
End If
Dim toSend : toSend = EscapeForSendKeys(text)
If debugMode Then WScript.Echo "Escaped: " & toSend
WScript.Echo "Click target box... typing in " & CStr(delayMs) & " ms"
WScript.Sleep delayMs
sh.SendKeys toSend
If enterAfter Then sh.SendKeys "~"
' --- Fixed: robust escaping with placeholders to avoid double-escaping ---
Function EscapeForSendKeys(s)
' Escape modifier/meta chars first
s = Replace(s, "%", "{%}") ' Alt
s = Replace(s, "^", "{^}") ' Ctrl
s = Replace(s, "+", "{+}") ' Shift
s = Replace(s, "~", "{~}") ' Enter symbol
' Use placeholders so new braces aren't re-escaped
Dim L, R: L = Chr(1): R = Chr(2)
s = Replace(s, "{", L)
s = Replace(s, "}", R)
s = Replace(s, L, "{{}") ' literal {
s = Replace(s, R, "{}}") ' literal }
EscapeForSendKeys = s
End Function
💻 Examples
1️⃣ Basic example:
cscript //nologo SafeType.vbs "MyP@ss;word123"
2️⃣ Add Enter afterward:
cscript //nologo SafeType.vbs "MyP@ss;word123" /enter
3️⃣ Focus a specific window first (exact title match):
cscript //nologo SafeType.vbs "MyP@ss;word123}}" /enter /title:"Untitled - Notepad"
4️⃣ From PowerShell:
Start-Process "cscript.exe" -ArgumentList '//nologo "C:\Scripts\SafeType.vbs" "MyP@ss;word123}}" /enter /title:"Untitled - Notepad"'