Sends simulated keystrokes or text to a window or control.
ControlSend Keys , ControlID, WinTitle, WinText, ExcludeTitle, ExcludeText ControlSendText Keys , ControlID, WinTitle, WinText, ExcludeTitle, ExcludeText
Note: On Linux, window targets are limited to the script's own windows and controls. ControlSendText also supports local text consoles. See Linux for input timing and requirements. On macOS, only ControlSendText to the script's own Edit controls is supported.
Type: String
The sequence of keys to send (see the Send function for details). The rate at which characters are sent is determined by SetKeyDelay.
Unlike the Send function, mouse clicks cannot be sent by ControlSend. Use ControlClick for that.
For a Linux text-console target, Keys must contain valid Unicode text of at most 1024 UTF-8 bytes after newline normalization. CRLF pairs and LF characters become CR; a backspace uses the console's configured erase character, and tabs remain tabs. SetKeyDelay and its press duration do not apply. Empty text validates the target syntax and window criteria locally without contacting the service.
Type: String, Integer or Object
If omitted, the keystrokes will be sent directly to the target window instead of one of its controls. Otherwise, specify the control's ClassNN, text or HWND, or an object with a Hwnd property. For details, see Control Identifiers.
On Linux, ControlSendText also accepts an absolute local console path from /dev/tty1 through /dev/tty63. This target can be inactive and must belong to a logged-in session of the same user. See Local text consoles for prerequisites. ControlSend does not support these targets.
Type: String, Integer or Object
If each of these is blank or omitted, the Last Found Window will be used. Otherwise, specify for WinTitle a window title or other criteria to identify the target window and/or for WinText a substring from a single text element of the target window (as revealed by the included Window Spy utility).
ExcludeTitle and ExcludeText can be used to exclude one or more windows by their title or text. Their specification is similar to WinTitle and WinText, except that ExcludeTitle does not recognize any criteria other than the window title.
For a Linux text-console target, all four parameters must be blank or omitted. The Last Found Window is not used.
Window titles and text are case-sensitive. By default, hidden windows are not detected and hidden text elements are detected, unless changed with DetectHiddenWindows and DetectHiddenText. When using pure HWNDs, hidden windows are always detected. By default, a window title can contain WinTitle or ExcludeTitle anywhere inside it to be a match, unless changed with SetTitleMatchMode.
A TargetError is thrown if the window or control could not be found.
An UnsupportedError is thrown for another program's window or control on Linux, for ControlSend on macOS, or for a macOS ControlSendText target other than the script's own Edit controls.
For Linux text-console targets, invalid paths, nonempty window criteria, invalid Unicode or excessive text length raise ValueError. A missing or disconnected console device raises TargetError; an unavailable service or authorization mechanism, or an unsupported console mode, raises UnsupportedError. Permission denials, including a target without a qualifying logged-in session, and operating-system or communication failures during submission raise OSError. An error may follow partial submission, and a communication failure can leave the outcome unknown.
ControlSendText is similar to ControlSend, except that it sends the individual characters of the Keys parameter without translating {Enter} to Enter, ^c to Ctrl+C, etc. For details, see Text mode. It is also valid to use {Raw} or {Text} with ControlSend.
On Windows, this function is intended for use with controls in a non-GUI window, i.e. a window that is not created with the Gui function. It works best with common or predefined Microsoft controls; some applications use custom or modified controls, in which case the function might not work as expected. For GUI controls, it is usually more convenient to manipulate them via their GuiControl object, assuming a suitable counterpart exists.
Unlike Send, ControlSend ignores SendMode. It uses the operating system's PostMessage function to post keyboard messages.
If the ControlID parameter is omitted, this function will attempt to send directly to the target window by sending to its topmost control (which is often the correct one) or the window itself if there are no controls. This is useful if a window does not appear to have any controls at all, or just for the convenience of not having to worry about which control to send to.
On Windows, modifier keystrokes (Ctrl, Alt, Shift, and Win) are sent as they normally would be by the SendEvent function. This allows command prompt and other console windows to properly detect uppercase letters, control characters, etc. It may also improve reliability in other ways.
However, in some cases these modifier events may interfere with the active window, especially if the user is actively typing during a ControlSend or if Alt is being sent (since Alt activates the active window's menu bar). This can be avoided by explicitly sending modifier up and down events as in this example:
ControlSend "{Alt down}f{Alt up}", "Edit1", "Untitled - Notepad"
The method above also allows the sending of modifier keystrokes (Ctrl, Alt, Shift, and Win) while the workstation is locked (protected by logon prompt).
BlockInput should be avoided when using ControlSend against a console window such as command prompt. This is because it might prevent capitalization and modifier keys such as Ctrl from working properly.
The value of SetKeyDelay determines the speed at which keys are sent. If the target window does not receive the keystrokes reliably, try increasing the press duration via the second parameter of SetKeyDelay as in these examples:
SetKeyDelay 10, 10 SetKeyDelay 0, 10 SetKeyDelay -1, 0
If the target control is an edit control (or something similar), the following functions are usually more reliable and faster than ControlSend:
ControlSend is generally not capable of manipulating a window's menu bar. To work around this, use MenuSelect. If that is not possible due to the nature of the menu bar, you could try to discover the message that corresponds to the desired menu item by following the SendMessage Tutorial.
For window targets on X11 and Wayland, these functions reach only the script's own GTK windows and controls. A target window sends input to its focused control, including while the window is inactive. Sending to another program's window raises UnsupportedError. For accessibility automation, see Controls and accessibility automation.
Input is posted in order to the UI thread. Allow the UI thread to process it, for example with Sleep, before reading the resulting text. Modifier keys affect only the target events and do not change global keyboard state. Edit input respects read-only controls and text-input cancellation. Synthetic edit keys use GTK editing actions and Eto callbacks, so native GTK key signal observers do not receive them.
ControlSendText can send text to a local virtual console without switching to it. It requires the system keysharp-input service and client ABI 1.2 or newer, with terminal input support, plus the InputControl permission. The calling user must own the active session on seat0, and the target must be a logged-in text-console session on that seat belonging to the same user. Desktop services and window permissions are not required. See the Linux installation instructions for component installation and upgrades.
The console must use text display mode, the normal Linux line discipline (N_TTY), Unicode keyboard mode and UTF-8 terminal input mode (IUTF8). A console running a graphical session, a login prompt, another user's session, /dev/tty0, aliases, and pseudo-terminals such as /dev/pts/3 are not supported.
Backspace requires an enabled terminal erase character; otherwise the request raises UnsupportedError before submitting text. Other characters are subject to the terminal's normal input processing. A newline can submit a command to a shell.
Text is submitted to the console's input queue, so the terminal's line discipline and foreground program determine how it is interpreted. Completion means bytes were submitted, not that the program consumed them; a full terminal input buffer can discard text. If an error occurs after part of the text was submitted, that prefix cannot be recalled. Do not retry the whole call automatically.
ControlSendText inserts text at the selection of the script's own single-line, multi-line or password Edit control. A read-only Edit ignores the text.
SetKeyDelay, Escape sequences (e.g. `n) , Control functions, Send, Automating Winamp
Opens Notepad minimized and send it some text. This example may fail on Windows 11 or later, as it requires the classic version of Notepad.
Run "Notepad",, "Min", &PID ; Run Notepad minimized.
WinWait "ahk_pid " PID ; Wait for it to appear.
; Send the text to the inactive Notepad edit control.
; The third parameter is omitted so the last found window is used.
ControlSend "This is a line of text in the notepad window.{Enter}", "Edit1"
ControlSendText "Notice that {Enter} is not sent as an Enter keystroke with ControlSendText.", "Edit1"
Msgbox "Press OK to activate the window to see the result."
WinActivate "ahk_pid " PID ; Show the result.
Opens the command prompt and sent it some text. This example may fail on Windows 11 or later, as it requires the classic version of the command prompt.
SetTitleMatchMode 2
Run A_ComSpec,,, &PID ; Run command prompt.
WinWait "ahk_pid " PID ; Wait for it to appear.
ControlSend "ipconfig{Enter}",, "cmd.exe" ; Send directly to the command prompt window.
Creates a GUI with an edit control and sent it some text.
MyGui := Gui()
MyGui.Add("Edit", "r10 w500")
MyGui.Show()
ControlSend "This is a line of text in the edit control.{Enter}", "Edit1", MyGui
ControlSendText "Notice that {Enter} is not sent as an Enter keystroke with ControlSendText.", "Edit1", MyGui
On Linux, types text at the prompt on TTY3 while the current session remains active. First log in on TTY3 as the same user and meet the console prerequisites. The example omits a newline, so it does not submit the command line.
#ErrorStdOut #Warn All, StdOut ControlSendText "hello", "/dev/tty3"