Page 1 of 1

SendEvent and variable

Posted: 26 Nov 2022, 06:24
by Nod1234
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

Re: SendEvent and variable

Posted: 26 Nov 2022, 07:32
by mikeyww

Code: Select all

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

Re: SendEvent and variable

Posted: 26 Nov 2022, 08:06
by Nod1234
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?

Re: SendEvent and variable

Posted: 26 Nov 2022, 08:37
by mikeyww
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

Re: SendEvent and variable

Posted: 26 Nov 2022, 09:56
by Nod1234
:thumbup: