2 actions for one key Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
KillShotOW
Posts: 4
Joined: 17 Nov 2017, 04:31

2 actions for one key

17 Nov 2017, 04:36

Hi. I'm very VERY new to this, and i need a little help.

I want to press one button, this instance F8. The first time i press it, to do #tab, second time i press it !tab.

Can anyone help? Thank you in advance!
User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

Re: 2 actions for one key

17 Nov 2017, 07:56

Code: Select all

F8::
	SendInput, % (Times = 1) "#{Tab}" : "!{Tab}"
	Times++
	Return

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat

KillShotOW
Posts: 4
Joined: 17 Nov 2017, 04:31

Re: 2 actions for one key

18 Nov 2017, 01:35

Delta Pythagorean wrote:

Code: Select all

F8::
	SendInput, % (Times = 1) "#{Tab}" : "!{Tab}"
	Times++
	Return
"Error: A":" is missing its "?"

Line#
002: Return
---> 003: SendInput, % (Times = 1) "#{Tab}" : "!{Tab}"
004: Times += 1
005: Return
006: Exit

The Program will exit."


Help?
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: 2 actions for one key  Topic is solved

18 Nov 2017, 02:09

So what Delta is doing is called ternary. To correct their code, it would be:

Code: Select all

F8::
	SendInput, % (Times = 1) ? "#{Tab}" : "!{Tab}"
	Times++
	Return
However, this will only produce #{Tab} on the second press of F8; the first will be !{Tab} and all other F8s after will be !{Tab} because Times will always be greater than 1.

To alternate, the variables Times should be changing between two values. A great way to do that is with the logical-not operator ! -- it changes true (1) to false (0) and false (0) to true (1).

I would do it this way, without ternary. And I'll show ternary after this.

Code: Select all

F8::
toggle:=!toggle ; custom variable, you can name it bird or coin or whatever you'd like
If toggle ; if toggle is true or "1"
Send #{Tab}
else
Send !{Tab}
return
Ternary:

Code: Select all

F8::
toggle:=!toggle
Send % (toggle) ? "#{Tab}" : "!{Tab}"
return
You can even consolidate the expression into a single line like this:

Code: Select all

F8::Send % (toggle:=!toggle) ? "#{Tab}" : "!{Tab}"
KillShotOW
Posts: 4
Joined: 17 Nov 2017, 04:31

Re: 2 actions for one key

18 Nov 2017, 23:44

[quote="Exaskryz"]So what Delta is doing is called ternary. To correct their code, it would be:

Code: Select all

F8::
	SendInput, % (Times = 1) ? "#{Tab}" : "!{Tab}"
	Times++
	Return
However, this will only produce #{Tab} on the second press of F8; the first will be !{Tab} and all other F8s after will be !{Tab} because Times will always be greater than 1.

To alternate, the variables Times should be changing between two values. A great way to do that is with the logical-not operator ! -- it changes true (1) to false (0) and false (0) to true (1).

I would do it this way, without ternary. And I'll show ternary after this.

Code: Select all

F8::
toggle:=!toggle ; custom variable, you can name it bird or coin or whatever you'd like
If toggle ; if toggle is true or "1"
Send #{Tab}
else
Send !{Tab}
return
Ternary:

Code: Select all

F8::
toggle:=!toggle
Send % (toggle) ? "#{Tab}" : "!{Tab}"
return
You can even consolidate the expression into a single line like this:

Code: Select all

F8::Send % (toggle:=!toggle) ? "#{Tab}" : "!{Tab}"
[/quote]


Thank you very much!

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 268 guests