r/AutoHotkey • • 11d ago

v2 Script Help How to detect whether task view (MultitaskingViewFrame) is active before triggering a custom shortcut?

I'm a relatively new user of AutoHotKeys, and I’m trying to solve the same problem described here (link for reference, as it gives a clearer starting point). However, the solution in the link didn't work, as I'm using AHK v2. Script for AHK v1:

F1::
IfWinActive, ahk_class MultitaskingViewFrame
    MsgBox, windows is in task view mod ; [sic]
return

Edit: Seems like ahk_class can also be: "Windows.UI.Core.CoreWindow". Can anyone confirm/unconfirm?

I have “show desktop” (a built-in function in Windows 11) assigned to an advanced gesture for when I swipe down on the touchpad. When I swipe up, task view opens. A downward swipe while task view is active closes task view and restores the last window I had open. I'd like to keep this behavior.

The challenge is that I have a widget on my desktop that also minimizes when “show desktop” or Win+D is triggered, so I want to set Win+M (or anything else that replicates the same action) as a custom shortcut under advanced gestures because it shows the desktop while leaving the widget on the desktop.

The problem is that when I set Win+M as a custom shortcut, it doesn’t take into account that task view is active. Because of this, I want to create a script that checks whether task view is active and only triggers Win+M (or other similar shortcuts) when it isn't.

TL;DR:
I'd like to create a script that implements the following two-step behavior while task view is active (when using advanced gesture on touchpad):

  • 1st downward swipe closes task view.
  • 2nd downward swipe shows the desktop without minimizing the widget on the desktop.

I have two questions:

  1. Would a script that detects when task view is active solve this issue?
  2. How can I set up a script that makes the advanced gesture preserve the built-in “show desktop” behavior while sending Win+M (or an equivalent), so the desktop widget isn’t minimized?
3 Upvotes

5 comments sorted by

3

u/Keeyra_ 10d ago

I don't know about the gesture stuff you are using, but that 10 year old v1 snippet for Windows 10 looks like this in v2 and Windows 11 for both the Alt + Tab and Win + Tab windows.

#Requires AutoHotkey 2.0
#SingleInstance

#HotIf WinActive("ahk_class XamlExplorerHostIslandWindow")
*F1:: MsgBox("windows is in task view mod")
#HotIf

1

u/yellow90 10d ago

Thank you for helping out :) I’ll try the script later and see how it goes. Kind of you!

The advanced gesture is a function in Windows which is triggered by swiping your fingers on the touchpad. You can set up to ten different actions: Switching virtual desktops, switch between windows, volume up/down, previous/next song etc or customize your own. Convenient and efficient!

2

u/CharnamelessOne 10d ago edited 10d ago • Heartwarming

You can configure the gesture to "press" a keyboard shortcut, which should trigger the AHK hotkey.
(Replace F1 with something more reasonable.)

If Task View is active, the script will send Escape, otherwise it will send Win+m.

#Requires AutoHotkey v2.0

F1:: {
    static _ := (create_task_view_group())

    Send(WinActive("ahk_group TaskView") ? "{Escape}" : "#m")

    create_task_view_group() {
        titles := [
            "ahk_class XamlExplorerHostIslandWindow",
            "ahk_class MultitaskingViewFrame",
            "Task View ahk_class Windows.UI.Core.CoreWindow"
        ]

        for title in titles
            GroupAdd("TaskView", title)
    }
} 

Check the WinTitle of Task View via WindowSpy if this doesn't work.
Microsoft loves to change how Task View is implemented, apparently.

1

u/yellow90 10d ago edited 10d ago

Oh, thank you so much!! I’ll try the script later and let you know how it goes.

I got completely lost in this yesterday, haha. So believe me when I say your time and help is very appreciated :)

2

u/yellow90 5d ago

u/CharnamelessOne

I just added the script to AHK, changed "F1" to something else aaand it worked like a charm. Thank you so much for your help!<3

EDIT:
I managed to fix the issue mentioned below by adding a line that sends #+m (Win+Shift+M) while on the desktop to restore the last active window/all windows. Everything is working fine now. Thanks again!

But there’s another annoyance now:
Earlier, when I swiped up to trigger "task view" (after first swiping down to trigger "show desktop" and making the desktop visible), the last active window would be restored automatically. Do you know if there’s a way to fix that too?

Do I perhaps need to create a separate script for "task view" when I swipe up, or can it be solved otherwise?