Hello,
I am new to this coding program but I have 5 years experience of coding.
Anyway I was wondering how do I make a toggle script.
For example I want to have my XButton1 to be a toggle , when i press it the script toggles on , something like Send e so it just writes "e" .
I got to that part working , but when I press XButton1 again the script keeps on running , is there way to stop it?
Thanks!
Can you help out a begginer? Topic is solved
Re: Can you help out a begginer?
Code: Select all
~LButton:: ; change accordingly
toggle := !toggle
If (toggle)
Send, e
Return
Re: Can you help out a begginer?
BoBo had forgotten #Maxthreadsperhotkey and setting toggle to 0, but here's the correct version:BoBo wrote:Not tested.Code: Select all
~LButton:: ; change accordingly toggle := !toggle If (toggle) Send, e Return
Code: Select all
#MaxThreadsPerHotkey 2
Toggle := 0
~LButton:: ; change accordingly
Toggle := !Toggle
If Toggle
Send, e
Return
~LButton:: is the hotkey. ~ is a modifier that prevents the native function from being blocked.
Firstly #MaxThreadsPerHotkey 2 ensures that a hotkey can be called even when it is still executing. This will be useful if your script is longer than just sending e. It doesn't matter in your cause, but if you have a long script, you need to use this.
Toggle := !Toggle sets the value of toggle to the opposite of what it was. So if toggle was true it will be turned false and vice versa. It sets the value of Toggle to !Toggle. and ! means opposite, this only works for true and false.
0 means true. 1 means false.
So we set Toggle to zero at the beginning which BoBo had forgotten. Without assigning a variable to a value, the script won't know what the opposite of the variable is.
If Toggle If *variable name*, checks whether the variable is false. If it is false then it will do an action.
In your case it was sending e. If it was not then it won't do anything. If you still want it to do something instead of nothing then add an else after Send, e.
Lastly return marks an end to the thread.
I am your average ahk newbie. Just.. a tat more cute.
Re: Can you help out a begginer?
May I try to correct you? (your signature says it's OK)Nwb wrote:BoBo had forgotten #Maxthreadsperhotkey and setting toggle to 0, but here's the correct version:
1. AHK allows writing short and crisp code. The way this works may be considered quirky or just helpful. Pick your favourite.
In essence, AHK does not spit your code back at you, where other interpreters/compilers would. This is undesirable for some, but not for all.
In particular, when you use a variable that has not been assigned a value, AHK gives away for free the following implicit assignment: Toggle := ""
As you can see here, there is no error and an empty string resolves to False:
Code: Select all
MsgBox, % Toggle ? "yes" : "no"
In particular: BoBo's script does not need #Maxthreadsperhotkey, it's a good example for a toogle hotkey that does not need it.
In my humble opinion, your "correct" version is much more elaborate then it needs to be (by using AHK's features),
and it introduces an unnecessary directive, and I would lean towards avoiding such, rather than calling "correct".
Re: Can you help out a begginer? Topic is solved
My bad but when I try to run such a script it says that the variable has not been defined. Must be because of #Warn makes sense. But I had no bad intentions I just wanted to help.wolf_II wrote:May I try to correct you? (your signature says it's OK)Nwb wrote:BoBo had forgotten #Maxthreadsperhotkey and setting toggle to 0, but here's the correct version:
1. AHK allows writing short and crisp code. The way this works may be considered quirky or just helpful. Pick your favourite.
In essence, AHK does not spit your code back at you, where other interpreters/compilers would. This is undesirable for some, but not for all.
In particular, when you use a variable that has not been assigned a value, AHK gives away for free the following implicit assignment: Toggle := ""
As you can see here, there is no error and an empty string resolves to False:BoBo's code took the freebee, your code does not. Either way is correct, not your's alone.Code: Select all
MsgBox, % Toggle ? "yes" : "no"
2. You seem to associate #Maxthreadsperhotkey with toggling. While is is true there is a connection, it is not how you put it.Edit: sorry i misunderstood
In particular: BoBo's script does not need #Maxthreadsperhotkey, it's a good example for a toogle hotkey that does not need it.
In my humble opinion, your "correct" version is much more elaborate then it needs to be (by using AHK's features),
and it introduces an unnecessary directive, and I would lean towards avoiding such, rather than calling "correct".
Last edited by Nwb on 07 Apr 2018, 07:27, edited 1 time in total.
I am your average ahk newbie. Just.. a tat more cute.
Who is online
Users browsing this forum: No registered users and 49 guests