【问题标题】:What is the right way to send Alt + Tab in Ahk?在 Ahk 中发送 Alt + Tab 的正确方法是什么?
【发布时间】:2016-06-28 13:00:13
【问题描述】:

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

我对 ahk 的经验很少,但是直到现在我都完成了每个脚本,没有任何问题。我浏览了 ahk 教程,但到目前为止没有找到解决方案。

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

!{Tab}

,

{Alt down}{Tab}{Alt up}

我已经尝试过睡眠延迟、多行、括号内的多行、命令后有或没有逗号等。

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

有什么建议吗?

【问题讨论】:

    标签: autohotkey


    【解决方案1】:
    $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
    }
    

    EDIT(由用户Ooker建议):

    脚本会弹出一个菜单供您选择。

    这就是它的样子:

    【讨论】:

    • 这正是我想要的!效果很好!谢谢!
    • Windows 10 应用切换器有那么糟糕吗?我知道你不能用鼠标使用它,但我仍然觉得它很有帮助。我不记得 Windows 8 是否有任何不同。
    • Windows 10 引入了不同的应用切换器,您可以use with the mouse。上述代码的主要优点是可以在不看到切换器的情况下切换到上一个活动窗口。
    • 那么你的意思是当你只想切换到上一个标签时,你不必看到所有应用程序的快速弹出窗口?
    • 是的,这正是我的意思。你可以自己试一试。
    【解决方案2】:

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

    【讨论】:

    • 当您在应用程序之间有固定轮换时,此解决方案更适合:在完成程序 A 后切换到 B,然后切换到 C,然后切换回 A,依此类推。但是如果你只是想要一个 Alt+Tab 的替代品,那么它缺少 GUI 选择会让你因为不知道自己在哪里而感到困惑。
    【解决方案3】:

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

    AltTabMenu 打开标签菜单并选择程序,而AltTab、ShiftAltTab 浏览它。

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

    【讨论】:

    • 嗯,这似乎是一个不错的解决方案,但我无法让它发挥作用。
    • 我深入研究了它,无论我如何编写脚本,应该打开 AltTabMenu 或发送 Alt+Tab 的键以某种方式忽略了“Tab”并且脚本在我之前不起作用在我的键盘上按热键 + 真正的 Tab。
    • @GlebG 打开一个新的空文本文件,将上述代码复制到文件中,另存为test.ahk并运行该文件。然后按下按钮:h n m h
    • 好吧,我没那么笨哈哈。我已经尝试过所有其他示例,除非我按下键盘上真正的 Tab 键,否则它不起作用。我已经在很多方面对其进行了更改,但仍然无法正常工作。它在 Lua 中与 StrokePlus 一起工作,但不适用于 Ahk。我知道“AltTabMenu”应该类似于 Alt+Ctrl+Tab。但由于某种原因,在我的情况下,Tab 被忽略为功能键(如果我单独使用它,它在文本中用作 Tab 键)。
    • @2501 - Windows 8,AHK 1.1.16.05,提升 - 似乎 AltTab、ShiftAltTab 被忽略,AltTabMenu 导致奇怪的状态(我将在此处保存详细信息)
    【解决方案4】:

    Windows 8/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
    

    【讨论】:

    • 这是实际工作的最简单的解决方案。谢谢!
    【解决方案5】:

    为我工作:

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

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

    【讨论】:

    • 我用的是Win10
    【解决方案6】:

    我的个人目标是将 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
    

    【讨论】:

      【解决方案7】:

      好吧,我终于找到了原因和一些“解决方案”here和here。 Windows 8 似乎阻止了 Ahk {Alt Down}{Tab} 和 AltTabMenu 以及其他一些键。

      现在我正在使用它来向前滚动窗口:

      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 飞溅的情况下进入上一个应用程序会很棒。

      【讨论】:

        【解决方案8】:

        如果您想创建多个“选项卡”,那么下面的函数应该有助于做到这一点。这至少是我的 Windows 8.1 机器上自己的解决方案。

        方法是:

        • 1) 获取所有窗口的列表
        • 2) 循环 1:
          • 找到当前窗口的索引
          • 将索引设置为(“当前”+“偏移”)
        • 3) 循环 2:
          • 循环直到点击要切换到的索引,然后切换窗口

        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
        }
        

        【讨论】:

          【解决方案9】:
          send {Alt down}{tab}
          send {Alt up}
          

          【讨论】:

          • 请解释你的答案。
          【解决方案10】:

          请参考此链接:https://www.autohotkey.com/docs/Hotkeys.htm#alttab

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

          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
          

          【讨论】:

            【解决方案11】:

            !{Tab} 如果您在其前后添加睡眠,则可以在窗口之间切换。

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

            【讨论】:

              【解决方案12】:

              我认为这个问题是一个简单的请求:如何使用 AHK 在 Win10 中更改选项卡,因为 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
              }
              

              因此,只需在您的代码中使用这个片段,您就可以跳过打开的“下一个”窗口。 罗斯曼

              【讨论】:

                猜你喜欢
                • 2015-01-20
                • 2015-02-03
                • 2019-10-09
                • 2018-01-12
                • 1970-01-01
                • 2018-09-22
                • 2023-02-21
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多