設定顯示或者隱藏MasterSeeker和Total Commander主視窗的快捷鍵的AutoHotkey指令碼2024.10.21 =========

QQ595076941發表於2024-10-21

設定顯示或者隱藏MasterSeeker和Total Commander主視窗的快捷鍵的AutoHotkey指令碼2024.10.21 =========

;======== 設定顯示或者隱藏MasterSeeker和Total Commander主視窗的快捷鍵的AutoHotkey指令碼2024.10.21 =========
; 此指令碼從此行開始
; D:\app\RegHotkey\RegHotkey.ahk
; 滑鼠右擊AHK托盤圖示顯示選單並提示所有快捷鍵
; AutoHotkey版本1.1

;======== 強制自身以 ansi + 管理員許可權 執行,編譯此指令碼時選擇 ANSI 32位 =========
runwith("admin","ansi")

;-----------------------------------
; 多程序熱鍵——快速響應隨時中止 v2.1  By FeiYue
;
; 1、一般的多程序熱鍵,每次按熱鍵都會重新啟動一個程序
;    這會造成熱鍵響應延遲,和頻繁啟動、退出程序。
;    這個庫利用執行緒疊加和雙程序交替重啟的設計解決了上述問題
;
; 2、Go.Go("標籤名或函式名", 序號可省略)  啟動子程序
;    Go.Go("清理標籤或為空", 序號可省略)  停止子程序
;-----------------------------------
Class Go {  ;// 類開始
init() {
	static h:=Go.init()
#NoEnv
#NoTrayIcon
#SingleInstance force
#MaxThreads 255
	if (A_Args.Length()>0)
	{
		Suspend, On
		flag:="Ahk_Sub"
		s:=RegExReplace(A_Args[1], "\s")
		this.WinKill(flag "[" s "]")
		r:=this.Menu_Go.Bind(this)
		Menu, MyMenu, Add, Run, % r
		Gui, %flag%: Add, Text, 0x80 -Wrap
		Gui, %flag%: Menu, MyMenu
		Gui, %flag%: Show, Hide, % flag "[" s "]"
		return
Ahk_SubGuiClose:
	Critical
	WinMenuSelectItem,,, 1&
return
}
else
{
	flag:="Ahk_Main"
	this.WinKill(flag)
	Gui, %flag%: Show, Hide, % flag
	OnExit(this.WinKill.Bind(this,"Ahk_Sub"))
	Menu, Tray, Icon
}
}
Menu_Go() {
	ControlGetText, s
	s:=Trim(s)
	if (s="Reload")
	{
		WinGetTitle, s
		this.RunAhk(RegExReplace(s,"Ahk_Sub\[|]"))
		ExitApp
	}
	if IsLabel(s)
		gosub, %s%
	else if IsFunc(s)
		%s%()
	loop
		Sleep, 60000
}
WinKill(flag) {
	DetectHiddenWindows, On
	WinGet, list, List, % flag " ahk_class AutoHotkeyGUI"
	loop % list
		IfWinExist, % "ahk_id " list%A_Index%
		{
			WinGet, pid, PID
			WinClose, ahk_class AutoHotkey ahk_pid %pid%,, 3
			IfWinExist,,, Process, Close, %pid%
		}
	DetectHiddenWindows, Off
}
RunAhk(s="") {
	if (A_IsCompiled)
		Run, "%A_ScriptFullPath%" /f %s%
	else
		Run, "%A_AhkPath%" /f "%A_ScriptFullPath%" %s%
}
; 一個程序疊加超過100個執行緒就切換另一個程序,原程序重啟
Go(LabelOrFuncName="", index=0) {
	static arr:=[]
	DetectHiddenWindows, On
	SetFormat, IntegerFast, d
	index:=Floor(index), (!arr[index] && arr[index]:=[0,0])
		, ii:=Floor(arr[index][1]), jj:=Floor(arr[index][2])
		, tt:="Ahk_Sub[" index "_" jj "]"
	if (++ii>100)
	{
		IfWinExist, % tt
		{
			ControlSetText,, Reload
			WinClose
		}
		else
			this.RunAhk(index "_" jj)
		arr[index][2]:=jj:=!jj, ii:=1
			, tt:="Ahk_Sub[" index "_" jj "]"
	}
	arr[index][1]:=ii
	IfWinNotExist, % tt
	{
		this.RunAhk(index "_" jj)
		WinWait, % tt,, 3
	}
	IfWinExist, % tt
	{
		ControlSetText,, % LabelOrFuncName
		WinClose
	}
	DetectHiddenWindows, Off
}
}  ;// 類結束


;======== 新增托盤圖示右鍵選單 =========
; 滑鼠右擊AHK托盤圖示顯示選單並提示所有快捷鍵
Menu, Tray, Add,按Alt+Z終止此指令碼, ExitAppMenuHandler  ; 建立新選單項.
Menu, Tray, Add,雙擊Ctrl鍵2秒後關閉顯示器螢幕電源, PowerOffDisplay   ; 建立新選單項.
Menu, Tray, Add,按~鍵顯示或者隱藏Maye Lite, PowerOffDisplay  ; 請在Maye Lite中設定快捷鍵熱鍵
Menu, Tray, Add,按F3鍵顯示或者隱藏MasterSeeker, ccc  ; 建立新選單項.
Menu, Tray, Add,按F4鍵顯示或者隱藏Total Commander, aaa  ; 建立新選單項.
Hotkey,!z,ExitAppMenuHandler
ExitAppMenuHandler()
{
	ExitApp
}

;======== 設定關閉顯示器的快捷鍵為 雙擊 Ctrl鍵 =========
Control::
	if (A_PriorHotkey <> "Control" or A_TimeSincePriorHotkey > 400)
	{ KeyWait, Control
		return
	}
	; MsgBox 雙擊
	Go.Go("PowerOffDisplay")
return
PowerOffDisplay:
	Sleep,2000 ;暫停2秒,防止受鍵盤操作影響,關閉顯示器後又立即開啟。
	;~ 關閉顯示器
	SendMessage, 0x112, 0xF170, 2,, Program Manager
return

;======== 設定顯示或者隱藏 MasterSeeker 主視窗的快捷鍵為 F3 鍵 =========
; (F3鍵位於鍵盤主鍵區數字鍵4正上方)
F3::Go.Go("ccc")
ccc:
	; WinGetPos, , , W, ,MasterSeeker 1.5.1 by DxCK ahk_exe MasterSeeker.exe
	; if (WinActive("MasterSeeker 1.5.1 by DxCK" ahk_exe MasterSeeker.exe) and W>700)
	IfWinActive,MasterSeeker 1.5.1 by DxCK ahk_exe MasterSeeker.exe
	{
		; WinHide,MasterSeeker 1.5.1 by DxCK ahk_exe MasterSeeker.exe
		WinMinimize,MasterSeeker 1.5.1 by DxCK ahk_exe MasterSeeker.exe
	}
	else
	{
		DetectHiddenWindows,On
		SetTitleMatchMode,2
		IfWinNotExist,ahk_exe MasterSeeker.exe
		{
			IfWinNotExist,MasterSeeker 1.5.1 by DxCK
			{
				IfWinNotExist,ahk_exe MasterSeeker.exe
				{
					IfWinNotExist,MasterSeeker 1.5.1 by DxCK
					{
						IfExist,D:\Prog\MasterSeeker\MasterSeeker.exe
						{
							Run,D:\Prog\MasterSeeker\MasterSeeker.exe
						}
						else
						{
							MsgBox,0,,找不到檔案 D:\Prog\MasterSeeker\MasterSeeker64.exe,5
							ExitApp
						}
					}
				}
			}
		}
		SetTimer,WinActivateMasterSeeker,Delete
		SetTimer,WinActivateMasterSeeker,500
	}
return
WinActivateMasterSeeker()
{
	WinShow,MasterSeeker 1.5.1 by DxCK ahk_exe MasterSeeker.exe
	WinGet, WinMSID, ID,MasterSeeker 1.5.1 by DxCK ahk_exe MasterSeeker.exe
	DllCall("SwitchToThisWindow", "UInt", WinMSID, "UInt", 1)
	WinActivate,MasterSeeker 1.5.1 by DxCK ahk_exe MasterSeeker.exe
	WinMove,MasterSeeker 1.5.1 by DxCK ahk_exe MasterSeeker.exe,,0,0,A_ScreenWidth,A_ScreenHeight
	WinMaximize,MasterSeeker 1.5.1 by DxCK ahk_exe MasterSeeker.exe
	IfWinActive,MasterSeeker 1.5.1 by DxCK ahk_exe MasterSeeker.exe
	{
		loop,5
		{
			WinMove,MasterSeeker 1.5.1 by DxCK ahk_exe MasterSeeker.exe,,A_ScreenWidth/2,20,A_ScreenWidth/2-10,A_ScreenHeight-90
			; 注意: MasterSeeker 的搜尋框控制元件的名是 Filename ,很奇怪,很神奇,
			; MasterSeeker 的搜尋框控制元件不是類名 MasterSeeker 1.5.1 by DxCK ahk_exe MasterSeeker.exe7,也不是類名 Edit1
			; ControlFocus,In Directory,MasterSeeker 1.5.1 by DxCK ahk_exe MasterSeeker.exe
			; 設定輸入焦點到視窗的指定控制元件上.
			ControlFocus,Filename,MasterSeeker 1.5.1 by DxCK ahk_exe MasterSeeker.exe
		}
		ControlSend,Filename,{LControl Down}a{LControl Up},MasterSeeker 1.5.1 by DxCK ahk_exe MasterSeeker.exe
		SetTimer,WinActivateMasterSeeker,Delete
		Go.Go()
		return
	}
}

;======== 設定顯示或者隱藏 Total Commander 11.01 主視窗的快捷鍵為 F4 鍵 =========
;(F4鍵位於鍵盤主鍵區數字鍵5正上方
; Total Commander 11.01使用支援按照“拼音首字母”序列篩選過濾檔案列表的增強版QuickSearch eXtended 2.2.7外掛)
F4::Go.Go("aaa")
aaa:
	; WinGetPos, , , W, ,Total Commander (x64) ahk_exe TotalCMD64.exe
	; if (WinActive("Total Commander (x64)" ahk_exe TotalCMD64.exe) and W>400)
	DetectHiddenWindows,On
	SetTitleMatchMode,2
	;等待指定標題視窗出現
	;WinWait, Total Commander (x64) 11.01 - User ahk_class TTOTAL_CMD ahk_exe TOTALCMD64.EXE, [軟體]  2.47
	IfWinActive,Total Commander (x64) 11.01 ahk_class TTOTAL_CMD ahk_exe TotalCMD64.exe
	{
		WinMinimize,Total Commander (x64) 11.01 ahk_class TTOTAL_CMD ahk_exe TotalCMD64.exe
	}
	else
	{
		WinShow,Total Commander (x64) 11.01 ahk_class TTOTAL_CMD ahk_exe TotalCMD64.exe
		WinGet, WinTCID, ID,Total Commander (x64) 11.01 ahk_class TTOTAL_CMD ahk_exe TotalCMD64.exe
		DllCall("SwitchToThisWindow", "UInt", WinTCID, "UInt", 1)
		WinActivate,Total Commander (x64) 11.01 ahk_class TTOTAL_CMD ahk_exe TotalCMD64.exe
		WinMove,Total Commander (x64) 11.01 ahk_class TTOTAL_CMD ahk_exe TotalCMD64.exe,,0,0,A_ScreenWidth,A_ScreenHeight
		WinMaximize,Total Commander (x64) 11.01 ahk_class TTOTAL_CMD ahk_exe TotalCMD64.exe
		IfWinNotExist,Total Commander (x64) 11.01 ahk_class TTOTAL_CMD ahk_exe TotalCMD64.exe
		{
			IfWinNotExist,ahk_exe TOTALCMD64.EXE
			{
				IfExist,D:\Prog\TotalCMD\TotalCMD64.exe
				{
					Run,D:\Prog\TotalCMD\TotalCMD64.exe
					Go.Go()
					return
				}
				else
				{
					MsgBox,0,,找不到檔案 D:\Prog\TotalCMD\TotalCMD64.exe,5
					ExitApp
				}
			}
			SetTimer,WinActivateTC,Delete
			SetTimer,WinActivateTC,500
		}
	}
	Go.Go()
return
WinActivateTC()
{
	WinShow,Total Commander (x64) 11.01 ahk_class TTOTAL_CMD ahk_exe TotalCMD64.exe
	WinGet, WinTCID, ID,Total Commander (x64) 11.01 ahk_class TTOTAL_CMD ahk_exe TotalCMD64.exe
	DllCall("SwitchToThisWindow", "UInt", WinTCID, "UInt", 1)
	WinActivate,Total Commander (x64) 11.01 ahk_class TTOTAL_CMD ahk_exe TotalCMD64.exe
	WinMove,Total Commander (x64) 11.01 ahk_class TTOTAL_CMD ahk_exe TotalCMD64.exe,,0,0,A_ScreenWidth,A_ScreenHeight
	WinMaximize,Total Commander (x64) 11.01 ahk_class TTOTAL_CMD ahk_exe TotalCMD64.exe
	IfWinActive,Total Commander (x64) 11.01 ahk_class TTOTAL_CMD ahk_exe TotalCMD64.exe
	{
		SetTimer,WinActivateTC,Delete
		Go.Go()
		return
	}
}

; ===============================================================
; 強制自身程序以 管理員許可權 或 普通許可權 或 ANSI 或 U32 或 U64 版本執行。
; 例1: runwith("admin","u32") 強制自身以 u32 + 管理員許可權 執行。
; 例2: runwith("","ansi")     強制自身以 ansi 版本執行(許可權不變)。
; 例3: runwith("normal")      強制自身以 普通許可權 執行(版本不變)。
RunWith(RunAsAdmin:="Default", ANSI_U32_U64:="Default")
{
	; 格式化預期的模式
	switch, RunAsAdmin
	{
		case "Normal","Standard","No","0":		RunAsAdmin:=0
		case "Admin","Yes","1":								RunAsAdmin:=1
		case "default":												RunAsAdmin:=A_IsAdmin
default:															RunAsAdmin:=A_IsAdmin
	}
switch, ANSI_U32_U64
{
	case "A32","ANSI","A":								ANSI_U32_U64:="AutoHotkeyA32.exe"
	case "U32","X32","32":								ANSI_U32_U64:="AutoHotkeyU32.exe"
	case "U64","X64","64":								ANSI_U32_U64:="AutoHotkeyU64.exe"
	case "default":												ANSI_U32_U64:="AutoHotkey.exe"
default:															ANSI_U32_U64:="AutoHotkey.exe"
	}
; 獲取傳遞給 “.ahk” 的使用者引數(不是 /restart 之類傳遞給 “.exe” 的開關引數)
for k, v in A_Args
{
	if (RunAsAdmin=1)
	{
		; 轉義所有的引號與轉義符號
		v:=StrReplace(v, "\", "\\")
		v:=StrReplace(v, """", "\""")
		; 無論引數中是否有空格,都給引數兩邊加上引號
		; Run       的內引號是 "
		ScriptParameters  .= (ScriptParameters="") ? """" v """" : A_Space """" v """"
	}
	else
	{
		; 轉義所有的引號與轉義符號
		; 注意要轉義兩次 Run 和 RunAs.exe
		v:=StrReplace(v, "\", "\\")
		v:=StrReplace(v, """", "\""")
		v:=StrReplace(v, "\", "\\")
		v:=StrReplace(v, """", "\""")
		; 無論引數中是否有空格,都給引數兩邊加上引號
		; RunAs.exe 的內引號是 \"
		ScriptParameters .= (ScriptParameters="") ? "\""" v "\""" : A_Space "\""" v "\"""
	}
}
; 判斷當前 exe 是什麼版本
if (!A_IsUnicode)
	RunningEXE:="AutoHotkeyA32.exe"
else if (A_PtrSize=4)
	RunningEXE:="AutoHotkeyU32.exe"
else if (A_PtrSize=8)
	RunningEXE:="AutoHotkeyU64.exe"
; 執行模式與預期相同,則直接返回。 ANSI_U32_U64="AutoHotkey.exe" 代表不對 ahk 版本做要求。
if (A_IsAdmin=RunAsAdmin and (ANSI_U32_U64="AutoHotkey.exe" or ANSI_U32_U64=RunningEXE))
	return
; 如果當前已經是使用 /restart 引數重啟的程序,則報錯避免反覆重啟導致死迴圈。
else if (RegExMatch(DllCall("GetCommandLine", "str"), " /restart(?!\S)"))
{
	預期許可權:=(RunAsAdmin=1) ? "管理員許可權" : "普通許可權"
	當前許可權:=(A_IsAdmin=1)  ? "管理員許可權" : "普通許可權"
	ErrorMessage=
	(LTrim
		預期使用: %ANSI_U32_U64%
		當前使用: %RunningEXE%
		預期許可權: %預期許可權%
		當前許可權: %當前許可權%
		程式即將退出。
	)
	MsgBox 0x40030, 執行狀態與預期不一致, %ErrorMessage%
	ExitApp
}
else
{
	; 獲取 AutoHotkey.exe 的路徑
	SplitPath, A_AhkPath, , Dir
	if (RunAsAdmin=0)
	{
		; 強制普通許可權執行
		switch, A_IsCompiled
		{
			; %A_ScriptFullPath% 必須加引號,否則含空格的路徑會被截斷。%ScriptParameters% 必須不加引號,因為構造時已經加了。
			; 工作目錄不用單獨指定,預設使用 A_WorkingDir 。
			case, "1": Run, RunAs.exe /trustlevel:0x20000 "\"%A_ScriptFullPath%\" /restart %ScriptParameters%",, Hide
default: Run, RunAs.exe /trustlevel:0x20000 "\"%Dir%\%ANSI_U32_U64%\" /restart \"%A_ScriptFullPath%\" %ScriptParameters%",, Hide
	}
}
else
{
	; 強制管理員許可權執行
	switch, A_IsCompiled
	{
		; %A_ScriptFullPath% 必須加引號,否則含空格的路徑會被截斷。%ScriptParameters% 必須不加引號,因為構造時已經加了。
		; 工作目錄不用單獨指定,預設使用 A_WorkingDir 。
		case, "1": Run, *RunAs "%A_ScriptFullPath%" /restart %ScriptParameters%
default: Run, *RunAs "%Dir%\%ANSI_U32_U64%" /restart "%A_ScriptFullPath%" %ScriptParameters%
	}
}
ExitApp
}
}
; 此指令碼到此行結束
;-----------------------------------

  

相關文章