ControlSend / ControlSendRaw

Sends simulated keystrokes to a window or control.

ControlSend [, Control, Keys, WinTitle, WinText, ExcludeTitle, ExcludeText]

Note: ControlSendRaw uses the same parameters.

 

Parameters

Control Can be either ClassNN (the classname and instance number of the control) or the name/text of the control, both of which can be determined via Window Spy. When using name/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 control's parent window (see Automating Winamp for an example).
Keys

The sequence of keys to send (see the Send command for details). To send a literal comma, escape it (`,). The rate at which characters are sent is determined by SetKeyDelay.

Unlike the Send command, mouse clicks cannot be sent by ControlSend.

WinTitle The title or partial title of the target window (the matching behavior is determined by SetTitleMatchMode). If this and the next 3 parameters are omitted, the Last Found Window will be used. If this is the letter A and the next 3 parameters are omitted, the active window will be used. To use a window class, specify ahk_class ExactClassName (shown by Window Spy). To use a process identifier (PID), specify ahk_pid %VarContainingPID%. To use a window group, specify ahk_group GroupName. To use a window's unique ID number, specify ahk_id %VarContainingID%. The search can be narrowed by specifying multiple criteria. For example: My File.txt ahk_class Notepad
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 exactly as they appear rather than translating {Enter} to an ENTER keystroke, ^c to Control-C, etc.

If the Control parameter is omitted, this command 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 command. 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, Edit1, {Alt down}f{Alt up}, 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:
Control, EditPaste, This text will be inserted at the caret position., ControlName, WinTitle
ControlSetText, ControlName, This text will entirely replace any current text., WinTitle

ControlSend is generally not capable of manipulating a window's menu bar. To work around this, use WinMenuSelectItem. 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 always case sensitive. Hidden windows are not detected unless DetectHiddenWindows has been turned on.

 

Related

SetKeyDelay, Control, ControlGet, ControlGetText, ControlMove, ControlGetPos, ControlClick, ControlSetText, ControlFocus, Send, Automating Winamp

 

Examples

ControlSend, Edit1, This is a line of text in the notepad window., Untitled

SetTitleMatchMode, 2
ControlSend, , abc, cmd.exe ; Send directly to a command prompt window.