Page 1 of 1

Unexpected {

Posted: 18 Apr 2022, 12:12
by wllrjahauthcpebmsh

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?

Re: Unexpected {

Posted: 18 Apr 2022, 12:26
by Xtra
if (keyVar = 1) Send {w down} is not valid syntax

Try:

Code: Select all

if (keyVar = 1) 
    Send {w down}

Re: Unexpected {

Posted: 18 Apr 2022, 13:03
by BoBo

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:

Re: Unexpected {

Posted: 18 Apr 2022, 13:15
by Cuadrix
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}")

Re: Unexpected {

Posted: 18 Apr 2022, 14:24
by gregster
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)

Re: Unexpected {

Posted: 18 Apr 2022, 16:54
by Cuadrix
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

Re: Unexpected {

Posted: 20 Apr 2022, 03:20
by Helgef
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}"

Re: Unexpected {

Posted: 20 Apr 2022, 03:58
by BoBo
@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: