2017-08-23 127 views
0

我有一個簡單的.ahk文件,可以在我按下Esc時重新加載當前正在運行的腳本。爲什麼#include打破全局變量?

; reload-hotkey.ahk 

Esc::Reload  ; reload script with Esc 

出於某種原因,導入此文件將導致全局變量來停止正常工作。

; test-file.ahk 

#Include %A_ScriptDir%\reload-hotkey.ahk ; This line causes the problem 
globalString := "Hello" 
^q:: 
    localString := "World" 
    MsgBox '%globalString% %localString'  ; Output: ' World' 
Return 

如果我刪除我的#include聲明,代碼工作正常。

; test-file-2.ahk 

globalString := "Hello" 
^q:: 
    localString := "World" 
    MsgBox '%globalString% %localString%' ; Output: 'Hello World' 
Return 

這隻會發生,如果我包括的文件包括一個熱鍵。如果函數只包含方法或函數,我的代碼將按預期工作。

僅供參考,我使用的是AutoHotkey Unicode 32位1.1.26.01。

爲什麼#include聲明會導致全局變量無法正常工作?

回答

1

變量定義必須出現在任何熱鍵或熱點字符串定義之前。

因此把變量定義放在include語句的上面。

globalString := "Hello" 
#Include %A_ScriptDir%\reload-hotkey.ahk ; This line causes the problem 

有關詳細信息,請參閱Autohotkey文檔中的Auto-execute Section

+0

我會添加像[CoordMode](https://autohotkey.com/docs/commands/CoordMode.htm)和[SetKeyDelay](https://autohotkey.com/docs/commands/SetKeyDelay.htm)還需要放置任何熱鍵或熱鍵定義之後。 –

相關問題