Toggle hotkey on/off problem

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Hajdes
Posts: 38
Joined: 30 Jul 2021, 17:29

Toggle hotkey on/off problem

Post by Hajdes » 03 Aug 2021, 10:51

Can you tell me what is wrong in this code and why it isn't working?

Code: Select all

Label:
    Suspend
    ToolTip % A_IsSuspended ? "Script suspended" : "", 400, 0
    if(A_IsSuspended){
        if (GetKeyState("t", "P") || GetKeyState("y", "P"))
            Hotkey, %LabelKey%, Label, Off
        else if (GetKeyState("Enter", "P") || GetKeyState("Escape", "P"))
            Hotkey, %LabelKey%, Label, On
    }
 
return
User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Toggle hotkey on/off problem

Post by mikeyww » 03 Aug 2021, 10:55

What happens when you run the script? What should happen instead? What is the value of each conditional statement & variable? What calls the subroutine?
Hajdes
Posts: 38
Joined: 30 Jul 2021, 17:29

Re: Toggle hotkey on/off problem

Post by Hajdes » 03 Aug 2021, 11:06

mikeyww wrote:
03 Aug 2021, 10:55
What happens when you run the script? What should happen instead? What is the value of each conditional statement & variable? What calls the subroutine?
%LabelKey%=F6

1 way: I click T/Y my script is suspended, F6 don't work I click enter/esc my script is unsuspended, I can use F6 - everything fine
2 way: I click F6 my script is suspended, I click T/Y and until I click enter/esc F6 shouldn't work but if I click F6 it unsuspend script - here is problem

Here is my code what I have in t (in y key is the same code ) key:

Code: Select all

~t::
    Suspend On
    if (A_IsSuspended){
        Hotkey, %LabelKey%, Label, Off
        Loop
        if (GetKeyState("Enter", "P") || GetKeyState("Escape", "P"))
            break     
    }
    Suspend Off
    if (!A_IsSuspended)
    Hotkey, %LabelKey%, Label, On
return
User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Toggle hotkey on/off problem

Post by mikeyww » 03 Aug 2021, 11:12

I don't see F6 anywhere in this script. Debugging is easier if there is a real script to test.
Hajdes
Posts: 38
Joined: 30 Jul 2021, 17:29

Re: Toggle hotkey on/off problem

Post by Hajdes » 03 Aug 2021, 11:13

mikeyww wrote:
03 Aug 2021, 11:12
I don't see F6 anywhere in this script. Debugging is easier if there is a real script to test.
I write above: %LabelKey%=F6
Hajdes
Posts: 38
Joined: 30 Jul 2021, 17:29

Re: Toggle hotkey on/off problem

Post by Hajdes » 03 Aug 2021, 11:17

mikeyww wrote:
03 Aug 2021, 11:12
I don't see F6 anywhere in this script. Debugging is easier if there is a real script to test.
All script:

Code: Select all

Hotkey, %LabelKey%, Label

Label:
    Suspend
    ToolTip % A_IsSuspended ? "Script suspended" : "", 400, 0
    if(A_IsSuspended){
        if (GetKeyState("t", "P") || GetKeyState("y", "P"))
            Hotkey, %LabelKey%, Label, Off
        else if (GetKeyState("Enter", "P") || GetKeyState("Escape", "P"))
            Hotkey, %LabelKey%, Label, On
    }
return

~t::
    Suspend On
    if (A_IsSuspended){
        Hotkey, %LabelKey%, Label, Off
        Loop
        if (GetKeyState("Enter", "P") || GetKeyState("Escape", "P"))
            break     
    }
    Suspend Off
    if (!A_IsSuspended)
    Hotkey, %LabelKey%, Label, On
return

~y::
    Suspend On
    if (A_IsSuspended){
        Hotkey, %LabelKey%, Label, Off
        Loop
        if (GetKeyState("Enter", "P") || GetKeyState("Escape", "P"))
            break     
    }
    Suspend Off
    if (!A_IsSuspended)
    Hotkey, %LabelKey%, Label, On
return
User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Toggle hotkey on/off problem

Post by mikeyww » 03 Aug 2021, 16:39

I am not following your description of what the script should do. In particular, "until I click enter/esc F6 shouldn't work but if I click F6 it unsuspend script" sounds like a contradiction. As you clarify in brief form such as "If _____, then _______; next, _________", etc., please also translate "/" into a word such as "and" or "or", or be more precise about what you are trying to convey. If "t" is setting a state to be checked later, use a variable instead of GetKeyState.
Hajdes
Posts: 38
Joined: 30 Jul 2021, 17:29

Re: Toggle hotkey on/off problem

Post by Hajdes » 09 Aug 2021, 11:35

mikeyww wrote:
03 Aug 2021, 16:39
I am not following your description of what the script should do. In particular, "until I click enter/esc F6 shouldn't work but if I click F6 it unsuspend script" sounds like a contradiction. As you clarify in brief form such as "If _____, then _______; next, _________", etc., please also translate "/" into a word such as "and" or "or", or be more precise about what you are trying to convey. If "t" is setting a state to be checked later, use a variable instead of GetKeyState.
If I click F6 my script is suspended then if I click T key or Y key and I want that F6 is turned off (If I click F6 it can't unsuspend script) until I click enter/esc key.
But in my code is that problem when I click F6 my script is suspended then I click T key or Y key and if I click F6 it unsuspend script.
Do you understand it now?
Last edited by Hajdes on 09 Aug 2021, 12:23, edited 1 time in total.
User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Toggle hotkey on/off problem

Post by mikeyww » 09 Aug 2021, 11:43

Thank you for explaining.

To disable the hotkey that you added, you can add the following to any subroutine where you would like the disabling to occur.

Code: Select all

Hotkey, %LabelKey%, Off
Hajdes
Posts: 38
Joined: 30 Jul 2021, 17:29

Re: Toggle hotkey on/off problem

Post by Hajdes » 09 Aug 2021, 11:47

mikeyww wrote:
09 Aug 2021, 11:43
Thank you for explaining.

To disable the hotkey that you added, you can add the following to any subroutine where you would like the disabling to occur.

Code: Select all

Hotkey, %LabelKey%, Off
I know it and I added it to the label:

Code: Select all

if(A_IsSuspended){
        if (GetKeyState("t", "P") || GetKeyState("y", "P"))
            Hotkey, %LabelKey%, Label, Off
        else if (GetKeyState("Enter", "P") || GetKeyState("Escape", "P"))
            Hotkey, %LabelKey%, Label, On
    }
But it don't want to work
User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Toggle hotkey on/off problem

Post by mikeyww » 09 Aug 2021, 12:04

A demo:

Code: Select all

LabelKey = F6
Gosub, ~Enter

~Esc::
~Enter::
Suspend, Off
Hotkey, %LabelKey%, Label, On
SoundBeep, 1500
Return

~t::
Suspend, Off
Return

Label:
Suspend, On
Hotkey, %LabelKey%, Off
SoundBeep, 1000
Return
Adjust as needed. You can enable or disable suspend wherever you want it to happen.
Hajdes
Posts: 38
Joined: 30 Jul 2021, 17:29

Re: Toggle hotkey on/off problem

Post by Hajdes » 09 Aug 2021, 12:26

Hajdes wrote:
09 Aug 2021, 11:35
mikeyww wrote:
03 Aug 2021, 16:39
I am not following your description of what the script should do. In particular, "until I click enter/esc F6 shouldn't work but if I click F6 it unsuspend script" sounds like a contradiction. As you clarify in brief form such as "If _____, then _______; next, _________", etc., please also translate "/" into a word such as "and" or "or", or be more precise about what you are trying to convey. If "t" is setting a state to be checked later, use a variable instead of GetKeyState.
If I click F6 my script is suspended then if I click T key or Y key and I want that F6 is turned off (If I click F6 it can't unsuspend script) until I click enter/esc key.
But in my code is that problem when I click F6 my script is suspended then I click T key or Y key and if I click F6 it unsuspend script.
Do you understand it now?
Sorry but I didn't add there (I wrote it on start post) that F6 is a toggle which suspend script and unsuspend it, so your code isn't good for me because it only do things above but thank you for that. I gave my code in post above but I will also give it here :

Code: Select all

LabelKey = F6
Hotkey, %LabelKey%, Label

Label:
    Suspend
    ToolTip % A_IsSuspended ? "Script suspended" : "", 400, 0
    if(A_IsSuspended){
        if (GetKeyState("t", "P") || GetKeyState("y", "P"))
            Hotkey, %LabelKey%, Label, Off
        else if (GetKeyState("Enter", "P") || GetKeyState("Escape", "P"))
            Hotkey, %LabelKey%, Label, On
    }
return

~t::
    Suspend On
    if (A_IsSuspended){
        Hotkey, %LabelKey%, Label, Off
        Loop
        if (GetKeyState("Enter", "P") || GetKeyState("Escape", "P"))
            break     
    }
    Suspend Off
    if (!A_IsSuspended)
    Hotkey, %LabelKey%, Label, On
return

~y::
    Suspend On
    if (A_IsSuspended){
        Hotkey, %LabelKey%, Label, Off
        Loop
        if (GetKeyState("Enter", "P") || GetKeyState("Escape", "P"))
            break     
    }
    Suspend Off
    if (!A_IsSuspended)
    Hotkey, %LabelKey%, Label, On
return
User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Toggle hotkey on/off problem

Post by mikeyww » 09 Aug 2021, 16:10

You can change it to a toggle:

Code: Select all

LabelKey = F6
Gosub, ~Enter

~Esc::
~Enter::
Suspend, Permit
Hotkey, %LabelKey%, Label, On
SoundBeep, 1500
Return

~t::
~y::
Suspend, Permit
Hotkey, %LabelKey%, Off
Return

Label:
Suspend
SoundBeep, 1000
Return
GetKeyState reflects an instantaneous assessment of the key state when the command executes.
Hajdes
Posts: 38
Joined: 30 Jul 2021, 17:29

Re: Toggle hotkey on/off problem

Post by Hajdes » 09 Aug 2021, 17:35

mikeyww wrote:
09 Aug 2021, 16:10
You can change it to a toggle:

Code: Select all

LabelKey = F6
Gosub, ~Enter

~Esc::
~Enter::
Suspend, Permit
Hotkey, %LabelKey%, Label, On
SoundBeep, 1500
Return

~t::
~y::
Suspend, Permit
Hotkey, %LabelKey%, Off
Return

Label:
Suspend
SoundBeep, 1000
Return
No I can't because your code is dedicated to only my explain above. Run my script and you will see how it works.
First way using it: If I click F6 my script is suspend, and all keys/hotkeys don't work, then if I click F6 again and script is unsuspended.
Secound way using it: If I click T/Y key script is suspended and all hotkeys/keys don't work (also F6), until I click esc/enter.
Third way using it: If I click F6 my script is suspended then if I click T key or Y key and I want that F6 is turned off (If I click F6 it can't unsuspend script) until I click enter/esc key. - THIS DON'T WORK IN MY SCRIPT, your code works here only, can you insert your code to the my ?
Because I can't, of course if it is possible.
User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Toggle hotkey on/off problem

Post by mikeyww » 09 Aug 2021, 18:00

Thank you for explaining. I understand what you wrote, but I do not follow your script so well, so I cannot adjust it. Sorry about that. I was providing a script as a way to demonstrate how you can use hotkeys to change the suspension state, in a way that might be more reliable than your script. The adjustment seems easy because you have control over every suspension event and hotkey. If it does not work for you, then OK, I will be quiet so that others can respond.

One additional suggestion that I have for you is that if you want to wait for a certain key, you can use Input instead of running an infinite loop.

You have written the following:

1. F6 is a toggle which suspend script and unsuspend it.
2. I want that F6 is turned off (If I click F6 it can't unsuspend script) until I click enter/esc key.

This looks like a contradiction to me-- and perhaps is why we have not solved the issue for you. If you can explain this apparent contradiction, it might help others as they look to find solutions for you. Best wishes.
Hajdes
Posts: 38
Joined: 30 Jul 2021, 17:29

Re: Toggle hotkey on/off problem

Post by Hajdes » 10 Aug 2021, 05:48

mikeyww wrote:
09 Aug 2021, 18:00
Thank you for explaining. I understand what you wrote, but I do not follow your script so well, so I cannot adjust it. Sorry about that. I was providing a script as a way to demonstrate how you can use hotkeys to change the suspension state, in a way that might be more reliable than your script. The adjustment seems easy because you have control over every suspension event and hotkey. If it does not work for you, then OK, I will be quiet so that others can respond.

One additional suggestion that I have for you is that if you want to wait for a certain key, you can use Input instead of running an infinite loop.

You have written the following:

1. F6 is a toggle which suspend script and unsuspend it.
2. I want that F6 is turned off (If I click F6 it can't unsuspend script) until I click enter/esc key.

This looks like a contradiction to me-- and perhaps is why we have not solved the issue for you. If you can explain this apparent contradiction, it might help others as they look to find solutions for you. Best wishes.
Yes, I thought it couldn't be done
User avatar
mikeyww
Posts: 26877
Joined: 09 Sep 2014, 18:38

Re: Toggle hotkey on/off problem

Post by mikeyww » 10 Aug 2021, 06:01

When I wrote "contradiction", I did not mean "impossibility". I meant that you should clarify exactly what should happen. You tried to do that, but I have still failed to understand it. Others may have a better idea.
Post Reply

Return to “Ask for Help (v1)”