If WinWait / else suspend problem Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
MauroG
Posts: 60
Joined: 11 Jul 2017, 05:52

If WinWait / else suspend problem

21 Nov 2017, 10:19

I have a script that should change Suspend's behavior according to a software being active or not:

Code: Select all

!+s::
If WinActive("ahk_exe Notepad.exe")
{
	Suspend
	Sleep 500
	Suspend
	Return
}
else
	Suspend
Return
the idea being that if Notepad is active, then the script reactivate itself after half a second, while if Notepad isn't active user has to manually reactivate the script by pressing again the hotkey.

The problem is, while the first part of the script works (i.e., if Notepad is active the script suspends, then reactivates itself), the second part works for suspending it, but it doesn't reactivate itself if I press again !+s. It just doesn't see the hotkey anymore:
008: if WinActive("ahk_exe Notepad.exe")
010: Suspend
011: Sleep,500 (0.50)
012: Suspend (0.03)
013: Return (10.98)
008: if WinActive("ahk_exe Notepad.exe")
016: Suspend
017: Return (12.70)
[and nothing else appears if I press again !+s]
If I use

Code: Select all

!+s::suspend
the code works just fine, so I guess the problem isn't the script being suspended.
User avatar
divanebaba
Posts: 805
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: If WinWait / else suspend problem

22 Nov 2017, 01:12

Hi.
Please have a look to this: https://autohotkey.com/boards/viewtopic.php?f=9&t=40147
User UnReALiTyy has used a very tricky way, to use one key to suspend and unsuspend a script, only by using KeyWait.
Your solution could be like code below (untested): EDIT:Maybe you can implement.

Code: Select all

*Pause::
Sleep, 250
While, GetKeyState("Pause","P") {
exitapp
}
suspend
KeyWait, Pause, D
suspend, off ; originally reload was used, but why not using suspend, off
return
EDIT: why not
Last edited by divanebaba on 23 Nov 2017, 09:44, edited 3 times in total.
Einfach nur ein toller Typ. :mrgreen:
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: If WinWait / else suspend problem

22 Nov 2017, 01:47

You might be better off using the built-in #If commands that were designed for this sort of thing, like so:

Code: Select all

#IfWinActive, ahk_exe Notepad.exe
!+s::
  Suspend
  Sleep 500
  Suspend
Return

#IfWinNotActive, ahk_exe Notepad.exe
!+s::Suspend
That might do exactly the same as what you're currently doing, but give it a try, anyways, since it's sort of the preferred method. Edit: Actually, it does solve your problem.
MauroG
Posts: 60
Joined: 11 Jul 2017, 05:52

Re: If WinWait / else suspend problem

22 Nov 2017, 04:21

Thanks, I'll go that way.

divanebaba: Unfortunately that would require to suspend with one hotkey and unsuspend with another (that wouldn't be usable otherwise while the script is suspended), so it's not useful in my case; thanks anyway.
User avatar
divanebaba
Posts: 805
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: If WinWait / else suspend problem

23 Nov 2017, 09:34

MauroG wrote:... Unfortunately that would require to suspend with one hotkey and unsuspend with another (that wouldn't be usable otherwise while the script is suspended) ...
I'm sorry to tell, that user UnReALiTyy has disclosed a way, to suspend and unsuspend hotkeys with only one button. Sounds impossible, but a tricky use of KeyWait makes this possible. See my post before for code.
Einfach nur ein toller Typ. :mrgreen:
MauroG
Posts: 60
Joined: 11 Jul 2017, 05:52

Re: If WinWait / else suspend problem

23 Nov 2017, 09:42

I'll try this new code (the one you posted before editing didn't work, I'll check this), thanks.
User avatar
divanebaba
Posts: 805
Joined: 20 Dec 2016, 03:53
Location: Diaspora

Re: If WinWait / else suspend problem

23 Nov 2017, 10:08

MauroG wrote:... user has to manually reactivate the script by pressing again the hotkey. ...
... but it doesn't reactivate itself if I press again !+s. It just doesn't see the hotkey anymore: ...
My english is not the best. I've understand, one hotkey for suspending and unsuspending.
Fortunally at same time there was a similar thread at the german forum. My first thougt was, even like yours, that it is not possible, to trigger code with suspended hotkeys.
Developer said, in original,: "... weil bereits laufende Threads nicht pausieren ...", means in english something like "... active threads can not suspend ...".
I think this is very good stuff, don't you think same? :D
Einfach nur ein toller Typ. :mrgreen:
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: If WinWait / else suspend problem

23 Nov 2017, 12:54

MauroG wrote:The problem is, while the first part of the script works (i.e., if Notepad is active the script suspends, then reactivates itself), the second part works for suspending it, but it doesn't reactivate itself if I press again !+s. It just doesn't see the hotkey anymore:
That's the entire purpose of the Suspend command... to not act on hotkeys. Your original script should work if you make the hotkey's first line: Suspend, Permit

Osprey's solution will work too, but will make the hotkeys use the keyboard hook. Depending on your script's purpose you may want to avoid that, although I'm guessing you're using the double suspends to unload and reload the hooks? If not, what are you using it for?
MauroG
Posts: 60
Joined: 11 Jul 2017, 05:52

Re: If WinWait / else suspend problem

23 Nov 2017, 13:02

Nextron wrote:That's the entire purpose of the Suspend command... to not act on hotkeys
I thought that, since

Code: Select all

!+s::Suspend
works both way, it would work also in an if/else statement(but then I found that "Any hotkey/hotstring subroutine whose very first line is Suspend (except Suspend On) will be exempt from suspension". I'll try about permit, thanks.
MauroG
Posts: 60
Joined: 11 Jul 2017, 05:52

Re: If WinWait / else suspend problem

23 Nov 2017, 13:10

Nextron wrote:Your original script should work if you make the hotkey's first line: Suspend, Permit
I wasn't able to do it, not sure where I should put Suspend, Permit.
Depending on your script's purpose you may want to avoid that, although I'm guessing you're using the double suspends to unload and reload the hooks? If not, what are you using it for?
I have a script which disables some combinations (like Alt+Tab) while in some software, but sometimes I need to use them; the idea is to suspend the script just for the time needed to use them once, then having it reactivating itself (so I won't forget to reactivate it manually).
Osprey
Posts: 453
Joined: 18 Nov 2017, 05:50

Re: If WinWait / else suspend problem  Topic is solved

23 Nov 2017, 16:37

MauroG wrote:
Nextron wrote:Your original script should work if you make the hotkey's first line: Suspend, Permit
I wasn't able to do it, not sure where I should put Suspend, Permit.
You put it as the first line, like so (using your original script):

Code: Select all

!+s::
Suspend, Permit
If WinActive("ahk_exe Notepad.exe")
{
	Suspend
	Sleep 500
	Suspend
	Return
}
else
	Suspend
Return
That exempts that hotkey from suspension. In other words, when the script is suspended, that one hotkey will stay working so that you can unsuspend the script.
MauroG
Posts: 60
Joined: 11 Jul 2017, 05:52

Re: If WinWait / else suspend problem

23 Nov 2017, 17:44

That works, thanks; I misunderstood what Suspend, Permit do.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Holarctic, jameswrightesq, wpulford and 413 guests