AutoHotKey script switching between virtual desktop using ^!LButton and ^!RButton highlights one app from the start menu

I've created an AutoHotKey script which allows me to switch between virtual desktops more comfortably but there is a weird issue that makes a random (but most often the most recent) app ask for attention.

^!LButton:: Send #^{Left}

^!RButton:: Send #^{Right}

An app highlighting itself:

enter image description here

Changing ^ (Ctrl) to + (Shift) doesn't remove the issue. Changing the whole input sequence to F4 (for one case) and F8 for another removed the issue, but it's not ergonomic and they aren't the keys I'd like to use.


Try this

^!LButton:: 
    Send #^{Left}
    Click up
    FocusForemostWindow()
return


^!RButton:: 
    Send #^{Right}
    FocusForemostWindow()
return


#If GetKeyState("Ctrl") and GetKeyState("Alt")

    LButton::
    RButton::
    return

#If

; Give focus to the foremost window on the current desktop.
FocusForemostWindow() {
    list := ""
    WinTitle := ""
    ; ToolTip
    ; get a list of all windows on the current desktop
    WinGet, id, list,,, Program Manager
    Loop, %id%
    {
        this_ID := id%A_Index%
        WinGetTitle, WinTitle, ahk_id %this_ID%
        If (WinTitle="")
            continue
        WinGet, exStyle, exStyle, ahk_id %this_ID%
        If !(exStyle & 0x100)
            continue
        If !IsWindowOnCurrentVirtualDesktop(this_ID)
            continue
        IfWinNotActive, ahk_id %this_ID%
        {
            WinActivate, ahk_id %this_ID%
            WinWaitActive, ahk_id %this_ID% 
        }
        ; ToolTip, %WinTitle%   
            break
    }
    Sleep, 30
}

; https://autohotkey.com/boards/viewtopic.php?p=64295#p64295
; Indicates whether the provided window is on the currently active virtual desktop:

IsWindowOnCurrentVirtualDesktop(hWnd) {
    onCurrentDesktop := ""
    CLSID := "{aa509086-5ca9-4c25-8f95-589d3c07b48a}" 
    IID := "{a5cd92ff-29be-454c-8d04-d82879fb3f1b}" 
    IVirtualDesktopManager := ComObjCreate(CLSID, IID)  
    Error := DllCall(NumGet(NumGet(IVirtualDesktopManager+0), 3*A_PtrSize), "Ptr", IVirtualDesktopManager, "Ptr", hWnd, "IntP", onCurrentDesktop)
    ObjRelease(IVirtualDesktopManager)  
    if !(Error=0)
        return false, ErrorLevel := true
    return onCurrentDesktop, ErrorLevel := false
}