#If Expression #ElIf Expression #Else #EndIf #Define Name #Undef Name
Selects which source is compiled. Only the active branch reaches the compiler; excluded code is removed before parsing, so it need not be valid beyond its conditional structure. For context-sensitive hotkeys, use #HotIf.
#If WINDOWS
MsgBox "Windows"
#ElIf LINUX
MsgBox "Linux"
#ElIf OSX
MsgBox "macOS"
#EndIf
#If !(WINDOWS || LINUX)
MsgBox "Not Windows or Linux"
#EndIf
#Define FEATURE_X
#If FEATURE_X
MsgBox "Feature X enabled"
#EndIf
Directives are resolved per physical line, so a block may split a single statement:
x := (
#If WINDOWS
1
#Else
2
#EndIf
)
Each directive must be on its own line.
Symbol names are case-insensitive and carry no value; a symbol is either defined or it is not. The following are predefined:
| Symbol | Defined when |
|---|---|
KEYSHARP | Always. |
WINDOWS, LINUX, OSX | Exactly one, matching the platform the running Keysharp build targets. |
DEBUG | Only in a debug build of Keysharp itself. |
X64, ARM64, X86, ARM | Exactly one, matching A_ProcessArch. |
#Define adds a symbol from that point in the file onward and #Undef removes one. --define:NAME predefines symbols for a whole compilation, including any module it imports.
A condition may combine defined symbols, true and false, integers (zero is false, in any spelling), !/not, &&/and, ||/or, and parentheses. An undefined symbol is false. A name which is both defined and spelled true or false is treated as the symbol.
Anything else, such as #If defined(WINDOWS) or #If WINDOWS == 1, is a compile-time error, even in an excluded branch.
A condition after #Else or #EndIf is a compile-time error, as are unbalanced or misplaced directives such as a missing #EndIf or an #ElIf after #Else.
A #Define made in an #Included file remains defined in the including file. An #Imported module does not see the importing script's symbols; use --define to configure one.
#Error Message #Warning Message
Emits Message when the script is compiled, reported against the directive's own line. #Error fails the compilation; #Warning does not, and is written to standard error while the script still runs. Both are commonly paired with conditional compilation:
#If !WINDOWS #Warning This script has only been tested on Windows. #EndIf
The message is the rest of the line, taken literally. Both directives are permitted anywhere a directive is, including inside a function, class or block.
#Warn settings do not affect #Warning, and a compiled executable does not report it when it runs.
#App, #TrayIcon, Compiling a Script, command-line switches, #Warn, #HotIf