OnClipboardChange

AutoHotkey GUI

OnClipboardChange

OnClipboardChange() [v1.1.20+]

Registers a function or function object to run whenever the clipboard's content changes.

OnClipboardChange(Func , AddRemove)

Parameters

Func

A function name or function object to call. The function's parameter and return value are described below.

AddRemove

One of the following values:
1 (the default): Call the function after any previously registered functions.
-1: Call the function before any previously registered functions.
0: Do not call the function.

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

Func

FunctionName(Type)
Type

Contains one of the following values:
0 if the clipboard is now empty;
1 if it contains something that can be expressed as text (this includes files copied from an Explorer window);
2 if it contains something entirely non-text such as a picture.

Return Value

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

Example

This example is equivalent to the one above, except that the function is not called when the script first starts; only when the content of the Clipboard changes.

#Persistent
OnClipboardChange("ClipChanged")
return

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

OnClipboardChange Label

Deprecated: This approach is not recommended for use in new scripts. Use the OnClipboardChange function 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:
0 if the clipboard is now empty;
1 if it contains something that can be expressed as text (this includes files copied from an Explorer window);
2 if it contains something entirely non-text such as a picture.

The following example is a working script. Whenever it is running, it will briefly display a ToolTip for each clipboard change.

#Persistent
return

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

Remarks

If the clipboard changes while an OnClipboardChange function or label is already running, that notification event is lost. If this is undesirable, specify Critical as the label's first line. 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, its OnClipboardChange function or label is typically not executed immediately; that is, commands immediately below the command that changed the clipboard are likely to execute beforehand. To force the function or label to execute immediately, use a short delay such as Sleep 20 after changing the clipboard.

Related

Clipboard, OnExit, OnMessage(), RegisterCallback()