Can't seem to turn off the onclipboard change function after toggling it on Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
jsong55
Posts: 263
Joined: 30 Mar 2021, 22:02

Can't seem to turn off the onclipboard change function after toggling it on

26 May 2024, 21:37

Code: Select all

on_icon:="C:\Users\user\OneDrive\AHK Scripts\Icon\switch-on.ico"
off_icon:="C:\Users\user\OneDrive\AHK Scripts\Icon\switch-off.ico"
class MainWin
{
    BuildGui()
    {
        global g
        g := GuiExt()

        g.SetFont("cBlack s10","Calibri")
        g.Add("Picture","xm y+0" " w100 h-1" " v" "toggle",off_icon)
        g["toggle"].OnEvent("click",c_toggle)
        c_toggle(ctrl, *)
        {
            if app.settings["check_clipboard"] = "Yes"
            {
                ctrl.value:=off_icon
                app.settings["check_clipboard"]:="No"
                OnClipboardChange(objbindmethod(this,"clip_change_fn"),0)

            }
            else
            {
                ctrl.value:=on_icon
                app.settings["check_clipboard"]:="Yes"
                OnClipboardChange(objbindmethod(this,"clip_change_fn"),1)
            }
            sleep(500)
        }
        
    }
    ShowGui()
    {
        g.show()
    }
    clip_change_fn(*)
    {
        msgbox("detecting clipboards")
    }
}
app:=MainWin()
app.settings:=Map("check_clipboard","No")
app.BuildGui()
app.ShowGui()
I suspect something is off with the way I nest / created the classes
niCode
Posts: 320
Joined: 17 Oct 2022, 22:09

Re: Can't seem to turn off the onclipboard change function after toggling it on

27 May 2024, 01:45

The BoundFunc object created in ObjBindMethod needs to be a reference to the same function. It sounds silly but there's an underlying reference that you don't see when you create a BoundFunc object that is unique. You created two: one in each OnClipboardChange call.

Instead just create a variable above that binds the method and use that in each OnClipboardChange.
jsong55
Posts: 263
Joined: 30 Mar 2021, 22:02

Re: Can't seem to turn off the onclipboard change function after toggling it on  Topic is solved

27 May 2024, 04:16

@niCode
Ah thank you for pointing that out. Additionally I needed to add static to ensure multiple BoundFunc are NOT created

as such

Code: Select all

        static Bound_clip_change_fn:=ObjBindMethod(this, "clip_change_fn")

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: No registered users and 42 guests