OnClipboardChange

OnClipboardChange can be used as a function or label, although the former is recommended for new scripts. As a function, it registers a function to be called automatically whenever the clipboard's content changes. As a label, it is launched automatically whenever the clipboard's content changes.

OnClipboardChange Function [v1.1.20+]

Registers a function to be called automatically whenever the clipboard's content changes.

OnClipboardChange(Callback , AddRemove)

Parameters

Callback

A function name or function object to call. To pass a literal function name, enclose it in quotes.

The callback accepts one parameter and can be defined as follows:

MyCallback(DataType) { ...

Although the name you give the parameter does not matter, it is assigned one of the following numbers:

You can omit the callback's parameter if the corresponding information is not needed.

If this is the last or only callback, the return value is ignored. Otherwise, it can return a non-zero integer to prevent subsequent callbacks from being called.

AddRemove

If omitted, it defaults to 1. Otherwise, specify one of the following numbers:

If the OnClipboardChange label exists, it is always called first.

OnClipboardChange Label

Deprecated: The OnClipboardChange label is not recommended for use in new scripts. Use the OnClipboardChange function described above instead.

A label named OnClipboardChange (if it exists) is launched automatically whenever any application (even the script itself) has changed the contents of the clipboard. The label also runs once when the script first starts.

The built-in variable A_EventInfo contains one of the following numbers:

Remarks

If the clipboard changes while a callback or label is already running, that notification event is lost. If this is undesirable, use Critical. However, this will also buffer/defer other threads (such as the press of a hotkey) that occur while the OnClipboardChange thread is running.

If the script itself changes the clipboard, the callback or label is typically not executed immediately; that is, statements immediately below the statement that changed the clipboard are likely to execute beforehand. To force the callback or label to execute immediately, use a short delay such as Sleep 20 after changing the clipboard.

Clipboard, OnExit, OnMessage(), RegisterCallback()

Examples

Function vs. Label.

Despite the different syntax, both examples have the same effect: They briefly display a tooltip for each clipboard change. Note: Unlike the label, the callback does not run once when the script first starts, but only when the clipboard's content has changed.

#Persistent
OnClipboardChange("ClipChanged")
return

ClipChanged(DataType) {
    ToolTip Clipboard data type: %DataType%
    Sleep 1000
    ToolTip  ; Turn off the tip.
}
#Persistent
return

OnClipboardChange:
ToolTip Clipboard data type: %A_EventInfo%
Sleep 1000
ToolTip  ; Turn off the tip.
return

Same as above but with a function object.

#Persistent
OnClipboardChange(ClipChanged)
return

class ClipChanged {
    Call(DataType) {
        ToolTip Clipboard data type: %DataType%
        Sleep 1000
        ToolTip  ; Turn off the tip.
    }
}