SendEvent and variable

Ask gaming related questions (AHK v1.1 and older)
Nod1234
Posts: 62
Joined: 13 Dec 2021, 12:10

SendEvent and variable

Post by Nod1234 » 26 Nov 2022, 06:24

Hey,
I'm trying to send a variable like this:

Code: Select all

#Persistent
#NoEnv
SendMode Input
SetKeyDelay, 5

a::
variable := A_ThisHotkey
tooltip % variable
SendEvent {%variable%}
Return

^ESC::ExitApp		
Return
But it doesn't work. Has anyone a idea why?
Thanks

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

Re: SendEvent and variable

Post by mikeyww » 26 Nov 2022, 07:32

Code: Select all

$a::Send % SubStr(A_ThisHotkey, 2)
Explained: https://www.autohotkey.com/docs/Hotkeys.htm#prefixdollar

Nod1234
Posts: 62
Joined: 13 Dec 2021, 12:10

Re: SendEvent and variable

Post by Nod1234 » 26 Nov 2022, 08:06

Thank you mikeyww,
that works perfect!
One more question: How can i implement:

Code: Select all

% SubStr(variable, 2)
in this routine:

Code: Select all

SendEvent % on ? "%SubStr(variable, 2)" : ""
like that?

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

Re: SendEvent and variable

Post by mikeyww » 26 Nov 2022, 08:37

Use expressions, because that is what % indicates at the start of an AHK command parameter.

Code: Select all

on := True
$a::
$Up::
Send % on ? "{" SubStr(A_ThisHotkey, 2) "}" : ""
SoundBeep, 1500
Return
When forcing an expression in an AHK command, the % is needed only once per parameter. The rest of the parameter is an expression.

Code: Select all

on := True
$a::
$Up::
expression1 := "{" SubStr(A_ThisHotkey, 2) "}"
expression2 := on ? expression1 : ""
Send % expression2
SoundBeep, 1500
Return

Nod1234
Posts: 62
Joined: 13 Dec 2021, 12:10

Re: SendEvent and variable

Post by Nod1234 » 26 Nov 2022, 09:56

:thumbup:

Post Reply

Return to “Gaming Help (v1)”