這是我的腳本的一部分。如果它沒有運行,它將通過熱鍵運行程序,或者在其他情況下顯示/隱藏它的窗口。Autohotkey,與對象分離鑰匙
ConsolePath := "cmd.exe"
ConsoleWndClass := "ConsoleWindowClass"
CalculatorPath := "calc.exe"
CalculatorWndClass := "CalcFrame"
#s::
RunOrToggleActive(ConsolePath, ConsoleWndClass)
return
#c::
RunOrToggleActive(CalculatorPath, CalculatorWndClass)
return
RunOrToggleActive(path, wndClass) {
SplitPath, path, process
Process, Exist, %process%
If !ErrorLevel {
Run, %path%
}
else {
ToggleActive(wndClass)
}
}
ToggleActive(wndClass)
{
IfWinNotActive, % "ahk_class " wndClass
{
WinActivate, % "ahk_class " wndClass
}
else
{
WinMinimize, % "ahk_class " wndClass
}
}
工作正常,但這種方法的問題是添加新的程序和熱鍵非常費力。必須添加2個變量,複製熱鍵處理程序代碼,替換熱鍵,替換變量。如果我想爲每個程序添加新的熱鍵功能(例如!#s和!#c即使它已經運行也運行另一個程序實例),我將不得不重複新的代碼。我的實際腳本有7個程序,編輯起來非常困難。
我想它是這樣的(半僞代碼)工作:
appDescs := Object()
appDescs.Insert(new ProgramDesc("cmd.exe", "ConsoleWindowClass", "s"))
appDescs.Insert(new ProgramDesc("calc.exe", "CalcFrame", "c"))
#"some key"{
find key in appDescs array and RunOrToggleActive(for correspondig program)
}
!#"some key"{
find key in appDescs array and RunNewInstance(for correspondig program)
}
RunOrToggleActive(programDesc) {
...
}
RunNewInstance(programDesc) {
...
}
Class ProgramDesc {
__New(path, wndClass, key) {
this.path := path, this.wndClass := wndClass, this.key := key
}
}
我不知道如何實現#"some key"{
行爲。如果有人會根據上面的模式重寫代碼(或者建議更好的代碼),我會非常感激。
我試圖修改我的代碼,但它不起作用。在第二個聲明中,我沒有找到熱鍵#n的錯誤。 'fn1:= Func(「RunOrToggleApp」)。綁定(appDesc)' 'Hotkey,%「#」appDesc.key,%fn1,on' – grabantot
在上面的答案中增加了完整的函數版本。 – lintalist
謝謝,原來問題是我已經ahk v1.15,更新到1.22後,它工作正常。 – grabantot