# PostMessage / SendMessage

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

PostMessage, Msg [, wParam, lParam, Control, WinTitle, WinText, ExcludeTitle, ExcludeText]
SendMessage, Msg [, wParam, lParam, Control, WinTitle, WinText, ExcludeTitle, ExcludeText]

#### Parameters

Msg | The message number to send, which can be an expression. See the message list to determine the number.
wParam | The first component of the message, which can be an expression. If blank or omitted, 0 will be sent.
lParam | The second component of the message, which can be an expression. If blank or omitted, 0 will be sent.
Control | If this parameter is blank or omitted, the message will be sent directly to the target window rather than one of its controls. Otherwise, this parameter 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. 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 | 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% (also accepts a control's HWND). 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

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. 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. This result is an integer between 0 and 4294967295. If the result is intended to be a signed integer, a negative number can be revealed by following this example: MsgReply := ErrorLevel > 0x7FFFFFFF ? -(~ErrorLevel) - 1 : ErrorLevel

#### 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. It does not wait for acknowledgement or reply. By contrast, SendMessage waits up to 5 seconds 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.

The parameters Msg, wParam, and lParam should all be integers between -2147483648 and 4294967295 (0xFFFFFFFF). 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, 0xC, 0, &MyVar, ClassNN, WinTitle  ; 0XC is WM_SETTEXT

In 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, 5+&MyVar would not work but &MyVar or &MyVar+5 would work.

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

    Run Notepad
    WinWait Untitled - Notepad
    SendMessage, 0xC, 0, "New Notepad Title"  ; 0XC 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, 0x1A,,,, ahk_id 0xFFFF  ; 0x1A is WM_SETTINGCHANGE

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

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

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

#### Related

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

#### Examples

    #o::  ; Win+O hotkey that turns off the monitor.
    Sleep 1000  ; Give user a chance to release keys (in case their release would wake up the monitor again).
    ; Turn Monitor Off:
    SendMessage, 0x112, 0xF170, 2,, Program Manager  ; 0x112 is WM_SYSCOMMAND, 0xF170 is SC_MONITORPOWER.
    ; Note for the above: Use -1 in place of 2 to turn the monitor on.
    ; Use 1 in place of 2 to activate the monitor's low-power mode.
    return

    ; Start the user's chosen screen saver:
    SendMessage, 0x112, 0xF140, 0,, Program Manager  ; 0x112 is WM_SYSCOMMAND, and 0xF140 is SC_SCREENSAVE.

    ; Scroll up by one line (for a control that has a vertical scroll bar):
    ControlGetFocus, control, A
    SendMessage, 0x115, 0, 0, %control%, A

    ; Scroll down by one line:
    ControlGetFocus, control, A
    SendMessage, 0x115, 1, 0, %control%, A

    ; Switch the active window's keyboard layout/language to English:
    PostMessage, 0x50, 0, 0x4090409,, A  ; 0x50 is WM_INPUTLANGCHANGEREQUEST.

    ; This example asks Winamp which track number is currently active:
    SetTitleMatchMode, 2
    SendMessage, 1024, 0, 120, - Winamp
    if ErrorLevel <> FAIL
    {
        ErrorLevel++  ; Winamp's count starts at "0", so adjust by 1.
        MsgBox, Track #%ErrorLevel% is active or playing.
    }
    ; See Automating Winamp for more information.

    ; To find the process ID of an AHK script (an alternative to "WinGet PID"):
    SetTitleMatchMode, 2
    DetectHiddenWindows, on
    SendMessage, 0x44, 0x405, 0, , SomeOtherScript.ahk - AutoHotkey v
    MsgBox %ErrorLevel% is the process id.