#Startup [Keysharp]

Evaluates an expression once before capability checks, hotkey setup and the auto-execute section.

#Startup Expression

Parameters

Expression

An expression to evaluate. Its result is ignored. Call a function for multiple statements, or use expression continuation to spread it over several lines.

Remarks

This directive is permitted only at module level, including in included files and imported modules.

Packages are loaded before startup expressions run. Expressions run in the same dependency order as module auto-execution. Within each module, they run in source order, with included files treated as part of the including source.

The expression uses its module's global scope. A function called by the expression uses the function's ordinary scope rules. Assignments in the auto-execute section have not run yet.

Use this directive to initialize a library before callbacks can need it, such as a managed key-state provider.

Error Handling

An uncaught error stops startup. Subsequent startup expressions and the auto-execute section do not run. Handle expected errors with try and catch inside a function.

Auto-execute Section, #Package, Clr, #Import, #Include

Examples

Evaluates a single expression before the auto-execute section.

#Startup OutputDebug("Preparing " A_ScriptName)

OutputDebug("Auto-execute")

Calls a function for multiple initialization statements.

#Startup Initialize()

Initialize() {
    global Settings := Map("Timeout", 1000)
    Settings["Retries"] := 3
}

OutputDebug("Timeout: " Settings["Timeout"])