Expression + Function ifs in Hotkey Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Expression + Function ifs in Hotkey

Post by hemsith14_ » 19 Dec 2022, 13:16

Hey, how do I combine an expression + a function as a if statement in hotkey?
For example:

Code: Select all

Statement := True
Hotkey, If, % Func && Statement
Hotkey, ^1, Test
Thanks for the help

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: Expression + Function ifs in Hotkey

Post by hemsith14_ » 19 Dec 2022, 15:47

????

User avatar
mikeyww
Posts: 27245
Joined: 09 Sep 2014, 18:38

Re: Expression + Function ifs in Hotkey

Post by mikeyww » 19 Dec 2022, 16:02

Hi,

You can access Statement in your function.

Code: Select all

#SingleInstance Force
Statement := True
; Statement := False
fn := Func("t").Bind(True)
Hotkey, If, % fn
Hotkey, ^1, Test
Hotkey, If
Return

Test:
MsgBox, Test
Return

t(r) {
 Global Statement
 Return r && Statement
}
Or:

Code: Select all

#SingleInstance Force
Statement := True
; Statement := False
go := t(True) && Statement
Hotkey, If, go
Hotkey, ^1, Test
Hotkey, If
Return

Test:
MsgBox, Test
Return

#If go
#If

t(r) {
 Return r
}

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: Expression + Function ifs in Hotkey

Post by hemsith14_ » 19 Dec 2022, 16:15

Thank you,
Is it possible to do it without changing the function at all?

User avatar
mikeyww
Posts: 27245
Joined: 09 Sep 2014, 18:38

Re: Expression + Function ifs in Hotkey

Post by mikeyww » 19 Dec 2022, 16:18

Yes. That is shown in my second example.

Also:

Code: Select all

Statement := True
; Statement := False
x := True
; x := False
key = ^1
Hotkey, If, t(x) && Statement
Hotkey, %key%, Test
Hotkey, If
Return

F3::
x := !x
SoundBeep, 1500
Return

Test:
MsgBox, Test
Return

#If t(x) && Statement
#If

t(r) {
 Return r
}

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: Expression + Function ifs in Hotkey

Post by hemsith14_ » 19 Dec 2022, 16:30

I don't understand it, I'm sorry.

My function is dynamic, as it follows the mouse.
I want to define that the hotkeys will only occur if the function returns the right value (which I had worked with only Hotkey, If, % Function) and when the statement is true but without changing anything about the function.

User avatar
mikeyww
Posts: 27245
Joined: 09 Sep 2014, 18:38

Re: Expression + Function ifs in Hotkey

Post by mikeyww » 19 Dec 2022, 16:41

OK then.

Code: Select all

#SingleInstance Force
theRightValue := 3.141
Statement     := True
x              = 96.5
key            = ^1
Hotkey, If, t(x) = theRightValue && Statement
Hotkey, %key%, Test
Hotkey, If
Return

Test:
MsgBox, Test
Return

#If t(x) = theRightValue && Statement
#If

t(r) {
 abc := r + 9
 ; Return 2
 Return 3.141
}

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: Expression + Function ifs in Hotkey

Post by hemsith14_ » 19 Dec 2022, 16:50

I'm really trying to understand you code but I can't. What is the most simple way to do it?

User avatar
mikeyww
Posts: 27245
Joined: 09 Sep 2014, 18:38

Re: Expression + Function ifs in Hotkey

Post by mikeyww » 19 Dec 2022, 16:55

I think that one of us has to post some code. You post yours, or I make up an example. Thus, I made up an example that includes your hotkey and your Statement. I can't get it any simpler, but happy to answer any questions about it.

The first Hotkey, If line checks both the function and the statement. If the condition is met, then the hotkey is active. Otherwise, it isn't.

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: Expression + Function ifs in Hotkey

Post by hemsith14_ » 19 Dec 2022, 17:16

I don't understand why it is so complicated. All I want is to add an if statement to something that I already have working.

This is working for me.

Code: Select all

Hotkey, If, % FUNCTION
I just want to tell AHK - the following hotkeys should work only if the thing that happens above is working as before and add to add if the following statement is true too.

Just like when doing
#If this && that.

User avatar
mikeyww
Posts: 27245
Joined: 09 Sep 2014, 18:38

Re: Expression + Function ifs in Hotkey

Post by mikeyww » 19 Dec 2022, 17:24

I will provide what I understand about this use of Hotkey; others may have more details or information. I believe that the choices are that you can use an expression or a function object. Therefore, you can build your Statement into your function (sounds like not an option for you), or you can provide an expression instead of a function object. Therefore, the example that I posted uses an expression that combines your function call with your Statement. It should work for what you are trying to achieve.

Making this work involves two parts. The first part is stating your expression. The example is repeated here below.

Code: Select all

Hotkey, If, t(x) = theRightValue && Statement
The second part is also using the same expression in an #If directive elsewhere in the script.

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: Expression + Function ifs in Hotkey

Post by hemsith14_ » 19 Dec 2022, 17:28

the function I use is

Code: Select all

TEST := Func("Rolls").Bind(CX1, Y1, X2, Y2)
Then:

Code: Select all

Hotkey, If, % TEST
What do I have to change?

I don't understand why you added all the numbers with dots and x. How is it related to what I'm trying to achieve?

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: Expression + Function ifs in Hotkey

Post by hemsith14_ » 19 Dec 2022, 17:38

Let's say this function returns the right numbers:

Code: Select all

TEST := Func("Rolls").Bind(CX1, Y1, X2, Y2)
Now this works:

Code: Select all

Hotkey, If, % TEST
Where do I add the statement ?

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: Expression + Function ifs in Hotkey

Post by hemsith14_ » 19 Dec 2022, 18:05

????

User avatar
mikeyww
Posts: 27245
Joined: 09 Sep 2014, 18:38

Re: Expression + Function ifs in Hotkey  Topic is solved

Post by mikeyww » 19 Dec 2022, 18:11

I think you want

Code: Select all

Hotkey, If, Rolls(CX1`, Y1`, X2`, Y2) && Statement

Code: Select all

#SingleInstance Force
Statement := True
CX1 := Y1 := X2 := Y2 := 54
Hotkey, If, Rolls(CX1`, Y1`, X2`, Y2) && Statement
Hotkey, ^1, Test, On
Hotkey, If
Return

F3::
Statement := !Statement
SoundBeep, 1500
Return

Test:
MsgBox, Test
Return

#If Rolls(CX1, Y1, X2, Y2) && Statement
#If

Rolls(CX1, Y1, X2, Y2) {
 Return True
}
Last edited by mikeyww on 19 Dec 2022, 18:40, edited 2 times in total.

hemsith14_
Posts: 296
Joined: 07 Nov 2020, 08:37

Re: Expression + Function ifs in Hotkey

Post by hemsith14_ » 19 Dec 2022, 18:27

Oh yes, I think that is it.

Could you explain how it's done?
What is the effect of " ` "?

User avatar
mikeyww
Posts: 27245
Joined: 09 Sep 2014, 18:38

Re: Expression + Function ifs in Hotkey

Post by mikeyww » 19 Dec 2022, 18:30

Code: Select all

Hotkey, If, Rolls(CX1`, Y1`, X2`, Y2) && Statement
When the hotkey is triggered, this expression is evaluated. It calls the function, and then does a logical AND with Statement. If the condition is met, then the hotkey subroutine executes; otherwise, it does not.
The Hotkey command uses the string that you pass to it, not the original source code. Commas and deref chars (percent signs) are interpreted before the command is called, so may need to be escaped if they are part of the original expression. Escape sequences are resolved when the script loads, so only the resulting characters are considered
The expression statement is repeated in an #If directive later in the script.
Expression must be an expression which has been used with the #If directive elsewhere in the script. Although this command is unable to create new expressions, it can create new hotkeys using an existing expression.

Post Reply

Return to “Ask for Help (v1)”