;; ウィンドウタイトルを部分一致で検索 SetTitleMatchMode, 2 ;; Focus to Emacs WinActivate, Emacs ahk_exe vcxsrv.exe Focus(z){ static past ; saves the last z parameter passed windows := AltTabWindows() if (z = "next") { static next, count if (z != past || next > windows.length()) { loop % windows.length() next := A_Index+1 until (WinActive("A") = windows[A_Index]) past := z WinActivate, % "ahk_id" windows[next] return windows[next] } else if (next < windows.length()) { next++, count++ past := z WinActivate, % "ahk_id" windows[next] return windows[next] } else if (next = windows.length()) { count-- past := z if (count = 0) past := "reset" WinActivate, % "ahk_id" windows[next] return windows[next] } } if (z = "recent") { loop % windows.length() recent := A_Index+1 until (WinActive("A") == windows[A_Index]) past := z return windows[recent] ; WinActivate, % "ahk_id" windows[recent] ; return windows[recent] } if (z ~= "(?i)^bot") { past := z WinActivate, % "ahk_id" windows[windows.length()] return windows[windows.length()] } if (z is integer) { past := z WinActivate, % "ahk_id" windows[z] return windows[z] } } AltTabWindows() { ; modernized, original by ophthalmos https://www.autohotkey.com/boards/viewtopic.php?t=13288 static WS_EX_APPWINDOW := 0x40000 ; has a taskbar button static WS_EX_TOOLWINDOW := 0x80 ; does not appear on the Alt-Tab list static GW_OWNER := 4 ; identifies as the owner window AltTabList := {} windowList := "" DetectHiddenWindows, Off ; makes DllCall("IsWindowVisible") unnecessary WinGet, windowList, List ; gather a list of running programs Loop, %windowList% { ownerID := windowID := windowList%A_Index% + 0 ; format as an unsigned integer Loop ; gets the hWnd of the top-most parent if the window is a child window. ownerID := DllCall("GetWindow", "uint", ownerID, "uint", GW_OWNER) Until !DllCall("GetWindow", "uint", ownerID, "uint", GW_OWNER) ownerID := ownerID ? ownerID : windowID If (DllCall("GetLastActivePopup", "uint", ownerID) = windowID) { WinGet, es, ExStyle, ahk_id %windowID% ; Must appear on the Alt+Tab list, have a taskbar button, and not be a Windows 10 background app. If (!((es & WS_EX_TOOLWINDOW) && !(es & WS_EX_APPWINDOW)) && !IsInvisibleWin10BackgroundAppWindow(windowID)) AltTabList.Push(Format("0x{:x}", windowID)) } } return AltTabList } IsInvisibleWin10BackgroundAppWindow(hWindow) { result := 0 VarSetCapacity(cloakedVal, A_PtrSize) ; DWMWA_CLOAKED := 14 hr := DllCall("DwmApi\DwmGetWindowAttribute", "ptr", hWindow, "uint", 14, "ptr", &cloakedVal, "uint", A_PtrSize) if !hr ; returns S_OK (which is zero) on success. Otherwise, it returns an HRESULT error code result := NumGet(cloakedVal) ; omitting the "&" performs better return result ? true : false } /* DWMWA_CLOAKED: If the window is cloaked, the following values explain why: 1 The window was cloaked by its owner application (DWM_CLOAKED_APP) 2 The window was cloaked by the Shell (DWM_CLOAKED_SHELL) 4 The cloak value was inherited from its owner window (DWM_CLOAKED_INHERITED) */ out := 0 stdout := FileOpen("*", "w", "UTF-8") stdout.WriteLine(Focus("recent")) FileAppend % var, * ; StdOut stdout.Close() ExitApp % out