In older version (maybe around 2020/03) no this problem.
The different in below. Just let people know version's different.
Code: Select all
;;; old version
; If variable not define it seens default define boolean False
; use #if
; gui listview syntax
LV := helper_gui.Add("ListView", "r80 w430", "keywords|refenence")
;;; new version
; can't use if statement estimate no define variable
; use #HotIf
; gui listview syntax
LV := helper_GUI.Add("ListView", "r80 w430", ["keywords", "refenence"])
If I active gui_main() than active gui_search than I active a search than it need close main_gui by main_hwnd.
But in new version searchButton_Click function's "WinClose(main_hwnd)" show error "Target window not found."
In older version I can close search_gui by this method. So looks like global variable have different behavior in new version.
Test method is
- run host.ahk
- press alt-q
- press space
- type any string in text gui
- press enter
Code: Select all
; host.ahk
SoundBeep(300,150)
DetectHiddenWindows True
#Include gui.ahk
#Include commands.ahk
#Include lib.ahk
Alt & q::
{
if WinExist(main_hwnd)
{
WinClose(main_hwnd)
Return
}
else
{
gui_main()
if WinExist(search_hwnd)
WinClose(search_hwnd)
Return
}
}
Code: Select all
; gui.ahk
global main_hwnd := "", search_hwnd := "", help_hwnd := ""
gui_main()
{
beginnerSearchHint := "→ h{Space}查詢"
(main_gui := Gui.New()).Opt("+AlwaysOnTop -caption")
main_gui.BackColor := "FFFFFF"
global main_hwnd := main_gui.hwnd
main_gui.OnEvent("Escape",(this) => this.Hide()) ; 需要在 gui active 的狀態下才能關閉
((ref_main := main_gui.Add("Edit", "r0 w220 h30 -vscroll -Wrap -WantReturn +Lowercase", beginnerSearchHint)).OnEvent('Change', 'DetectUserInput'))
ref_main.SetFont("s14", "Verdana")
main_gui.Show()
}
gui_search(guiTitle, option, searchTip)
{
(search_GUI := Gui.New()).Opt("-caption -Border +Owner")
search_GUI.BackColor := "C0C0C0"
global search_hwnd := search_GUI.hwnd
search_GUI.OnEvent("Escape", (this) => this.Hide())
(ref_search_title := search_GUI.Add("Text","r0 w220 h30", guiTitle)).SetFont("s16", "Verdana")
(ref_search := search_GUI.Add("Edit", "r0 w220 h30 -WantReturn -Wrap -vscroll", searchTip)).OnEvent('Change', 'searchDetectUserInput')
ref_search.SetFont("s16", "cBlue")
; 搜索 GUI
searchDetectUserInput(GuiCtrlObj, Info)
{
(search_GUI.Add("Button", "yp w0 Default", "Default Hidden")).OnEvent("Click", "searchButton_Click")
searchStr := GuiCtrlObj.Value
searchButton_Click(*)
{
searchByOption(searchStr, option)
WinClose(main_hwnd)
}
Return
}
search_GUI.Show()
}
searchByOption(inputStr, optionString)
{
if (optionString == "google")
searchUrl := "http://www.google.com/search?q=" inputStr
else if (optionString == "autohotkey")
searchUrl := "https://www.google.com/search?num=50&safe=off&site=&source=hp&q=autohotkey%20" inputStr "&btnG=Search&oq=&gs_l="
Run(searchUrl)
Return
}
regexHelp()
{
helper_GUI := Gui.New()
global help_hwnd := helper_GUI.hwnd
LV := helper_GUI.Add("ListView", "r80 w430", ["關鍵字", "說明"])
; 點擊時顯示清單行號
LV.OnEvent("Click", "LV_Click")
LV_Click(LV, RowNumber)
{
RowText := LV.GetText(RowNumber) ; Get the text from the row's first field.
ToolTip( RowNumber " line. keyword: " RowText)
SetTimer () => ToolTip(), -2000
}
Loop read, "commands.ahkl"
{
If Substr(A_LoopReadLine, 1, 1) != ";"
{
commandKeyStr := "userInputStr =="
EndStr := '")'
If InStr(A_LoopReadLine, commandKeyStr, false) ; 搜索不區分大小寫
{
; 關鍵字字串
keyPos := InStr(A_LoopReadLine, commandKeyStr)
headPos := keyPos + 17
endPos := InStr(A_LoopReadLine, EndStr)
diffs := endPos - headPos
baseText := SubStr(A_LoopReadLine, headPos, diffs)
commandStr := StrReplace(baseText, " ", "{Space}")
; 說明字串
helpStrHead := '") `;'
hPos := InStr(A_LoopReadLine, helpStrHead) + 4
endPos2 := StrLen(A_LoopReadLine) - hPos + 1
helpCommentText := SubStr(A_LoopReadLine, hPos, endPos2)
LV.Add(, commandStr, helpCommentText)
LV.ModifyCol ; Auto-size each column to fit its contents.
LV.ModifyCol(2, "Sort") ; 以第2欄排序
}
}
}
helper_GUI.Show()
}
#Hotif WinExist(help_hwnd)
{
Esc::WinClose(help_hwnd)
;Space::WinClose(help_hwnd)
;Enter::WinClose(help_hwnd)
return
}
#Hotif
Code: Select all
; commands.ahk
#1::switchDesktop()
DetectUserInput(GuiCtrlObj, Info)
{
userInputStr := GuiCtrlObj.Value
if (userInputStr == "rel") ; 其它 - Reload Script
{
GuiCtrlObj.Gui.Submit(Hide := true)
Sleep(500)
Reload
}
else if (userInputStr == "h ") ; 搜索 - 關鍵字列表 show command list
{
GuiCtrlObj.Gui.Submit(Hide := true)
regexHelp()
}
else if (userInputStr == " ") ; 搜索 - Search 搜索
{
GuiCtrlObj.Gui.Submit(Hide := true)
gui_search("谷歌搜索", "google","")
}
else if (userInputStr == "g ") ; 搜索 - Search 搜索
{
GuiCtrlObj.Gui.Submit(Hide := true)
gui_search("谷歌搜索", "google","")
}
else if (userInputStr == "a ") ; 搜索 - Search AutoHotkey related stuff
{
gui_search("AutoHotkey 搜索", "autohotkey","")
GuiCtrlObj.Gui.Submit(Hide := true)
}
}
Code: Select all
; lib.ahk
global var_switch := False
switchDesktop()
{
global ;var_switch := False
if (var_switch)
{
SendEvent "^#{Right}"
var_switch := !var_switch
}
else
{
SendEvent "^#{Left}"
var_switch := !var_switch
}
Return
}
Another issue about global variable
I have 3 script file that call it host2.ahk file.ahk lib.ahk. This script can use win-1 switch Windows 10 virtual desktop between two desktop.
If only include lib.ahk in shortcut.ahk. When I use shortcut active switchDesktop() function show error "This variable has not been assigned a value."
But if include lib.ahk and inclued shortcut.ahk in host.ahk will no error.
Code: Select all
; host2.ahk
#Include .\lib.ahk
#Include .\shortcut.ahk
Code: Select all
; shortcut.ahk
#Include .\lib.ahk
#1::switchDesktop()
Code: Select all
; lib.ahk
global var_switch := False
switchDesktop()
{
global
if (var_switch)
{
SendEvent "^#{Right}"
var_switch := False
}
else
{
SendEvent "^#{Left}"
var_switch := True
}
}