PostMessage / SendMessage

Sends a message to a window or control (SendMessage additionally waits for acknowledgement).

PostMessage, MsgNumber , wParam, lParam, Control, WinTitle, WinText, ExcludeTitle, ExcludeText
SendMessage, MsgNumber , wParam, lParam, Control, WinTitle, WinText, ExcludeTitle, ExcludeText, Timeout

Parameters

MsgNumber

The message number to send, which can be an expression. See the message list to determine the number.

wParam

If blank or omitted, 0 will be sent. Otherwise, specify the first component of the message, which can be an expression.

lParam

If blank or omitted, 0 will be sent. Otherwise, specify the second component of the message, which can be an expression.

Control

If blank or omitted, the message will be sent directly to the target window rather than one of its controls. Otherwise, specify 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.

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 ControlGet Hwnd, MouseGetPos, or DllCall().

WinTitle, WinText, ExcludeTitle, ExcludeText

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.

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. By default, a window title must start with the specified WinTitle or ExcludeTitle to be a match, unless changed with SetTitleMatchMode.

Timeout [AHK_L 42+]

If blank or omitted, it defaults to 5000, which is also the default behaviour in older versions of AutoHotkey which did not support this parameter. Otherwise, specify the maximum number of milliseconds to wait for the target window to process the message. If the message is not processed within this time, the command finishes and sets ErrorLevel to the word FAIL. Specify 0 to wait indefinitely. A negative number causes SendMessage to time out immediately. This parameter can be an expression.

ErrorLevel

[v1.1.04+]: These commands are able to throw an exception on failure. For more information, see Runtime Errors.

PostMessage: ErrorLevel is set to 1 if there was a problem such as the target window or control not existing. Otherwise, it is set to 0.

SendMessage: ErrorLevel is set to the word FAIL if there was a problem or the command timed out. Otherwise, it is set to the numeric result of the message, which might sometimes be a "reply" depending on the nature of the message and its target window.

The range of possible values depends on the target window and the version of AutoHotkey that is running. When using a 32-bit version of AutoHotkey, or if the target window is 32-bit, the result is a 32-bit unsigned integer between 0 and 4294967295. When using the 64-bit version of AutoHotkey with a 64-bit window, the result is a 64-bit signed integer between -9223372036854775808 and 9223372036854775807.

If the result is intended to be a 32-bit signed integer (a value from -2147483648 to 2147483648), it can be truncated to 32-bit and converted to a signed value as follows:

MsgReply := ErrorLevel << 32 >> 32

This conversion may be necessary even on AutoHotkey 64-bit, because results from 32-bit windows are zero-extended. For example, a result of -1 from a 32-bit window is seen as 0xFFFFFFFF on any version of AutoHotkey, whereas a result of -1 from a 64-bit window is seen as 0xFFFFFFFF on AutoHotkey 32-bit and -1 on AutoHotkey 64-bit.

Remarks

These commands should be used with caution because sending a message to the wrong window (or sending an invalid message) might cause unexpected behavior or even crash the target application. This is because most applications are not designed to expect certain types of messages from external sources.

PostMessage places the message in the message queue associated with the target window and does not wait for acknowledgement or reply. By contrast, SendMessage waits for the target window to process the message, up until the timeout period expires.

The wParam and lParam parameters should be integers. If AutoHotkey or the target window is 32-bit, only the low 32 bits are used; that is, the value should be between -2147483648 and 4294967295 (0xFFFFFFFF). If AutoHotkey and the target window are both 64-bit, any integer value supported by AutoHotkey can be used. As with all integer values in AutoHotkey, a prefix of 0x indicates a hex value. For example, 0xFF is equivalent to 255.

A string may be sent via wParam or lParam by specifying the address of a variable. The following example uses the address operator (&) to do this:

SendMessage, 0x000C, 0, &MyVar, ClassNN, WinTitle  ; 0x000C is WM_SETTEXT

[v1.0.43.06+]: A string put into MyVar by the receiver of the message is properly recognized without the need for extra steps. However, this works only if the parameter's first character is an ampersand (&); for example, &MyVar or &MyVar+5 would work, but not 5+&MyVar.

A quoted/literal string may also be sent as in the following working example (the & operator should not be used in this case):

#Persistent
ListVars
WinWaitActive, ahk_class AutoHotkey
SendMessage, 0x000C, 0, "New Title"  ; 0x000C is WM_SETTEXT

To send a message to all windows in the system, including those that are hidden or disabled, specify ahk_id 0xFFFF for WinTitle (0xFFFF is HWND_BROADCAST). This technique should be used only for messages intended to be broadcast, such as the following example:

SendMessage, 0x001A,,,, ahk_id 0xFFFF  ; 0x001A is WM_SETTINGCHANGE

To have a script receive a message, use OnMessage().

See the Message Tutorial for an introduction to using these commands.

Message List, Message Tutorial, OnMessage(), Automating Winamp, DllCall(), ControlSend, WinMenuSelectItem

Examples

Turns off the monitor via hotkey. In the SendMessage line, replace the number 2 with -1 to turn on the monitor, or replace it with 1 to activate the monitor's low-power mode.

#o:: ; Win+O
Sleep 1000  ; Give user a chance to release keys (in case their release would wake up the monitor again).
; Turn Monitor Off:
SendMessage, 0x0112, 0xF170, 2,, Program Manager  ; 0x0112 is WM_SYSCOMMAND, 0xF170 is SC_MONITORPOWER.
return

Starts the user's chosen screen saver.

SendMessage, 0x0112, 0xF140, 0,, Program Manager  ; 0x0112 is WM_SYSCOMMAND, and 0xF140 is SC_SCREENSAVE.

Scrolls up by one line (for a control that has a vertical scroll bar).

ControlGetFocus, control, A
SendMessage, 0x0115, 0, 0, %control%, A

Scrolls down by one line (for a control that has a vertical scroll bar).

ControlGetFocus, control, A
SendMessage, 0x0115, 1, 0, %control%, A

Switches the active window's keyboard layout/language to English (US).

PostMessage, 0x0050, 0, 0x4090409,, A  ; 0x0050 is WM_INPUTLANGCHANGEREQUEST.

Asks Winamp which track number is currently active (see Automating Winamp for more information).

SetTitleMatchMode, 2
SendMessage, 0x0400, 0, 120,, - Winamp
if (ErrorLevel != "FAIL")
{
    ErrorLevel++  ; Winamp's count starts at "0", so adjust by 1.
    MsgBox, Track #%ErrorLevel% is active or playing.
}

Finds the process ID of an AHK script (an alternative to WinGet PID).

SetTitleMatchMode, 2
DetectHiddenWindows, On
SendMessage, 0x0044, 0x405, 0, , SomeOtherScript.ahk - AutoHotkey v
MsgBox %ErrorLevel% is the process id.