使用AutoHotKey切换到ACTIVE监视器中最近打开的应用程序?

时间:2016-03-18 18:36:16

标签: autohotkey multiple-monitors alt-tab

我一直在使用简短的AutoHotKey脚本使用鼠标中键在Windows 10上模拟Alt Tab,这样我就可以快速打开我最近打开的应用,或者在两个应用之间切换,只需点击一下按钮。这就是我正在使用的

Mbutton::SendEvent {Alt Down}{Tab}{Alt Up}

但我最近一直在使用两台显示器,而我真正想做的是切换到最近打开的应用程序ON THE ACTIVE MONITOR(或鼠标当前所在的显示器)。单独的Alt Tab显然不起作用,因为通常最近打开的应用程序位于另一台显示器上,我只想将最近使用的应用程序的窗口向前移动,如果它在显示器中我目前在工作中。有谁知道这是否可能以及我该怎么做呢?

1 个答案:

答案 0 :(得分:0)

我在这里粘贴我的剧本。它非常难看,但它适用于我所描述的双显示器设置:切换到上一个(对于您单击中间按钮的显示器)活动窗口。

#WinActivateForce
#SingleInstance
#Persistent

CoordMode, Mouse, Screen
CoordMode, ToolTip, Screen

DetectHiddenWindows, On
SetTitleMatchMode , 2
SetBatchLines , -1
SetWorkingDir %A_ScriptDir%

Thread, interrupt, 0
SetTimer, UpdateCurrentWindow, 100

isDebug := true

SysGet, monitors_total, MonitorCount
SysGet, monitor_primary, MonitorPrimary

Hotkey, MButton, SwitchToLastWindow, On

return


SwitchToLastWindow:

WinGet, active_id, ID, A

Loop , %monitors_total%
{
    if (A_Index != current_monitor)
        continue

    switch_to_window := (active_id = winlast%A_Index%)
                      ? winprelast%A_Index%
                      : winlast%A_Index%
}

WinActivate , ahk_id %switch_to_window%

return



UpdateCurrentWindow:

WinGet, active_id, ID, A
monitor_index := GetMonitorIndexFromWindow(active_id)

current_monitor := GetMonitorIndexFromMouse()

if (monitor_index > monitors_total)
    monitors_total := monitor_index

if (winlast%monitor_index% != active_id)
{
    winprelast%monitor_index% := winlast%monitor_index%
    winlast%monitor_index% := active_id
}

if isDebug
{
    DebugToolTip := ""
    Loop , %monitors_total%
    {
        DebugToolTip .= "Monitor # " . A_Index
                     . ":`n`nLast window: "  . winlast%A_Index%
                     . "`nPre-last window: " . winprelast%A_Index% . "`n`n"
    }

    MouseGetPos, xpos, ypos
    DebugToolTip .= "x: " . xpos . "`ny: " . ypos . "`ncurrent_monitor: " . current_monitor
                 . "`n`nA_ScreenHeight: " . A_ScreenHeight . "`nA_ScreenWidth: " . A_ScreenWidth
                 . "`n`nmonitors_total: " . monitors_total . "`nmonitor_primary: " . monitor_primary

    ToolTip , %DebugToolTip%, 10, 10
}

return


; only done for one case: total 2 monitors, monitor to the left is primary
; need to redo for the generic case

GetMonitorIndexFromMouse()
{
    SysGet, monitors_total, MonitorCount
    SysGet, monitor_primary, MonitorPrimary
    MouseGetPos, xpos, ypos
    current_monitor := (xpos > A_ScreenWidth) ? 2 : 1

    return current_monitor
}


; this function is from autohotkey.com/board/topic/69464-how-to-determine-a-window-is-in-which-monitor/?p=440355

GetMonitorIndexFromWindow(windowHandle)
{
    ; Starts with 1.
    monitorIndex := 1

    VarSetCapacity(monitorInfo, 40)
    NumPut(40, monitorInfo)

    if (monitorHandle := DllCall("MonitorFromWindow", "uint", windowHandle, "uint", 0x2)) 
        && DllCall("GetMonitorInfo", "uint", monitorHandle, "uint", &monitorInfo) 
    {
        monitorLeft   := NumGet(monitorInfo,  4, "Int")
        monitorTop    := NumGet(monitorInfo,  8, "Int")
        monitorRight  := NumGet(monitorInfo, 12, "Int")
        monitorBottom := NumGet(monitorInfo, 16, "Int")
        workLeft      := NumGet(monitorInfo, 20, "Int")
        workTop       := NumGet(monitorInfo, 24, "Int")
        workRight     := NumGet(monitorInfo, 28, "Int")
        workBottom    := NumGet(monitorInfo, 32, "Int")
        isPrimary     := NumGet(monitorInfo, 36, "Int") & 1

        SysGet, monitorCount, MonitorCount

        Loop, %monitorCount%
        {
            SysGet, tempMon, Monitor, %A_Index%

            ; Compare location to determine the monitor index.
            if ((monitorLeft = tempMonLeft) and (monitorTop = tempMonTop)
                and (monitorRight = tempMonRight) and (monitorBottom = tempMonBottom))
            {
                monitorIndex := A_Index
                break
            }
        }
    }

    return monitorIndex
}

其中一项功能(GetMonitorIndexFromWindow(windowHandle))不是我的,from here

请注意,我没有时间处理我的GetMonitorIndexFromMouse()函数的通用案例,因此它对我的特定情况(2个监视器,左侧为主监视器)有好处。

我在这里有isDebug := true,因此您可以看到带变量的工具提示 - 当然,如果脚本适合您,请随时将其更改为isDebug := false