A Key = 1st time; Action 1 / next time: Action 2 Topic is solved
A Key = 1st time; Action 1 / next time: Action 2
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.
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.
- Almost_there
- Posts: 404
- Joined: 30 Sep 2014, 10:32
Re: A Key = 1st time; Action 1 / next time: Action 2
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
}
Re: A Key = 1st time; Action 1 / next time: Action 2
Among other possibilities:
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
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
}
}
a number, incremented at each turn and reinitialized once it reach the max number of actions desired.
Hope this helps
Re: A Key = 1st time; Action 1 / next time: Action 2 Topic is solved
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?
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?
Re: A Key = 1st time; Action 1 / next time: Action 2
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
}
}
Re: A Key = 1st time; Action 1 / next time: Action 2
Thank you again!!
- Almost_there
- Posts: 404
- Joined: 30 Sep 2014, 10:32
Re: A Key = 1st time; Action 1 / next time: Action 2
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?
Re: A Key = 1st time; Action 1 / next time: Action 2
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:
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
}