AutoHotkey Community

It is currently May 27th, 2012, 12:56 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: toggle function
PostPosted: January 26th, 2006, 7:02 pm 
Offline

Joined: December 18th, 2005, 5:40 pm
Posts: 234
Location: Italy - Galatro(RC)
maybe usefull for someone
this function return 1 the first time it is called, 0 the second time, 1 the third time, then 0, and so on.
You can use it in your if statement:
Code:
if(toogle())
 ....
else
 ....


so the script will execute the if block or the else one, depending on the state.

this is the function
Code:
toggle()
   {
      global
      if(firsttoggle="")
         {
            firsttoggle=nomore
            state=1
         }
      state := !state
      return state      
   }


this is an exemple(press "a"):
Code:
a::
msgbox,% toggle()
return

toggle()
   {
      global
      if(firsttoggle="")
         {
            firsttoggle=nomore
            state=1
         }
      state := !state
      return state      
   }

_________________
____________________
______________________
kiu - www.romeosa.com


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 26th, 2006, 7:49 pm 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
Thanks for sharing.

But I wouldn't set the vars to global. since it is not needed in this case.

I think this is sufficient:
Code:
toggle()
   {
      static state
      return !state       
   }
*not tested*

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 26th, 2006, 8:56 pm 
Offline

Joined: December 18th, 2005, 5:40 pm
Posts: 234
Location: Italy - Galatro(RC)
toralf wrote:
I wouldn't set the vars to global. since it is not needed in this case.

yes,i know, but for some reason without global the function doesn't work

Quote:
I think this is sufficient:
Code:
toggle()
   {
      static state
      return !state       
   }
*not tested*

you must initialize the variable somewhere in the script(not with my function).

Anyway, tested your code: it doesn't work

my regards to your Auto-Syntax-Tid,it's very helpful

_________________
____________________
______________________
kiu - www.romeosa.com


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 26th, 2006, 9:35 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
kiu wrote:
Anyway, tested your code: it doesn't work

Try this (tested):
Code:
Loop, 5
   MsgBox % "Toggle: " . toggle()

toggle() {
   static t
   If t
      t := false
   Else
      t := true
   Return, % t
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 26th, 2006, 10:09 pm 
Offline

Joined: December 18th, 2005, 5:40 pm
Posts: 234
Location: Italy - Galatro(RC)
and this(tested)

Code:
Loop, 5
 MsgBox % "Toggle: " . toggle()

toggle() {
 static t
 t := !t
 Return, t
}

_________________
____________________
______________________
kiu - www.romeosa.com


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 26th, 2006, 10:22 pm 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
Now this is a nice solution. Well done.

Thanks for the regards, but it will take some time before it will support OTB as in your example.

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 26th, 2006, 11:00 pm 
Offline

Joined: December 18th, 2005, 5:40 pm
Posts: 234
Location: Italy - Galatro(RC)
OTB :?:

_________________
____________________
______________________
kiu - www.romeosa.com


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 27th, 2006, 12:00 am 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
kiu wrote:
OTB :?:

:arrow: OTB


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 27th, 2006, 12:15 am 
Offline

Joined: December 18th, 2005, 5:40 pm
Posts: 234
Location: Italy - Galatro(RC)
thanks :D

_________________
____________________
______________________
kiu - www.romeosa.com


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 15th, 2008, 9:07 pm 
Hello, i am trying to figure out how to setup this scrips to actually toggle between 2 different buttons, but it's very esoteric. Please, help.

I had less trouble understanding the concept behind fissile materials.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 17th, 2008, 1:49 am 
Offline

Joined: July 15th, 2007, 1:43 am
Posts: 1320
Code:
Gui, Add, Button, w100 h20 vButton1 gButton1, Button1
Gui, Add, Button, w100 h20 +Disabled vButton2 gButton2, Button2
Gui, Show
Return

Button1:
Button2:
x++
GuiControl, % (x & 1) ? "Disable" : "Enable", % (x & 1) ? "Button2" : "Button1"
GuiControl, % (x & 1) ? "Enable" : "Disable", % (x & 1) ? "Button1" : "Button2"


I think that'll work. creds to Laszlo for the x & 1 method.

_________________
Religion is false. >_>


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 17th, 2008, 1:54 am 
Offline

Joined: April 18th, 2008, 7:57 am
Posts: 1390
Location: The Interwebs
Here's one even shorter:

Code:
toggle() {
 static t
 Return, ( t := !t)
}


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Bon, bowen666 and 21 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group