OnSuspend?

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Elequosoraptor
Posts: 18
Joined: 11 Oct 2017, 09:04

OnSuspend?

Post by Elequosoraptor » 19 Dec 2022, 04:15

In v2 is there a onSuspend function similar to onExit? It seems this would be pretty useful generally, as well as to me for a script I'm working on, but I can't seem to find any official way of detecting when a script is suspended, or unsuspended.

just me
Posts: 9575
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: OnSuspend?

Post by just me » 19 Dec 2022, 10:55


Elequosoraptor
Posts: 18
Joined: 11 Oct 2017, 09:04

Re: OnSuspend?

Post by Elequosoraptor » 19 Dec 2022, 13:01

Oh yes, I know about that variable, sorry if I wasn't clear. I'm looking for something that will trigger when the script is suspended or unsuspended; while the variable lets you check you'd have to have something like a constant while loop running in the script monitoring when the variable changes, which I don't want to do.

onExit is nice because it calls its given function automatically when the script exits.

User avatar
TheArkive
Posts: 1030
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: OnSuspend?

Post by TheArkive » 19 Dec 2022, 13:59

I'm not aware of any callback for the Suspend command, and the docs don't mention any such callback.

You could make your own wrapper function to do this, though that might be more trouble than it is worth.

ntepa
Posts: 439
Joined: 19 Oct 2022, 20:52

Re: OnSuspend?

Post by ntepa » 19 Dec 2022, 15:37

Code: Select all

SuspendC := Suspend.GetMethod("Call")
Suspend.DefineProp("Call", {
	Call:(this, mode:=-1) => (SuspendC(this, mode), OnSuspend(A_IsSuspended ? 1 : 0))
})

OnSuspend(mode) {
	if mode = 1
	{
		MsgBox "Suspend is on"
	}
	else if mode = 0
	{
		MsgBox "Suspend is off"
	}
}

#SuspendExempt true

1::Suspend 0
2::Suspend 1
3::Suspend -1

#SuspendExempt false

User avatar
TheArkive
Posts: 1030
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: OnSuspend?

Post by TheArkive » 19 Dec 2022, 15:47

@ntepa

:bravo:

I was trying to figure out how to do that. Thanks for posting.

Elequosoraptor
Posts: 18
Joined: 11 Oct 2017, 09:04

Re: OnSuspend?

Post by Elequosoraptor » 19 Dec 2022, 19:50

Thanks for the help! That wrapper generally works, though it misses suspensions done from the tray icon and main window unfortunately. Probably the best solution for now though.

ntepa
Posts: 439
Joined: 19 Oct 2022, 20:52

Re: OnSuspend?

Post by ntepa » 19 Dec 2022, 21:28

Now it detects if you suspended from the tray, or main window.

Code: Select all

SuspendC := Suspend.GetMethod("Call")
Suspend.DefineProp("Call", {
    Call:(this, mode:=-1) => (SuspendC(this, mode), OnSuspend(A_IsSuspended))
})
OnMessage(0x111, OnSuspendMsg)
OnSuspendMsg(wp, *) {
    if (wp = 65305) || (wp = 65404)
        OnSuspend(!A_IsSuspended)
}

OnSuspend(mode) {
    if mode = 1
    {
        MsgBox "Suspend is on"
    }
    else if mode = 0
    {
        MsgBox "Suspend is off"
    }
}

#SuspendExempt true

1::Suspend 0
2::Suspend 1
3::Suspend -1

#SuspendExempt false
I got the numbers 65305 and 65404 using:

Code: Select all

OnMessage(0x111, WM_COMMAND)

WM_COMMAND(wparam, lparam, msg, hwnd) {
    OutputDebug "wp: " wparam " | lp: " lparam "`n"
}
Last edited by ntepa on 19 Dec 2022, 21:59, edited 2 times in total.

Elequosoraptor
Posts: 18
Joined: 11 Oct 2017, 09:04

Re: OnSuspend?

Post by Elequosoraptor » 19 Dec 2022, 21:47

That's crazy! And works perfectly! Thanks for this.

ntepa
Posts: 439
Joined: 19 Oct 2022, 20:52

Re: OnSuspend?

Post by ntepa » 19 Dec 2022, 21:59

I made some edits. The suspend state was switched.

User avatar
thqby
Posts: 433
Joined: 16 Apr 2021, 11:18
Contact:

Re: OnSuspend?

Post by thqby » 19 Dec 2022, 22:40

Suspend.GetMethod("Call") == Func.Prototype.Call
so you can use (Func.Prototype.Call)(this, ...).

ntepa
Posts: 439
Joined: 19 Oct 2022, 20:52

Re: OnSuspend?

Post by ntepa » 19 Dec 2022, 23:25

like this? @thqby

Code: Select all

Suspend.DefineProp("Call", {
    Call:(this, mode:=-1) => ((Func.Prototype.Call)(this, mode), OnSuspend(A_IsSuspended))
})

User avatar
TheArkive
Posts: 1030
Joined: 05 Aug 2016, 08:06
Location: The Construct
Contact:

Re: OnSuspend?

Post by TheArkive » 20 Dec 2022, 04:31

Very cool @thqby.

Thanks.

Post Reply

Return to “Ask for Help (v2)”