Having Problems with Suspend Script

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
JOSHSKORN
Posts: 6
Joined: 13 Sep 2020, 18:33

Having Problems with Suspend Script

Post by JOSHSKORN » 02 Jul 2022, 20:59

I'm wiring a script such that I should be able to suspend with ALT+F7 and resume with ALT+F8. All the script does is stop me from pressing the letter P. This is part of a much larger script which works fine, so this is just for example purposes.

Here's what's suppose to happen:
Suspend Script
  • Press ALT+F7 to Suspend Script
  • Prompt For Password. Repeat until correct.
  • If correct password,
  • Show a message
  • Suspend Script
  • Set the password answer back to 0 (password is actually 0000 in this case)
Resume Script
[*]Press ALT+F8 to Resume Script
[*]Turn off Suspend Script
[*]Show a message that script is no longer suspended.

Here's what's actually happening:
  • Press ALT+F7 to turn off script - OK
  • Press ALT+F8 to turn off Script. Script turns off OK but does not show a message.
  • Press ALT+F7 to turn off script (Second time). Does not prompt for a password. Script just suspends. I noticed mypw is now equal to whatever I set it, in this case, mypw is 0.
    Step 1 runs OK. Steps 2 and 3 run identical for the next several times of turning the script off and on (ALT+F7 and ALT+F8).
Problems:
  • Not being prompted for password second time even though set mypw to something other than the password within the script.
  • Message not being displayed when resuming password.
Here's my code:

Code: Select all

p::Return

!F7::
while(mypw != 0000)
Inputbox,mypw,Pause Script, Enter Password,HIDE
if(mypw=0000)
{
MsgBox, %mypw%
mypw=0
Suspend
Return
}


!F8::Suspend, Off
Return
MsgBox,Continue
Return
What am I doing wrong? Please advise. Thank you.

User avatar
boiler
Posts: 16955
Joined: 21 Dec 2014, 02:44

Re: Having Problems with Suspend Script

Post by boiler » 02 Jul 2022, 21:06

You need to define a code block using { } associated with the while statement. Otherwise the only line that loops with the while statement is the one immediately following it.

You should also put a return at the end of the hotkey subroutine after the if code block.

The MsgBox near the bottom will never be executed when you precede it with a return. Even if you removed the return above it, it still won’t execute since you put the Suspend, Off on the same line as the hotkey. When you do that, there is an implicit return and nothing below it will execute. Multiple-line hotkey subroutines must start on the line below the hotkey.

JOSHSKORN
Posts: 6
Joined: 13 Sep 2020, 18:33

Re: Having Problems with Suspend Script

Post by JOSHSKORN » 03 Jul 2022, 01:04

After your comments, I modified the code:

Code: Select all

x::Return

!F7::
while(mypw != 0000)
{
Inputbox,mypw,Pause Script, Enter Password,HIDE
}
if(mypw=0000)
{
MsgBox, %mypw%
mypw=0
Suspend
Return
}
Return

!F8::Suspend, Off
Return
MsgBox,Continue Script
Return
I changed the code so the test keypress is "x", since "p" is in Notepad.

The Suspend and Suspend off toggling works fine (ALT-F7 and ALT-F8, respectively)

ALT-F7 will only prompt for a password the first time. It will otherwise show whatever variable I set it to. It will still enable/disable the lock, regardless if I re-enter the password for the next time or not, which is not good, obviously.

What should I do next to figure out how to fix/complete this scrip such in a manner that I need?


[Mod edit: Added [code][/code] tags]

User avatar
boiler
Posts: 16955
Joined: 21 Dec 2014, 02:44

Re: Having Problems with Suspend Script

Post by boiler » 03 Jul 2022, 04:02

By adding the braces around a single command after the while statement, it doesn’t change anything. It’s as if they’re not there because that’s how it acts without them. But I guess that’s what you want, so you might as well remove them.

When you check the password vs 0000, it’s actually an integer, so it’s the same as 0. If you want it to check vs. 0000, you need to put quotation marks around it in both places.

JOSHSKORN
Posts: 6
Joined: 13 Sep 2020, 18:33

Re: Having Problems with Suspend Script

Post by JOSHSKORN » 03 Jul 2022, 08:21

When you check the password vs 0000, it’s actually an integer, so it’s the same as 0. If you want it to check vs. 0000, you need to put quotation marks around it in both places.
@boiler
Thanks for that. I'm not sure why I didn't think of that, regarding 0 and 0000 being equal. Brain fart. I'd been working on this too long.

Final script, minus the line showing the password in the message box, I'm just leaving this here for others with the same question.

Code: Select all

x::Return

!F7::
while(mypw != "0000")
{
Inputbox,mypw,Pause Script, Enter Password,HIDE
}
if(mypw="0000")
{
MsgBox, %mypw%
mypw=0
Suspend
Return
}
Return

!F8::Suspend, Off
Return
MsgBox,Continue Script
Return
Is it perfect? I'm not totally sure, but it works. What I mean by that is, I could very well have some erroneous lines, to include unnecessary braces for just one line in the while block. Or, there could be shorter ways of accomplishing the same thing. If that's the case, I'm all ears/eyes.

Post Reply

Return to “Ask for Help (v1)”