how to avoid triggering the OnClipboardChange event when the script itself modifies the clipboard? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys

RussF
Posts: 1311
Joined: 05 Aug 2021, 06:36

Re: how to avoid triggering the OnClipboardChange event when the script itself modifies the clipboard?

Post by RussF » 06 Sep 2023, 08:43

The OnClipboardChange event will be triggered regardless, however, because you are defining your own callback function, you can test for a flag within the function. You simply need to set the flag immediately before filling the clipboard within your script. Run the following code and press F5 to see what happens when you set fill the clipboard programmatically, then copy some text with ^c to see the alternative.

Code: Select all

#Requires AutoHotkey v1.1.33+

ClipFlag    := False
OnClipBoardChange("MyClipFunc")
Return

F5::
    ClipFlag := True
    ClipBoard := "Clipboard set by program"
    Return
    
F10:: ExitApp

MyClipFunc(DataType){
    Global ClipFlag
    If (ClipFlag) {
        ClipFlag := False
        MsgBox % Clipboard
    } Else {
        MsgBox % "Clipboard set outside of program"
    }
}
Russ

geek
Posts: 1055
Joined: 02 Oct 2013, 22:13
Location: GeekDude
Contact:

Re: how to avoid triggering the OnClipboardChange event when the script itself modifies the clipboard?

Post by geek » 06 Sep 2023, 09:15

You can actually check who set the clipboard last, and filter based on that. E.g.

Code: Select all

#Requires AutoHotkey v1.1.33+

OnClipboardChange("MyClipFunc")
return

F5::Clipboard := "Clipboard set by program"

MyClipFunc(DataType) {
    if (DllCall("GetClipboardOwner") == A_ScriptHwnd)
    	return

    MsgBox Clipboard set outside of program
}

RussF
Posts: 1311
Joined: 05 Aug 2021, 06:36

Re: how to avoid triggering the OnClipboardChange event when the script itself modifies the clipboard?

Post by RussF » 06 Sep 2023, 10:00

@geek - nice! Same principle, but saves having to remember to set the flag every time your are going to fill the clipboard.

I looked up the function in Win32 for fun. I am always amazed at how people can find anything in that API. If you don't happen to know the exact function name to search for, you would need to drill down into the API. GetClipboardOwner() is found at Win32/API/Data Exchange/Winuser.h. Everything makes sense up to the Winuser.h part. It seems to be nothing but clipboard functions. Who would think that a section called Winuser would be nothing but clipboard functions? I sure wouldn't. Anyway, thanks for the assist!

Russ

User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: how to avoid triggering the OnClipboardChange event when the script itself modifies the clipboard?

Post by mikeyww » 06 Sep 2023, 10:47

"Disable" OnClipboardChange (stop callback)

Technique #3:

OnClipboardChange():

OnClipboardChange(Callback [, AddRemove])

If needed, the AddRemove parameter can be used to avoid calling the callback.
If omitted, it defaults to 1. Otherwise, specify one of the following numbers:

1 = Call the callback after any previously registered callbacks.
-1 = Call the callback before any previously registered callbacks.
0 = Do not call the callback.

Code: Select all

; This script shows how to "disable" OnClipboardChange (stop callback)
#Requires AutoHotkey v1.1.33
#Persistent
OnClipboardChange("done")

done(dataType) {
 OnClipboardChange("done", False)
 Clipboard := "Clipboard:`n`n" Clipboard
 MsgBox 64, Information, % Clipboard
 OnClipboardChange("done")
}

wer
Posts: 57
Joined: 29 Nov 2022, 21:28

Re: how to avoid triggering the OnClipboardChange event when the script itself modifies the clipboard?  Topic is solved

Post by wer » 06 Sep 2023, 23:46

thank you sirs all three methods are effective

Post Reply

Return to “Ask for Help (v1)”