How to turn a button into an on,off?? help pls :D Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
cleberma7
Posts: 2
Joined: 25 Jun 2022, 09:59

How to turn a button into an on,off?? help pls :D

Post by cleberma7 » 25 Jun 2022, 10:36

;I use this script to shorten some things, ex: just by pressing F i get 6 and 7 pressed togheter, but I can't figure out a way to make a button work like a Toggle on,off for ex: I want my R to send a key every 1000ms when i press it once and stop sending it when I press again... just like on, off, on, off. Can we make it work fine?

Code: Select all

#NoEnv
#Warn
SendMode Input
SetWorkingDir %A_ScriptDir%
#MaxHotkeysPerInterval 20000
1::1
2::2
3::3
4::4
5::5
6::6
7::7
8::8
9::9
0::0
q::q
w::w
e::e

r::r ;(I want this "r" to be pressed every 1000ms when I press it once and to stop when I press it again)

t::t
y::y
u::u
i::i
o::o
p::p
a::a
s::s
d::d
f::Send, 67
g::g
h::h
j::j
k::k
l::l
ç::ç
z::z
x::Send, 890
c::c
v::v
b::b
n::n
m::m
f1::f1
f2::f2
f3::f3
f4::f4
f5::f5
f6::f6
f7::f7
f8::f8
f9::Suspend,Toggle
f10::f10
f11::f11
f12::f12
Numpad1::Numpad1
Numpad2::Numpad2
Numpad3::Numpad3
Numpad4::Numpad4
Numpad5::Numpad5
Numpad6::Numpad6
Numpad7::Numpad7
Numpad8::Numpad8
Numpad9::Numpad9
[Mod edit: [code][/code] tags added.]

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

Re: How to turn a button into an on,off?? help pls :D

Post by mikeyww » 25 Jun 2022, 11:07

Code: Select all

r::SetTimer, r Up, % (on := !on) ? 1000 : "Off"
r Up::Send % on ? "x" : ""

cleberma7
Posts: 2
Joined: 25 Jun 2022, 09:59

Re: How to turn a button into an on,off?? help pls :D

Post by cleberma7 » 25 Jun 2022, 20:38

mikeyww wrote:
25 Jun 2022, 11:07

Code: Select all

r::SetTimer, r Up, % (on := !on) ? 1000 : "Off"
r Up::Send % on ? "x" : ""
It almost works fine but I'm struggling a little bit to understand how this code is working can U explain please?
now R heals at right timing and works as intended and X keeps cicling ATK1, ATK2 and ATK3 just like I needed :D but they interfere with each others state now, how can i define them as separate?? and also I get an error at the line I marked with ' $ ' that says -

Code: Select all

Warning: This variable has not been assigned a value
Specifically: on a (global variable)

--> $: SetTimer, r Up,(on := !on) ? 1080: "off"
The error doesn't seems to interfere with the functionality of the script it's only an aesthetical problem(?) The bigger problem now is how can I keep R and X separated so they don't get affected by each other (when it was healing (R) and i pressed ATK (X) it stops them both but if i pressed X again then both starts working and etc.) Hope I explained it all, sorry if I miss spell something (I speak PT-BR) thank you!

Code: Select all

1::1
2::2
3::3
4::4
5::5
6::6
7::7
8::8
9::9
0::0
q::q
w::w
e::e

$ r::SetTimer, r Up, % (on := !on) ? 1080 : "Off"
r Up::Send % on ? "1" : ""

t::t
y::y
u::u
i::i
o::o
p::p
a::a
s::s
d::d
f::Send, 567
g::g
h::h
j::j
k::k
l::l
ç::ç
z::z

x::SetTimer, x Up, % (on := !on) ? 1080 : "Off"
x Up::Send % on ? "890" : ""

c::c
v::v
b::b
n::n
m::m
f1::f1
f2::f2
f3::f3
f4::f4
f5::f5
f6::f6
f7::f7
f8::f8
f9::Suspend,Toggle
f10::f10
f11::f11
f12::f12
Numpad1::Numpad1
Numpad2::Numpad2
Numpad3::Numpad3
Numpad4::Numpad4
Numpad5::Numpad5
Numpad6::Numpad6
Numpad7::Numpad7
Numpad8::Numpad8
Numpad9::Numpad9
[Mod edit: [code][/code] tags added.]
Last edited by gregster on 25 Jun 2022, 20:57, edited 1 time in total.
Reason: Please use code tags. Thank you!

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

Re: How to turn a button into an on,off?? help pls :D

Post by mikeyww » 25 Jun 2022, 21:05

As a first step, test the script that I posted, by itself with no other code, in Notepad. That tells you if you are starting with a working script.

Rohwedder
Posts: 7551
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to turn a button into an on,off?? help pls :D  Topic is solved

Post by Rohwedder » 25 Jun 2022, 23:57

Hallo,
try:

Code: Select all

#NoEnv
#Warn, UseUnsetGlobal, Off ;Allows to read uninitialized variables
SendMode Input
SetWorkingDir %A_ScriptDir%
#MaxHotkeysPerInterval 20000
1::1
2::2
3::3
4::4
5::5
6::6
7::7
8::8
9::9
0::0
q::q
w::w
e::e

r::SetTimer, r Up, % (Ron := !Ron) ? 1080 : "Off"
r Up::Send % Ron ? "1" : ""

t::t
y::y
u::u
i::i
o::o
p::p
a::a
s::s
d::d
f::Send, 567
g::g
h::h
j::j
k::k
l::l
ç::ç
z::z

x::SetTimer, x Up, % (Xon := !Xon) ? 1080 : "Off"
x Up::Send % Xon ? "890" : ""

c::c
v::v
b::b
n::n
m::m
f1::f1
f2::f2
f3::f3
f4::f4
f5::f5
f6::f6
f7::f7
f8::f8
f9::Suspend,Toggle
f10::f10
f11::f11
f12::f12
Numpad1::Numpad1
Numpad2::Numpad2
Numpad3::Numpad3
Numpad4::Numpad4
Numpad5::Numpad5
Numpad6::Numpad6
Numpad7::Numpad7
Numpad8::Numpad8
Numpad9::Numpad9
Why are you remapping keys 1,2,3,4 ... to themselves? This is only necessary in special cases. Otherwise rather harmful!
Explanation:
This

Code: Select all

r::SetTimer, r Up, % (Ron := !Ron) ? 1080 : "Off"
r Up::Send % Ron ? "1" : ""
is the short form of:

Code: Select all

r::
Ron := !Ron
IF Ron
	SetTimer, r Up, % 1080
Else
	SetTimer, r Up, % "Off"
Return
r Up::
IF Ron
	Send % "1"
Else
	Send % ""
Return
using https://ahkde.github.io/docs/Variables.htm#ternary

Post Reply

Return to “Ask for Help (v1)”