put code in a variable?

Get help with using AutoHotkey and its commands and hotkeys
Aso
Posts: 17
Joined: 10 Nov 2016, 14:00

put code in a variable?

10 Nov 2016, 14:04

hi buddys,

i try to make my scripts more easy so im asking my self can i add a part of a script in a variable?

for example

---------------------------
sendvariable =
send {F1}
send {F3}
RETURN


then when where i need it so i can just insert
%sendvariable%

is that possible?
kon
Posts: 1756
Joined: 29 Sep 2013, 17:11

Re: put code in a variable?

10 Nov 2016, 14:18

There are several ways to do somthing similar to what you are describing. Here are some examples (in order of increasing complexity).

Code: Select all

; Using GoSub
; https://autohotkey.com/docs/commands/Gosub.htm
MsgBox, Starting...
gosub, SendLabel
MsgBox, Done.
ExitApp

SendLabel:
    Send, {F1}{F3}
return

Code: Select all

; Using a function
; https://autohotkey.com/docs/Functions.htm#intro
MsgBox, Starting...
SendLabel("{F1}", "{F3}")
MsgBox, Done.
ExitApp

SendLabel(Key1, Key2)
{
    Send, % Key1 Key2
    return
}

Code: Select all

; Using a func object
; https://autohotkey.com/docs/objects/Functor.htm
MsgBox, Starting...
MyFunc := Func("SendLabel").Bind("{F1}", "{F3}")  ; Store a functin object and bind two parameters ("{F1}" and "{F3}")
MyFunc.Call()  ; Call the bound func object
MsgBox, Done.
ExitApp

SendLabel(Key1, Key2)
{
    Send, % Key1 Key2
    return
}
User avatar
Exaskryz
Posts: 2876
Joined: 17 Oct 2015, 20:28

Re: put code in a variable?

10 Nov 2016, 14:22

A command cannot be processed as a variable, to my knowledge.

But what you can do is make the variable like this, using a continuation.

Code: Select all

sendvariable =
(
Send {F1}
Send {F3}
RETURN
)
And if you make a hotkey like !s::Send %sendvariable%, you can press Alt+s to basically paste those lines into your code.
Aso
Posts: 17
Joined: 10 Nov 2016, 14:00

Re: put code in a variable?

10 Nov 2016, 14:24

thanks buddys, gosub is very nice i try it

Return to “Ask For Help”

Who is online

Users browsing this forum: Paddy9888 and 151 guests