Registers a function or method to be called when a control notification is received via the WM_COMMAND message.

GuiControl.OnCommand(NotifyCode, Callback , AddRemove := 1)



Type: Object

The GuiControl object of the control to monitor.


Type: Integer

The control-defined notification code to monitor.


Type: String or Function Object

The function, method or object to call when the event is raised.

If the GUI has an event sink (that is, if Gui()'s EventObj parameter was specified), this parameter may be the name of a method belonging to the event sink.

Otherwise, this parameter must be a function object.


Type: Integer

If blank or omitted, it defaults to 1 (call the callback after any previously registered callbacks). Otherwise, specify one of the following numbers:


Certain types of controls send a WM_COMMAND message whenever an interesting event occurs. These are usually standard Windows controls which have been around a long time, as newer controls use the WM_NOTIFY message (see OnNotify). Commonly used notification codes are translated to events, which the script can monitor with OnEvent.

The message's parameters contain the control ID, HWND and notification code, which AutoHotkey uses to dispatch the notification to the appropriate callback. There are no additional parameters.

To determine which notifications are available (if any), refer to the control's documentation. Control Library (MSDN) contains links to each of the the Windows common controls (however, only a few of these use WM_COMMAND). The notification codes (numbers) can be found in the Windows SDK, or by searching the Internet.

Callback Parameters

The notes for OnEvent regarding this and bound functions also apply to OnCommand.

The callback receives one parameter:


Callback Return Value

If multiple callbacks have been registered for an event, a callback may return a non-empty value to prevent any remaining callbacks from being called.

The return value is ignored by the control.

These notes for OnEvent also apply to OnCommand: Threads, Destroying the GUI.

OnNotify can be used for notifications which are sent as a WM_NOTIFY message.