Unexpected {

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
wllrjahauthcpebmsh
Posts: 1
Joined: 18 Apr 2022, 12:11

Unexpected {

18 Apr 2022, 12:12

Code: Select all

keys := ["w", "a", "s", "d"] 

Random, keyVar, 1, 4
Random, timeVar, 3500, 10000
Random, timeHoldVar, 100, 300

if (keyVar = 1) Send {w down}
if (keyVar = 2) Send {a down}
if (keyVar = 3) Send {s down}
if (keyVar = 4) Send {d down}

Sleep timeHoldVar

if (keyVar = 1) Send {w up}
if (keyVar = 2) Send {a up}
if (keyVar = 3) Send {s up}
if (keyVar = 4) Send {d up}

sleep, timeVar
reload
I keep getting the error Unexpected { on Line 7. Why?
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: Unexpected {

18 Apr 2022, 12:26

if (keyVar = 1) Send {w down} is not valid syntax

Try:

Code: Select all

if (keyVar = 1) 
    Send {w down}
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Unexpected {

18 Apr 2022, 13:03

Code: Select all

keys := ["w", "a", "s", "d"] 

F12::						; press F12 to execute the following code
Random, keyVar, 1, 4
Random, timeVar, 3500, 10000
Random, timeHoldVar, 100, 300

Send {keys[keyVar] down}
Sleep % timeHoldVar
Send  % "{" . keys[keyVar] . "up}"
Sleep % timeVar
return
Not tested (but corrected as advised by @Helgef see below) :thumbup:
User avatar
Cuadrix
Posts: 236
Joined: 07 May 2017, 08:26

Re: Unexpected {

18 Apr 2022, 13:15

Because Send {w up} is a command.
Autohotkey commands require their own line.

If you want to keep it on the same line as the if statement, you can instead wrap it in a function like this:

Code: Select all

Send(k) {
	Send % k
}

if (keyVar = 1) Send("{w down}")
gregster
Posts: 9085
Joined: 30 Sep 2013, 06:48

Re: Unexpected {

18 Apr 2022, 14:24

Cuadrix wrote:
18 Apr 2022, 13:15
If you want to keep it on the same line as the if statement, you can instead wrap it in a function like this:
Afaics, this is misleading, and no valid/meaningful if-syntax. This would execute Send("{w down}") unconditionally, even if the if-condition is false.


You could use a ternary, though, with your defined function:

Code: Select all

(keyVar = 1) ? Send("{w down}") : ""
(Or, I guess, using commands, with the deprecated legacy syntax mentioned here -> IfEqual: https://www.autohotkey.com/docs/commands/IfEqual.htm#Remarks)
User avatar
Cuadrix
Posts: 236
Joined: 07 May 2017, 08:26

Re: Unexpected {

18 Apr 2022, 16:54

Afaics, this is misleading, and no valid/meaningful if-syntax. This would execute Send("{w down}") unconditionally, even if the if-condition is false.
Well damn. I didn't know that same line if statements weren't allowed. Thought that for sure it would be the same as in other languages. Guess not
Helgef
Posts: 4709
Joined: 17 Jul 2016, 01:02
Contact:

Re: Unexpected {

20 Apr 2022, 03:20

BoBo wrote:
18 Apr 2022, 13:03

Code: Select all

Send {keys[keyVar] down}
This mix of expression and command syntax is not valid. In expression syntax it would need to be,

Code: Select all

send % "{" . keys[keyVar] . " down}"
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Unexpected {

20 Apr 2022, 03:58

@Helgef - I've expected that (sort of) but wasn't sure if "stringyfiing" the curly braces is the way to go. :thumbup:
Well, my iPad won't work as a testing environment that good… :roll:

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: GooGooPark and 124 guests