BaconCatBug
BaconCatBug

Reputation: 195

How can I place Hotkey bindings at the start/top of an autoexecuting script rather than the end?

Normally, "After the script has been loaded, [AutoHotKey] begins executing at the top line, continuing until a Return, Exit, hotkey/hotstring label, or the physical end of the script is encountered (whichever comes first)."

However, I would like to place my hotkey triggers above the Auto-execute section, for ease of readability and/or to make it simpler for end users to edit the hotkeys to their liking. How can I do this?

Upvotes: 2

Views: 48

Answers (2)

Spyre
Spyre

Reputation: 2344

Alternatively, all you need is to put a return statement at the beginning of your script

With the example:

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

UserVariableOne := 3.14
UserVariableTwo := true
UserVariableThree := "Banana"

return ; <---This line here<<

^Space::
Msgbox, I am a Hotkeyed Messagebox! %UserVariableOne%
return

+Space::
Msgbox, I am another Hotkeyed Messagebox! %UserVariableTwo%
return

Esc::Exitapp

Source

Upvotes: 0

BaconCatBug
BaconCatBug

Reputation: 195

It's very simple. Simply give your Auto-execute section a label, and use a goto command just before your first hotkey bind to skip over parsing these hotkeys as part of the Auto-execute.

Example:

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

UserVariableOne := 3.14
UserVariableTwo := true
UserVariableThree := "Banana"

goto BeginTheScript

^Space::
Msgbox, I am a Hotkeyed Messagebox! %UserVariableOne%
return

+Space::
Msgbox, I am another Hotkeyed Messagebox! %UserVariableTwo%
return

Esc::Exitapp

BeginTheScript:
Msgbox, I am an Auto-execute Messagebox! %UserVariableThree%
return

Upvotes: 0

Related Questions