one key two actions toggle

Get help with using AutoHotkey and its commands and hotkeys
genaro
Posts: 6
Joined: 09 Feb 2016, 20:07

one key two actions toggle

18 Oct 2019, 00:08

Code: Select all

f1::MyFunc()

MyFunc() { ; function 'MyFunc'

static boolean := true ; the value of static variables (included any modification upon them) are remembered between function calls

	if (boolean:=!boolean) { ; !boolean return true if boolean=false and false if boolean=true 
	MouseClick, left, 107, 537,,1
	Send ^{9}
	; content of action 1 goes here
	} else {
	Send ^{1} 
	; content of action 2 goes here
	}

}
return


f2::
Send ^{1}
sleep 200
MouseClick, left, 107, 577,,1
MouseClick, left, 107, 537,,1
Send ^{9}
boolean:=!boolean
boolean := true
return
If I pressed F2, I want F1 to send "if" part the first time I press F1. Help plz.
Hellbent
Posts: 736
Joined: 23 Sep 2017, 13:34

Re: one key two actions toggle

18 Oct 2019, 00:35

Try this.

Code: Select all


#SingleInstance,Force



1::MyFunc()

MyFunc(override:="") { 
	static boolean := true 
	if(override){
		boolean:=0
		return
	}
	if (boolean:=!boolean) { 
		msgbox, in hotkey 1 if( boolean = true )
	} else {
		msgbox, in hotkey 1 else `n boolean = false
	}

}



2::
MyFunc(1)
return

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

Re: one key two actions toggle

18 Oct 2019, 00:44

Hallo,
try:

Code: Select all

Global boolean := true

f1::MyFunc()

MyFunc() { ; function 'MyFunc'
	if (boolean:=!boolean) { ; !boolean return true if boolean=false and false if boolean=true 
	MouseClick, left, 107, 537,,1
	Send ^{9}
	; content of action 1 goes here
	} else {
	Send ^{1} 
	; content of action 2 goes here
	}

}
return


f2::
Send ^{1}
sleep 200
MouseClick, left, 107, 577,,1
MouseClick, left, 107, 537,,1
Send ^{9}
boolean := true
return
Hellbent
Posts: 736
Joined: 23 Sep 2017, 13:34

Re: one key two actions toggle

18 Oct 2019, 00:55

Rohwedder wrote:
18 Oct 2019, 00:44
Hallo,
try:

Code: Select all


f2::
Send ^{1}
sleep 200
MouseClick, left, 107, 577,,1
MouseClick, left, 107, 537,,1
Send ^{9}
boolean := true   ;<---------------- should be false
return
boolean should be set to false here
Rohwedder
Posts: 2281
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: one key two actions toggle

18 Oct 2019, 01:29

If I pressed F2, I want F1 to send "if" part the first time I press F1.
Therefore boolean should be set to the intitalization value = True
Hellbent
Posts: 736
Joined: 23 Sep 2017, 13:34

Re: one key two actions toggle

18 Oct 2019, 01:55

Rohwedder wrote:
18 Oct 2019, 01:29
If I pressed F2, I want F1 to send "if" part the first time I press F1.
Therefore boolean should be set to the intitalization value = True
Try it.

Code: Select all

#SingleInstance,Force

Global boolean := true

F1::MyFunc()

MyFunc() { 
	if (boolean:=!boolean) { 
		ToolTip, in if
	} else {
		ToolTip, In else
	}

}
return


F2::
boolean := true
return

Esc::ExitApp
Rohwedder
Posts: 2281
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: one key two actions toggle

18 Oct 2019, 03:24

Try it:
Old Version with static boolean:

Code: Select all

#SingleInstance,Force
F1::MyFunc()
MyFunc() {
	static boolean := true
	if (boolean:=!boolean) { 
		ToolTip, in if
	} else {
		ToolTip, In else
	}
}
return
Esc::ExitApp
F1 to send "if" part the first time I press F1: In else

New Version with Global boolean:

Code: Select all

#SingleInstance,Force
Global boolean := true
F1::MyFunc()
MyFunc() { 
	if (boolean:=!boolean) { 
		ToolTip, in if
	} else {
		ToolTip, In else
	}
}
return
F2::
boolean := true
return
Esc::ExitApp
If I pressed F2, I want F1 to send "if" part the first time I press F1: In else as desired!
Hellbent
Posts: 736
Joined: 23 Sep 2017, 13:34

Re: one key two actions toggle

18 Oct 2019, 03:46

genaro wrote:
18 Oct 2019, 00:08
the first time I press F1
First off I think that we can both agree that based on what his code is doing, we are right in assuming that he doesn't mean for this to happen only on the first time that he presses F1 after having pressed F2.



With that out of the way, I see the meaning of this to be paraphrased as such:
If I pressed F2, I want F1 to send "if" part the first time I press F1:


When I press F2, I want the next time I press F1 to execute the code contained in "if"(Boolean:=!Boolean) and not the "else" part.
if I keep pressing F1 without pressing F2 I want it to use the normal toggle to pick which code gets executed.
Hellbent
Posts: 736
Joined: 23 Sep 2017, 13:34

Re: one key two actions toggle

18 Oct 2019, 03:55

Rohwedder wrote:
18 Oct 2019, 03:24
If I pressed F2, I want F1 to send "if" part the first time I press F1: In else as desired!

I almost feel like you're trolling me here.

I want F1 to send "if" part
In else as desired!
:lol:

Return to “Ask For Help”

Who is online

Users browsing this forum: BreezeB, kentpachi, malcev, RogerWilcoNL, Zudegia, zvit and 210 guests