在 Ahk 中发送 Alt + Tab 的正确方法是什么?

What is the right way to send Alt + Tab in Ahk?

好的。我知道这是一个非常愚蠢的问题。 但是我已经卡了一个小时了。

我对 ahk 的经验很少,但是直到现在我都可以毫无问题地完成每个脚本。我探索了 ahk 教程,但到目前为止没有找到解决方案。

我正在尝试切换到上一个。带有单个小键盘关闭键的应用程序。 我试过:

!{Tab}

,

{Alt down}{Tab}{Alt up}

我试过睡眠延迟、多行、括号内的多行、命令后有和没有逗号等

我很确定这很简单,但我还没有尝试过。

有什么建议吗?

您不应手动发送 alt+tab,因为它是一个特殊的 windows 命令,而应使用 AltTab commands 为您执行此操作。

AltTabMenu 打开选项卡菜单并选择程序,同时 AltTabShiftAltTab 浏览它。

h::AltTabMenu  
n::AltTab
m::ShiftAltTab

嗯,终于找到了原因和一些"solutions"here and here。 似乎 Windows 8 块 Ahk {Alt Down}{Tab} 和 AltTabMenu 以及其他一些键。

现在我用它来滚动 windows 向前:

Send !{ESC} 

显示 AltTabMenu:

Run, "C:\Users\Default\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\Window Switcher.lnk"

然后按照主题之一的建议切换到上一个应用程序:

LCtrl & z:: ; AltTabMenu


state := GetKeyState("Capslock", "T")
if state = 1
SetCapsLockState, Off  ; CapsLock On blocks Task Switching metro window

Send, !{Tab}   ; prevents displaying inactive Task Switching metro window
run, Window Switcher.lnk ; must be in script directory otherwise include path 
WinWait, Task Switching,, 2
KeyWait, Ctrl
Send, {Enter}

if state = 1
SetCapsLockState, On  ; restores CapsLock State
state =

return

#IfWinActive, Task Switching
LCtrl & q::Send, {Right}
LCtrl & a::Send, {Left}

在没有 AltTabMenu 启动的情况下进入上一个应用程序会很棒。

如果您只想切换回之前的应用程序,请使用发送,!{Esc}

$F1:: AltTab()
$F2:: AltTabMenu()

; AltTab-replacement for Windows 8:
AltTab(){
    list := ""
    WinGet, id, list
    Loop, %id%
    {
        this_ID := id%A_Index%
        IfWinActive, ahk_id %this_ID%
            continue    
        WinGetTitle, title, ahk_id %this_ID%
        If (title = "")
            continue
        If (!IsWindow(WinExist("ahk_id" . this_ID))) 
            continue
        WinActivate, ahk_id %this_ID%, ,2
            break
    }
}

; AltTabMenu-replacement for Windows 8:
AltTabMenu(){
    list := ""
    Menu, windows, Add
    Menu, windows, deleteAll
    WinGet, id, list
    Loop, %id%
    {
        this_ID := id%A_Index%
        WinGetTitle, title, ahk_id %this_ID%
        If (title = "")
            continue            
        If (!IsWindow(WinExist("ahk_id" . this_ID))) 
            continue
        Menu, windows, Add, %title%, ActivateTitle      
        WinGet, Path, ProcessPath, ahk_id %this_ID%
        Try 
            Menu, windows, Icon, %title%, %Path%,, 0
        Catch 
            Menu, windows, Icon, %title%, %A_WinDir%\System32\SHELL32.dll, 3, 0 
    }
    CoordMode, Mouse, Screen
    MouseMove, (0.4*A_ScreenWidth), (0.35*A_ScreenHeight)
    CoordMode, Menu, Screen
    Xm := (0.25*A_ScreenWidth)
    Ym := (0.25*A_ScreenHeight)
    Menu, windows, Show, %Xm%, %Ym%
}

ActivateTitle:
    SetTitleMatchMode 3
    WinActivate, %A_ThisMenuItem%
return

;-----------------------------------------------------------------
; Check whether the target window is activation target
;-----------------------------------------------------------------
IsWindow(hWnd){
    WinGet, dwStyle, Style, ahk_id %hWnd%
    if ((dwStyle&0x08000000) || !(dwStyle&0x10000000)) {
        return false
    }
    WinGet, dwExStyle, ExStyle, ahk_id %hWnd%
    if (dwExStyle & 0x00000080) {
        return false
    }
    WinGetClass, szClass, ahk_id %hWnd%
    if (szClass = "TApplication") {
        return false
    }
    return true
}

编辑(用户建议Ooker):

脚本弹出菜单供您选择。

这是它的样子:

Windows8/10 和 ctrl-alt-del 和 alt-tab 等键存在一些问题。这是一种解决方案:

F1::
  {   
        Send {Alt Down}{Tab} ;Bring up switcher immediately            
        KeyWait, F1, T.5  ; Go to next window; wait .5s before looping
        if (Errorlevel)
       {       
        While ( GetKeyState( "F1","P" ) ) {
            Send {Tab}        
            Sleep, 400 ; wait 400ms before going to next window
        }
    }
        Send {Alt Up} ;Close switcher on hotkey release
}
return

为我工作:

F1::
Send, {ALT DOWN}{TAB}{ALT UP}
return

它模拟 F1 键的 Alt + Tab 行为。

如果你想做多个 "tabs",那么下面的函数应该有助于做到这一点。这至少是我的 Windows 8.1 机器上自己的解决方案。

方法是:

  • 1) 获取所有 windows
  • 的列表
  • 2) 循环 1:
    • 找到当前的索引window
    • 设置索引切换到("current" + "offset")
  • 3) 循环 2:
    • 循环直到找到要切换到的索引,然后切换 window

下面的 AutoHotKey 代码示例:

; Test switch of 1 window
F1::AltTabFunction(offset:=1)

; Test switch of 2 windows
F2::AltTabFunction(offset:=2)

AltTabFunction(offset:=1)
{
    ; ****************************
    ; Function for switching windows by ALT-TAB (offset = number of windows to "tab")
    ; ****************************
    ; Get list of all windows.
    WinGet, AllWinsHwnd, List
    WinGetTitle, active_title, A ; Get title of active window.

    ; Find index of the current window.
    counter_of_none_hidden_windows := 0 ; Initiate counter for counting only the none-hidden windows.
    Loop, % AllWinsHwnd
    {
        ; Find title for window in this loop.
        WinGetTitle, CurrentWinTitle, % "ahk_id " AllWinsHwnd%A_Index%

        ; From [1]: "Retrieves an 8-digit hexadecimal number representing the extended style of a window.".
        ; [1] : https://autohotkey.com/docs/commands/WinGet.htm
        WinGet, exStyle, exStyle, % "ahk_id" AllWinsHwnd%A_Index%

        ; Skip hidden windows by checking exStyle.
        If !(exStyle & 0x100){
            Continue
        }

        ; Window is not hidden. Increase counter.
        counter_of_none_hidden_windows := counter_of_none_hidden_windows+1

        ; Set flag.
        titles_match := CurrentWinTitle = active_title
        If (titles_match) {
            window_index_to_switch_to := counter_of_none_hidden_windows+offset
            break
        }
    }

    ; Find index of the window to switch to and do the actual switch
    counter_of_none_hidden_windows := 0 ; Initiate counter for counting only the none-hidden windows.
    Loop, % AllWinsHwnd
    {
        ; From [1]: "Retrieves an 8-digit hexadecimal number representing the extended style of a window.".
        ; [1] : https://autohotkey.com/docs/commands/WinGet.htm
        WinGet, exStyle, exStyle, % "ahk_id" AllWinsHwnd%A_Index%

        ; Skip hidden windows by checking exStyle.
        If !(exStyle & 0x100){
            Continue
        }

        ; Window is not hidden. Increase counter.
        counter_of_none_hidden_windows := counter_of_none_hidden_windows+1

        ; Set flag.
        found_window_to_switch_to := counter_of_none_hidden_windows = window_index_to_switch_to

        ; Switch window.
        If (found_window_to_switch_to) {
            ; Get title.
            WinGetTitle, CurrentWinTitle, % "ahk_id " AllWinsHwnd%A_Index%
            ; Activate by title.
            WinActivate, %CurrentWinTitle%
            ; Stop loop.
            break
        }
    }
    return ; Nothing to return
}
send {Alt down}{tab}
send {Alt up}

!{Tab} 可以在 windows 之间切换,如果你在它之前和之后添加睡眠。

  • 睡眠 100
  • 发送 !{Tab}
  • 睡眠 100

我认为这个问题是一个简单的请求:如何使用 AHK 在 Win10 中使用 alt 选项卡,因为 win10 改变了一切? -> 我找到了最简单的解决方案,如下所示......代码使得在 Win10 emu 打开时必须按住 alt 键 - 然后使用箭头键额外数量的选项卡(如果你需要三个 alt 选项卡,然后是 "alt tab, then right 2",看到了吗?

macro key name::

{
        Sleep 100
        Send, ^c
        Sleep 1000
        Send, {alt down}{tab}
        Sleep 400
        Send, {right 2}{alt up}
        Sleep 400
        Send, ^v
        Sleep 400
}

因此,只需在您的代码中使用此片段,您就可以跳过 'next' window(s) 打开。 罗斯曼

我的个人目标是将 Alt-Tab 重新映射到 Win-Tab(因为我在 Windows 10 上使用 Mac 键盘)所以我采用了 Stepan 上面写的内容以及一些文档这是,对我来说工作正常:

#Tab::
{   
  Send {LAlt Down}{Tab}          
  KeyWait, LWin  ; Wait to release left Win key
  Send {LAlt Up} ; Close switcher on hotkey release
}
return

请参考这个link:https://www.autohotkey.com/docs/Hotkeys.htm#alttab

要在不激活所选 window 的情况下取消 Alt-Tab 菜单,请按或发送 Esc。在下面的示例中,您将按住左 Ctrl 并按 CapsLock 以显示菜单并在其中前进。然后您将释放左 Ctrl 以激活选定的 window,或按下鼠标滚轮取消。在 运行 此示例之前定义 AltTabWindow window 组,如下所示。

LCtrl & CapsLock::AltTab
#IfWinExist ahk_group AltTabWindow  ; Indicates that the alt-tab menu is present on the screen.
MButton::Send {Blind}{Escape}  ; The * prefix allows it to fire whether or not Alt is held down.
#If