A Key = 1st time; Action 1 / next time: Action 2 Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Guill
Posts: 128
Joined: 09 Jun 2016, 22:00

A Key = 1st time; Action 1 / next time: Action 2

12 May 2017, 16:52

Hello everyone,

I need to know how I could set the script so that the first time you press a key you do one action (Action 1) and the next time, another (Action 2). The next time, re-do action 1, and so on.

At the moment, I would need it to do only 2 actions, but I would also be interested to know if it is possible to perform with more than two actions, too.


It's possible? Thanks in advance.
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Re: A Key = 1st time; Action 1 / next time: Action 2

12 May 2017, 17:48

Here is one suggestion:

Code: Select all

f1::
	If ifFirst()
		MsgBox, First time you hit F1 button
	Else
		MsgBox, This is not the first time you hit F1 button
Return

ifFirst(){
	Static onlyonce := 1
	If onlyonce {
		onlyonce := 0
		Return 1
	}
	Else
		Return 0
}
A_AhkUser
Posts: 1089
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: A Key = 1st time; Action 1 / next time: Action 2

12 May 2017, 17:49

Among other possibilities:

Code: Select all

Hotkey, !i, MyFunc, on ; the label parameter of the hotkey command can be the name of a function whose contents will be executed when the hotkey is pressed.
return

MyFunc() { ; function 'MyFunc'

static boolean := false, i := 0 ; 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 
	ToolTip % ++i
	} else {
	ToolTip ; without the text parameter the tooltipwill be hidden
	}

}
from this sample code it is simple to implement a code that perform with more than two actions in turns: instead of a boolean value use instead
a number, incremented at each turn and reinitialized once it reach the max number of actions desired.


Hope this helps
Guill
Posts: 128
Joined: 09 Jun 2016, 22:00

Re: A Key = 1st time; Action 1 / next time: Action 2  Topic is solved

12 May 2017, 18:32

Thank you very much to both.

I tried both suggestions, but it only works the first, because, I dont know where I have to put Action 1 and Action 2 in the second one (A_AHKUser's suggestion). Sorry, my knowledge in AHK is little because, I dont know almost nothing about programming.

But, in the first one (Almost_there's) it works only twice (One for Action 1 and one for Action 2).
The third time it do nothing. And from there on, it no longer works.
I need 3º time, Action 1, 4º time, Action 2, and so on.


Can you help me to get it better?
A_AhkUser
Posts: 1089
Joined: 06 Mar 2017, 16:18
GitHub: AAhkUser
Location: France

Re: A Key = 1st time; Action 1 / next time: Action 2

12 May 2017, 18:37

Code: Select all

f1::MyFunc()

MyFunc() { ; function 'MyFunc'

static boolean := false ; 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 
	MsgBox, Action 1
	; content of action 1 goes here
	} else {
	MsgBox, Action 2
	; content of action 2 goes here
	}

}
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Re: A Key = 1st time; Action 1 / next time: Action 2

13 May 2017, 04:23

The code works every time on my computer - are you sure you haven't added something in your own code that make it work differently? Any changes between this code and your code?
Guill
Posts: 128
Joined: 09 Jun 2016, 22:00

Re: A Key = 1st time; Action 1 / next time: Action 2

13 May 2017, 09:45

Thank you Almost_there
This time, your code works but:
1º time: Action 1
2º time and the next ones: Action 2

I need:
1º time: Action 1
2º time: Action 2
3º time: Action 1
4º time: Action 2
And so on.


I copy my code below:

Code: Select all

+::
	If ifFirst()
		Run Notepad
	Else
		Run calc
Return

ifFirst(){
	Static onlyonce := 1
	If onlyonce {
		onlyonce := 0
		Return 1
	}
	Else
		Return 0
}

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], boiler, Chunjee, Google [Bot], NiptheZephyr, Sefrautic, slupka, stuarty27 and 41 guests