On/Off button and setting variable timer

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Sosasosa
Posts: 5
Joined: 26 Mar 2023, 15:26

On/Off button and setting variable timer

Post by Sosasosa » 26 Mar 2023, 16:08

Hi !

First of all, sorry if my english isn't perfect, it's not my native language. Then, if the subject already have been discuted, I'm sorry, I didn't found no references.
I'm starting to use autohotkey and I think it's a really powerfull software but there's some thing I don't know how to implement.
I'd love to create a switch for my script like an on off button. It means that if I want to use one part of the script, I just have to press one key to enable it, during this period my script work, and when I press it again it stop.
How can I do that ? I've got some repetitives operations to do and it'll easier to have this feature.
My second questions is related to fonctions about time like sleep, setimer, etc... I'd like to know if its possible to assign a value between to instead a fix one. For exemple, instead of

Code: Select all

sleep 100,
for exemple; to write

Code: Select all

sleep 100~300,
.

Thank you very much for your time.

Have a nice day !

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

Re: On/Off button and setting variable timer

Post by mikeyww » 26 Mar 2023, 19:27

Welcome to this AutoHotkey forum!

Your question is vague, so the answer is somewhat vague as well.

#HotIf can set a context for hotkeys. You can then set a flag variable to define your context. You can find many examples of this directive on the forum.

To generate a random number, you can use :arrow: Random.

Code: Select all

#Requires AutoHotkey v2.0
SoundBeep 1500
Sleep Random(100, 300)
MsgBox 123

Sosasosa
Posts: 5
Joined: 26 Mar 2023, 15:26

Re: On/Off button and setting variable timer

Post by Sosasosa » 27 Mar 2023, 10:18

Thanks for your fast answer.
I'm going to try to precise my idea.
Maybe with a token system, if this certain key have been pressed, I have to had a token or something to say "execute this code", if not, continue. With a second press, it'll erase the token and the script would be inactive.
I'm going to look for the HotIf function to see if I found a way to do something like this.

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

Re: On/Off button and setting variable timer

Post by mikeyww » 27 Mar 2023, 10:23

Yes, that is what variables do. The specific way to implement it depends on what code you want to execute, and under what circumstances.

Sosasosa
Posts: 5
Joined: 26 Mar 2023, 15:26

Re: On/Off button and setting variable timer

Post by Sosasosa » 28 Mar 2023, 21:43

I quess I'll need some help. I tried multiple time to create my code using #HotIf but it never worked. So I decided to try the code that have been given in the documentation, which is :

Code: Select all

#HotIf WinActive("ahk_class Notepad") or WinActive(MyWindowTitle)
#Space::MsgBox "You pressed Win+Spacebar in Notepad or " MyWindowTitle
but I receive the same error code that on my codes.
Image

When I finally had something running (I don't know why to be honest because I think it's missing a space between #HotIf and ThisHotkey) the #HotIf at the end is not reconized as a valid action. The code was :

Code: Select all

#HotIfThisHotkey("NumpadAdd")
#IfWinActive, ahk_class Notepad
LButton:: MsgBox "Test1"
#HotIf
Image

I really don't get it, what's the problem and how can I solve it ?
Thanks

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

Re: On/Off button and setting variable timer

Post by boiler » 28 Mar 2023, 22:12

The main problem is that you posted in the v2 section, so you are getting replies with AHK v2 code (and your own posted code is v2 code), but you are using AHK v1. Should I move the thread to the v1 section, or would you like to install v2 and run the posted code?

Sosasosa
Posts: 5
Joined: 26 Mar 2023, 15:26

Re: On/Off button and setting variable timer

Post by Sosasosa » 29 Mar 2023, 06:33

It seems that when I updated my autohotkey it didn't erase the old version. Thanks, I'll stay on version 2 and try to solve my problem. I feel free to come back if I need more informations.
Thanks !

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

Re: On/Off button and setting variable timer

Post by boiler » 29 Mar 2023, 06:39

If you mean that when you installed v2, it didn’t remove v1, that is on purpose so that both versions are available. To force it to select the correct version in case it’s not automatically selecting it based on the script content, put the following line at the top of your v2 scripts, as mikeyww showed in the script he posted:

Code: Select all

#Requires AutoHotkey v2.0

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

Re: On/Off button and setting variable timer

Post by mikeyww » 29 Mar 2023, 06:54

Code: Select all

#Requires AutoHotkey v2.0
MyWindowTitle := 'test'
on            := False

F3:: {
 Global on := !on
 SoundBeep 1000 + 500 * on
}

#HotIf on
F12::MsgBox 'On!', 'Status', 64

#HotIf WinActive('ahk_class Notepad') or WinActive(MyWindowTitle)
#Space:: {
 SoundBeep 1500
 Sleep Random(100, 300)
 MsgBox 'You pressed Win+Spacebar in Notepad or ' MyWindowTitle
}
#HotIf

Sosasosa
Posts: 5
Joined: 26 Mar 2023, 15:26

Re: On/Off button and setting variable timer

Post by Sosasosa » 11 Apr 2023, 21:53

Didn't had the time to come back. Thanks for this exemple, I'm going to try to use this base to make my script work. Thank you !

Tobgun1
Posts: 123
Joined: 23 Feb 2023, 07:28

Re: On/Off button and setting variable timer

Post by Tobgun1 » 11 Apr 2023, 23:02

Hello there ✌

Me and my Tudor like to work with "Settimer". And variables to toggle things on and off

Code: Select all

#Requires AutoHotkey v2.0 ; Script requires AutoHotkey V2

#SingleInstance Force ; When this script launches, if it was already running, it will close the previous one 

F4:: { ; <- F4 Hotkey to Toggle between Timer On and Timer Off
         Static on := False
        if on := !on {
         ComObject("SAPI.SpVoice").Speak("Function On")
         SetTimer(PutInHereYourFunctionName, 500) ; <- repeating checks if function on each "500" milliseconds
        } else {
         SetTimer(PutInHereNameOfFunction, 0) ; 0 = Off
         ComObject("SAPI.SpVoice").Speak("Function Off") ; Speaks the Text "Function Off" you can "put in here your own text"
    }
The Next thing I like to use is the code part

Code: Select all

if WinActive("PutInHereYourWindowTitle") { 
code lines in here
}
to make things only active if a specified window is open (like Microsoft-Word is open)


Last thing I liked was, let something NOT be active while I am working, was "idle check" so script waits until my computer is longer then xxx seconds into "idle" to make the action you do a auto repeating Settimer on the top and then you do the function with if A_TimeIdle < xxxx milliseconds on it.


This example here moves my mouse after around 20 seconds inactivity

Code: Select all

#Requires AutoHotkey v2.0 ; Script requires AutoHotkey V2

#SingleInstance Force ; When this script launches, if it was already running, it will close the previous one 


SetTimer(MouseMove, 1000) ; starts the function every x milliseconds (1 second)

MouseMove() {
        if A_TimeIdle >= 15000 { ; (15000 = 15 seconds)  Checks if PC was in Idle for x sec then activate following Function 

MouseMove Random(-1000,1000), Random(-1000,1000), Random(100), "R" ; RandomMouseMove across x and x Pixel
        }

    }

I recomend you to google the functions in the docs and maybe check for "Visual Studio Code" as Script Editor Programm,
You just need to install the AHK Language Packs
But you can also google that (Visual Studio AHKv2 Language.

Post Reply

Return to “Ask for Help (v2)”