ControlSend / ControlSendRaw

Sends simulated keystrokes to a window or control.

ControlSend Keys , Control, WinTitle, WinText, ExcludeTitle, ExcludeText
ControlSendRaw: Same parameters as above.

Parameters

Keys

The sequence of keys to send (see the Send function for details). To send a literal comma, escape it (`,). 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.

Control

Can be either ClassNN (the classname and instance number of the control) or the control's text, both of which can be determined via Window Spy. When using text, the matching behavior is determined by SetTitleMatchMode. If this parameter is blank or omitted, the target window's topmost control will be used. If this parameter is "ahk_parent", the keystrokes will be sent directly to the target window instead of one of its controls (see Automating Winamp for an example).

To operate upon a control's HWND (window handle), leave the Control parameter blank and specify "ahk_id " ControlHwnd for the WinTitle parameter (this also works on hidden controls even when DetectHiddenWindows is Off). The HWND of a control is typically retrieved via ControlGetHwnd, MouseGetPos, or DllCall.

WinTitle

A window title or other criteria identifying the target window. See WinTitle.

WinText

If present, this parameter must be a substring from a single text element of the target window (as revealed by the included Window Spy utility). Hidden text elements are detected if DetectHiddenText is ON.

ExcludeTitle

Windows whose titles include this value will not be considered.

ExcludeText

Windows whose text include this value will not be considered.

ErrorLevel

ErrorLevel is set to 1 if there was a problem or 0 otherwise.

Remarks

ControlSendRaw sends the keystrokes in the Keys parameter without translating {Enter} to an ENTER keystroke, ^c to Control-C, etc. For details, see Raw mode. It is also valid to use {Raw} with ControlSend.

If the Control 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.

By default, modifier keystrokes (Control, Alt, Shift, and Win) are sent as they normally would be by the Send 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 the Alt key 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 (Control/Alt/Shift/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 Control 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 are usually more reliable and faster than ControlSend:

ControlEditPaste("This text will be inserted at the caret position.", ControlName, WinTitle)
ControlSetText("This text will entirely replace any current text.", ControlName, WinTitle)

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.

Window titles and text are case sensitive. Hidden windows are not detected unless DetectHiddenWindows has been turned on.

Related

SetKeyDelay, Escape sequences (e.g. `n) , Control functions, ControlGetText, ControlMove, ControlGetPos, ControlClick, ControlSetText, ControlFocus, Send, Automating Winamp

Examples

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"
ControlSendRaw "Notice that {Enter} is not sent as an Enter keystroke with ControlSendRaw.", "Edit1"

Msgbox "Press OK to activate the window to see the result."
WinActivate "ahk_pid " PID	; Show the result.
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.