Suspend section

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
DaRos20
Posts: 5
Joined: 24 Jan 2022, 09:06

Suspend section

Post by DaRos20 » 24 Jan 2022, 09:32

Hi, I am trying to write a code but I cannot make it work properly.
Let's say I have 10 hotkeys in my code, I need the first 5 to work everytime (except when I suspend the entire script),
instead the last 5 hotkeys I want to work everytime (except obviously when I suspend the entire script), but I want to suspend them also if I press Ctrl+h.
So I thought I need to have a section that could be switched off by pressing a button and reactivated pressing again the same button.


I tried the following solution but is not working.
(My idea was to initiate a variable "State" and make it true when I start the .ahk, then if I press ^h I want to switch the state " if true→false or if false→true" and make the hotkeys active only when the State is true)

Code: Select all

VK31::Suspend        ;Suspend all the script

;SECTION 1

State = true

^h::
           If State = true
           {
                 State = false
           }
           else
           {
                State = true
           }

If Var = true
{
   Hotkeys active
}

; SECTION 2 (must be always on except obviously when VK31 is pressed)
[Mod edit: [code][/code] tags added.]

If I need to post my code tell me, but it may be confusing., instead of working on a sample that is easy and where I could learn my self how to adapt the solution to my code.

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

Re: Suspend section

Post by boiler » 24 Jan 2022, 09:41

Use real 'True' and 'False' rather than strings by using the expression operator := to assign true. That allows you to then toggle State's value with State := ! State. You can't put code like where you assign the initial value to State below a hotkey or it won't execute. See Auto-execute Section.

Code: Select all

#SingleInstance, Force
SendMode Input
SetWorkingDir, %A_ScriptDir%
State := true ; must go above first hotkey or it won't ever be executed. Also use := operator

VK31::Suspend ;Suspend all the script

;SECTION 1

^h::State := !State ; can simply do this when using real 'True' and 'False' instead of strings

#If State
; hotkeys to be active only when state is true go HighlightAHK

#If
; SECTION 2 (must be always on except obviously when VK31 is pressed)
Last edited by boiler on 24 Jan 2022, 09:43, edited 1 time in total.

User avatar
mikeyww
Posts: 26888
Joined: 09 Sep 2014, 18:38

Re: Suspend section

Post by mikeyww » 24 Jan 2022, 09:41

I like boiler's. If needed, here is the approach with the Suspend command.

Code: Select all

^h::Suspend
F4::MsgBox, This sometimes works.
F3::
Suspend, Permit
MsgBox, This always works!
Return
Explained: Suspend

DaRos20
Posts: 5
Joined: 24 Jan 2022, 09:06

Re: Suspend section

Post by DaRos20 » 24 Jan 2022, 10:14

It's the first time I write in a forum so I am not so expert, but really thank you @boiler and @mikeyww, I used boiler solution because I have maybe 40 hotkeys in the "always on" section and as I understood I needed to write permit in every one so that what a little bit more confusing to do.

User avatar
mikeyww
Posts: 26888
Joined: 09 Sep 2014, 18:38

Re: Suspend section

Post by mikeyww » 24 Jan 2022, 10:20

I agree completely!

DaRos20
Posts: 5
Joined: 24 Jan 2022, 09:06

Re: Suspend section

Post by DaRos20 » 24 Jan 2022, 10:40

Sorry, but I found now that the part that should work always is not running but only the part with #If state, I will post the entire code and the 2 section will be separated by a "-----" line.

(another question, if I want the SECTION to be off the first time the file.ahk is started and then activated the first time when I press the "key that activate State", how should I change my code to do that. Maybe it's like now but with State not starting as true but as false?)

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

;\\\\\\\\\\\VARIABILI\\\\\\\\\\\\\\\\\\\\

State := true

;\\\\\\\\\\ CONTROLLO PROGRAMMA \\\\\\\\\

!Space::Suspend ; sospendi Hotkey
VKB7::Suspend

;\\\\\\\\\\\\\\\\\\\ PENNA E MEDIA \\\\\\\\\\\\\\\\\\\\

^!s::State := !State

#If State
{
VK60::Send, ^z
VK69::Send, ^v
VK68::Send, ^c
VK67::Send, ^x

VK61::left
VK62::down
VK63::right
VK65::up

VK6A::Send, {f}
VK6D::Send, {esc}
VK6B::Send, {tab}
VK0D::Send, {space}
}

;---------------------------------------------------------------------------------------------------------

#If

{
SetTitleMatchMode, RegEx ; match window titles by regular expressions
#IfWinActive - OneNote$ ; ------ only in windows with title ending with "- OneNote"

;\\\\\\\\\\\\\\\\\\\ EQUAZIONI \\\\\\\\\\\\\\\\\\\\

VK5D::!VKbb                   ; Inserisci equazione

!x::Send, \times{Space}       ; Prodotto vettoriale
!VKBE::Send, \bullet{Space}   ; Prodotto vettoriale

VK78::Send, !jed{Enter}{Left} ; Parentesi
VK79::Send, !jec{Enter}{Left} ; Sottolineatura vettore
VK7A::Send, !jeb{Enter}{Left}{Left} ; Frazione

!w::Send, \omega{Space}       ;omega
!e::Send, \varepsilon{Space}  ;epsilon
!r::Send, \doubleR{Space}     ;Insieme Reali
!t::Send, \tau{Space}         ;tau
!u::Send, \theta{Space}       ;theta
!i::Send, \in{Space}          ;Appartiene
!o::Send, \phi{Space}         ;phi
!p::Send, \pi{Space}          ;pigreco
!è::Send, \Sigma{Space}       ;Sigma
!a::Send, \alpha{Space}       ;alpha
!d::Send, \Delta{Space}       ;Delta
!f::Send, \forall{Space}      ;per ogni
!g::Send, \nabla{Space}       ;gradiente
!j::Send, \varphi{Space}      ;varphi
!k::Send, \kappa{Space}       ;k
!l::Send, \lambda{Space}      ;lambda
!ò::Send, \sigma{Space}       ;sigma
!b::Send, \beta{Space}        ;beta
!n::Send, \doubleN{Space}     ;Insieme Naturali
!m::Send, \scriptM{Space}     ;M di matrice

;\\\\\\\\\\\\\\\\\\\ LAYOUT \\\\\\\\\\\\\\\\\\\\

#l::Send, !nl			; Larghezza pagina

;VK30::+VK30                   ; Simbolo = (FUNZ DISABILITATA)

#VK41::Send, !hdi6{Enter}	; Dim testo 6
#VK53::Send, !hdi7{Enter}       ; Dim testo 7
#VK44::Send, !hdi9{Enter}       ; Dim testo 9
#VK46::Send, !hdi12{Enter}      ; Dim testo 12

#f::Send, !hror!r255{Tab}0{Tab}254{Enter}               ; Fucsia

#v::Send, !htiCalibri Light{Enter}!hdi7{Enter}!hror!r0{Tab}176{Tab}80{Enter}    ; Calibri Light-7-Verde
#b::Send, !htiCalibri Light{Enter}!hdi9{Enter}!hror!r0{Tab}112{Tab}192{Enter}   ; Calibri Light-9-Blu
#n::Send, !htiCalibri Light{Backspace}{Backspace}{Backspace}{Backspace}{Backspace}{Backspace}{Enter}!hdi12{Enter}!hror!r0{Tab}0{Tab}1{Enter}            ; Calibri-12-Nero

;\\\\\\\\\\\\\\\\\\\\\ MOUSE \\\\\\\\\\\\\\\\\\\\\\

; funzione premi laterale (verso di te) e rilascia per il lazzo

VK05::
Send, !dl
KeyWait, VK05
Send, !dp
return

;\\\\\\\\\\\\\\\\\\\ TESTI \\\\\\\\\\\\\\\\\\\\\\\\\\

VK74::Send, ^b 			; Grassetto
VK75::Send, ^u 			; Sottolineato
VK76::Send, !cr                 ; Ritaglio schermata

VK77:: Send, (){Left}       	; parentesi tonde

VK20 & a::Send, Allora{:}
VK20 & q::Send, Quindi{:}
VK20 & o::Send, Ossia{:}

;\\\\\\\\\\\\\\\\\\\ TASTI FN \\\\\\\\\\\\\\\\\\\\\\\\\\

#VK70::Send, {F1}
#VK71::Send, {F2}
#VK72::Send, {F3}
#VK73::Send, {F4}
#VK74::Send, {F5}
#VK75::Send, {F6}
#VK76::Send, {F7}
#VK77::Send, {F8}
#VK78::Send, {F9}
#VK79::Send, {F10}
#VK7A::Send, {F11}
#VK7B::Send, {F12}

;\\\\\\\\\\\\\\\\\ COMBINAZIONI TASTI STANDARD \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

VK7B::Send, +{F9}

VK70::Send, ^z                ; Annulla
VK71::Send, ^x                ; Taglia
VK72::Send, ^c                ; Copia
VK73::Send, ^v                ; Incolla

#IfWinActive ; ------ end of section restricted to specific windows
}

User avatar
mikeyww
Posts: 26888
Joined: 09 Sep 2014, 18:38

Re: Suspend section

Post by mikeyww » 24 Jan 2022, 10:55

You can set the starting state to False if needed. Read auto-exec. Put "running code" such as SetTitleMatchMode, RegEx there, because it will not execute between hotkeys.

DaRos20
Posts: 5
Joined: 24 Jan 2022, 09:06

Re: Suspend section

Post by DaRos20 » 24 Jan 2022, 11:05

@mikeyww So I need to make 2 separate file.ahk and call them separeted?
Because when I had both separated they worked perfectly but now I wanted to have a single file to use with both pc and tablet, but I need that SECTION 1 could work with any app or website instead SECTION2 must work only on onenote.

To summarize I need to have:
SECTION1 working on every app or site and be suspendable independently
SECTION2 working only on onenote and be suspendable only from the master suspend command (or as in my code !Space or VKB7)

I really don't know the programm and it's pretty difficult for a non-informatic person like me, maybe it's too much but I need more a solution than reading all autohotkey guide and then do the code because it's too much for what I need.
Thanks again

User avatar
mikeyww
Posts: 26888
Joined: 09 Sep 2014, 18:38

Re: Suspend section

Post by mikeyww » 24 Jan 2022, 11:18

You can use one script.

Code: Select all

SetTitleMatchMode, 2

^i:: ; CTRL-I = Suspend all hotkeys
Suspend
SoundBeep, 1000 - 300 * A_IsSuspended
Return

^h:: ; CTRL-H = Suspend only Section 1
section1 := !section1
SoundBeep, 1300 + 500 * section1
Return

#If section1
F3::MsgBox, Section1

#IfWinActive OneNote ahk_class ApplicationFrameWindow ; Section 2
F4::MsgBox, Section2
#IfWinActive

DaRos20
Posts: 5
Joined: 24 Jan 2022, 09:06

Re: Suspend section

Post by DaRos20 » 24 Jan 2022, 11:46

Thank you so much @mikeyww

Post Reply

Return to “Ask for Help (v1)”