我正在尝试制作一个自动热键Win一旦我按下+ ,该脚本将运行以下序列Space:
- Win+ E-- 打开资源管理器窗口
- Win+ Left Arrow-- 对齐到屏幕左侧
- Win+ E-- 打开资源管理器窗口
- Win+ Right Arrow-- 对齐到屏幕右侧
我尝试了以下操作,但没有效果:
# space::
Send #e
Send {LWin Down}{Left}{LWin Up}
Send #e
Send {LWin Down}{Right}{LWin Up}
return
答案1
您的代码在发送哪些键方面没有问题。但它不起作用,因为它发送得太快了。在 #e 之后,需要一些时间才能启动新窗口。
一个简单的解决方案是等待一段固定的时间。但更好的方法是动态等待新窗口。例如,可以通过 COM 对象“Shell.Application”(参见 Grey 的回答)或 AHK 命令“WinGet, , List”(见下文)实现。
修复等待时间:
#space::
startExplorerAttached("Left")
startExplorerAttached("Right")
return
startExplorerAttached(sideToAttacheTo)
{
Send, #e
Sleep, 500
Send, #{%sideToAttacheTo%}
}
动态的:
#space::
startExplorerAttached("Left")
startExplorerAttached("Right")
return
startExplorerAttached(sideToAttacheTo)
{
hWnd := startExplorer()
if ( hWnd == -1 )
{
Msgbox, Could not start/find new explorer window!
return
}
WinActivate, ahk_id %hWnd%
Send, #{%sideToAttacheTo%}
}
startExplorer()
{
; Starts a new Windows Explorer with the Computer-View.
; Returns the handle from the new window or -1 if no new window is found
static AFTER_START_DELAY := 150
static AFTER_FAILED_SEARCH_DELAY := 20
static MAX_SEARCH_ATTEMPTS := 100
explorerWinTitle = ahk_class CabinetWClass
WinGet, explorerWinHandlesBeforeStart, List, %explorerWinTitle%
argToStartWithComputerView = /e`,
Run, explorer.exe %argToStartWithComputerView%
Sleep, %AFTER_START_DELAY%
Loop, %MAX_SEARCH_ATTEMPTS%
{
WinGet, explorerWinHandlesAfterStart, List, %explorerWinTitle%
Loop, %explorerWinHandlesAfterStart%
{
curAfterStartWinHandle := explorerWinHandlesAfterStart%A_Index%
isOldWin := false
Loop, %explorerWinHandlesBeforeStart%
{
curBeforeStartWinHandle := explorerWinHandlesBeforeStart%A_Index%
if ( curBeforeStartWinHandle == curAfterStartWinHandle )
{
isOldWin := true
break
}
}
if ( not isOldWin )
{
return curAfterStartWinHandle
}
}
Sleep, %AFTER_FAILED_SEARCH_DELAY%
}
return -1
}
答案2
oShellWindows:=(oShell:=ComObjCreate("Shell.Application")).windows
#Space:: ; win+space
KeyWait, Space
oShell.MinimizeAll
Loop, 2 ; any reasonable amount
{
curCnt:=oShellWindows.count
Send, {LWinDown}{vk45}{LWinUp} ; vk45 - e
While, curCnt=oShellWindows.count
Sleep, 500
}
Exit, oShell.TileVertically, curCnt:=""