Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

Autohotkey Toggle State Function


  • Please log in to reply
13 replies to this topic
ShadowExile
  • Guests
  • Last active:
  • Joined: --
Hey guys, having a bit of an issue with a bind I want to make, though I really shouldn't be having this problem as Autohotkey seems similar to Java, and I just finished Semester 1 of Java in uni.

Anyway, my idea was really simple and it's basically what you'd expect, hit a bind once and it does one thing, hit again and it does another. It alternates between the two.

Initially I had a value called on which I initialised using on := false

My program would go (initially, I've tried many variations and looked through autohotkeys documentations, nothing worked properly)

Numpad4::
on := false

If(on = false)
{
SendInput blah blah blah
on := true
}

else {
SendInput alternative blah blah blah
on := false
}
return

I tried to add in loops, gotos, breaks, pauses etc, but none of them achieved the affect that I really wanted.

I want the bind to basically just stop after it does one thing. So if on is false, then "blah blah blah" comes out, and it stops till the button is pressed again. However it has to remember what the value of "on" is, even past return or break. With return and break, I seem to find that the value of "on" is forgotten, so the bind cannot continue past the first stage.

Any help would be greatly appreciated, here's to it probably being just some dumb major oversight on my behalf. Thanks guys :)

Rseding91
  • Members
  • 702 posts
  • Last active: Oct 03 2014 08:15 PM
  • Joined: 07 Jun 2010
Every time the hotkey is triggered it resets the "on" to "false"

You need to use something like this:

NumPad4::

If (On){
Send bla bla bla
On := False
} Else {
Send Ble ble ble
On := true
}
Return

ShadowExile
  • Guests
  • Last active:
  • Joined: --
Oh god, I knew it would be a dumb oversight. Thanks for helping me out anyway :) Knew I was on (somewhat) the right track.

This has made everything perfect for me now, thanks so much :D

Leef_me
  • Moderators
  • 8442 posts
  • Last active: Today, 06:21 PM
  • Joined: 08 Apr 2009
a slightly "cleaner" design
n=0   ; this isn't really needed, the invert of empty is 1
return
f1::
n:=!n         ; invert or reverse the prior value, 1 or 0 are the only choices
if n=1
  tooltip, this is true
else
  tooltip, this is false
return


nimda
  • Members
  • 4368 posts
  • Last active: Jan 15 2014 02:47 PM
  • Joined: 26 Dec 2010
One line:
n::ToolTip % (t := !t) ? "Bla" : "Ble"


tomoe_uehara
  • Members
  • 2112 posts
  • Last active: Apr 06 2014 06:00 PM
  • Joined: 05 Sep 2009
I wonder where did this thing come from? --> "t := !t"
Is it on the helpfile?


nimda
  • Members
  • 4368 posts
  • Last active: Jan 15 2014 02:47 PM
  • Joined: 26 Dec 2010
It is called a 'toggle'
The variable t stands for toggle.
The ! is a logical NOT, so if t is blank or 0, !t returns a 1 (true) else it returns 0 (false)
The assignment can be used inline, as shown.

tomoe_uehara
  • Members
  • 2112 posts
  • Last active: Apr 06 2014 06:00 PM
  • Joined: 05 Sep 2009
So it will only returns 1 & 0 ?

girlgamer
  • Moderators
  • 2973 posts
  • Last active: Today, 09:03 PM
  • Joined: 04 Jun 2010
Technically !x should return a zero or a NOT zero which logically can be any number other than zero. Typically we think of it as a 1 or TRUE. if you want true 1's and 0's this will do it for you...
x := ((x +1) & 1)
how that works is this:
the x variable has 1 added to it to turn on or off the low order bit of the variable
then the &1 masks out all other bits but the low order bit and throws away the rest.
so the logic is
if x = any even number, x+1 = an odd number, &1 takes the 1 from the odd number and throws away the rest. Result is 1
if x = any odd number, x+1 = an even number, &1 takes the zero from the even number and throws away the rest. result is 0
with true bit mapping the code would ideally be
x := ((x | 1 ) & 1)
where | 1 is an bitwise OR (similar to an add 1 operation)
another way it could be done is with the xor operation which flips all the bits in the operand to their opposite state
x := ((^x) & 1)
or by using the bitwise NOT operator ~
x := ((~x) & 1)
all produce essentiall the same thing -- a toggle
however most people just use x := !x which is sloppy but effective.

Leef_me
  • Moderators
  • 8442 posts
  • Last active: Today, 06:21 PM
  • Joined: 08 Apr 2009

Technically !x should return a zero or a NOT zero which logically can be any number other than zero. Typically we think of it as a 1 or TRUE. if you want true 1's and 0's this will do it for you...


Your statements that I have quoted are incorrect.

<!-- m -->http://www.autohotke...s.htm#Operators<!-- m -->

Logical-not (!): If the operand is blank or 0, the result of applying logical-not is 1, which means "true". Otherwise, the result is 0 (false). For example: !x or !(y and z).


The logical Not of zero is 1 or True, which doesn't include "any number other than zero". While the logical not of "any number other than zero" is zero, the reverse case is not true.

however most people just use x := !x which is sloppy but effective


The method is effective, agreed. As far as "sloppy", that is a matter of opinion.
Since variables, even numbers, are typically stored as strings, AutoHotkey's variable storage is 'sloppy'.

Your bit-wise statements appear to be correct.

girlgamer
  • Moderators
  • 2973 posts
  • Last active: Today, 09:03 PM
  • Joined: 04 Jun 2010
I stand corrected on the logical not.

]In arithmetic expressions, the NOT operator means logical NOT and has the same meaning as the ¬ and ! operators. Logical NOT evaluates whether a number or variable has a value of 0. If the character or variable has a value of 0, the result of the command is 1, which means true. If the character or variable has a value other than 0, the result of the command is 0, which means false.

For example, the command

Evaluate NOT 100

displays 0 in the active window. But the command

Evaluate NOT 0

displays 1.

reference: <!-- m -->http://developer.app... ... gical.html<!-- m -->[/url]

nimda
  • Members
  • 4368 posts
  • Last active: Jan 15 2014 02:47 PM
  • Joined: 26 Dec 2010

So it will only returns 1 & 0 ?

Yes.

girlgamer
  • Moderators
  • 2973 posts
  • Last active: Today, 09:03 PM
  • Joined: 04 Jun 2010
according to the documentation
if x is 0 or blank then !x returns 1
and
if x is 1 then !x returns 0

the assignment is important to preserve the state for the next test.
x := !x

AfterLemon
  • Moderators
  • 236 posts
  • Connoisseur of Boobs
  • Last active: Oct 17 2014 06:27 PM
  • Joined: 09 Oct 2012

This should be somehow written into a helpfile.