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, Timeout



The message number to send. See the message list to determine the number.

wParam, lParam

The message parameters. If omitted, each parameter defaults to 0.

Each parameter can be a pure integer or a string. If it is a string, the address of the string is sent (even if it is a numeric string). If it is a variable reference such as myBuf and that variable contains a string (or is empty), the variable's length is updated afterward in case the target window modified it.


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 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 ControlGetHwnd, MouseGetPos, or DllCall.


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


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.


Windows whose titles include this value will not be considered.


Windows whose text include this value will not be considered.


The maximum number of milliseconds to wait for the target window to process the message. If omitted, it defaults to 5000 (milliseconds), which is also the default behaviour in older versions of AutoHotkey which did not support this parameter. If the message is not processed within this time, the function finishes and sets ErrorLevel to 1.

Return Value

PostMessage returns an empty string.

SendMessage returns an empty string if there was a problem or the message timed out. Otherwise, it returns 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 := MsgReply << 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.


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


These functions 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 for the target window to process the message, up until the timeout period expires.

The wParam and lParam parameters should usually be pure 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.

If wParam or lParam is a string or variable containing a string (even an empty or numeric string), the address of the string or variable is sent. For example:

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 functions.

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


Message List, Message Tutorial, OnMessage, Automating Winamp, DllCall, ControlSend, MenuSelect


#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.

; 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
track := SendMessage(1024, 0, 120,, "- Winamp")
if !ErrorLevel
    track++  ; Winamp's count starts at 0, so adjust by 1.
    MsgBox "Track #" track " is active or playing."
; See Automating Winamp for more information.

; To find the process ID of an AHK script (an alternative to WinGetPID):
SetTitleMatchMode 2
DetectHiddenWindows true
pid := SendMessage(0x44, 0x405, 0, , "SomeOtherScript.ahk - AutoHotkey v")
MsgBox pid " is the process id."