Bakr
Bakr

Reputation: 363

AutoHotKey issue with AltTab shortcut

i was trying to create a simple script with AHK to switch between windows (similar to the "recent" button on a samsung mobile) with this code:

XButton2::
Send, {Alt down}{Tab}
Return

the problem is that i don't know how to make the script to release the Alt key when i strike Enter or click the Left mouse button. Any help please?

Upvotes: 2

Views: 1577

Answers (2)

Bakr
Bakr

Reputation: 363

I re-wrote the script for AutoHotkey v2:

XButton2::
{
    ErrorLevel := !KeyWait("XButton2", "T0.3")
    if (ErrorLevel)
    {
        Send("{Alt down}{Tab}{Alt up}")
        ErrorLevel := !KeyWait("XButton2")
    }
    else
    {
        Send("{Alt down}{tab}")
    }
}

; https://www.autohotkey.com/docs/v2/Hotkeys.htm#AltTabRemarks
#HotIf  WinExist("ahk_class XamlExplorerHostIslandWindow")  ; Indicates that the alt-tab menu is present on the screen.
    ~*LButton::  ; The * prefix allows it to fire whether or not Alt is held down.
    {
        Click()
        Send("{Alt up}")
    }
    *Enter::
    *MButton::
    {
        Send("{Enter}")
        Send("{Alt up}")
    }
    ~*Esc::Send("{Alt up}")  ; When the menu is cancelled, release the Alt key automatically.
    ;*Esc::Send "{Esc}{Alt up}"  ; Without tilde (~), Escape would need to be sent.
#HotIf

Upvotes: 2

Relax
Relax

Reputation: 10543

XButton2:: 
    AltTabMenu := true  ; assign the Boolean value "true" or "1" to this variable
    Send, {Alt down}{Tab}
return

; The #If directive creates context-sensitive hotkeys:

#If (AltTabMenu) ; If this variable has the value "true" 

    ; The * prefix fires the hotkey even if extra modifiers (in this case Alt) are being held down

    *Enter::
    *MButton::
        Send {Enter}
        Send {Blind}{Alt Up} ; release Alt
        MouseCenterInActiveWindow()
        AltTabMenu := false
    return

    ~*LButton::
        Click
        Send {Blind}{Alt Up} ; release Alt
        MouseCenterInActiveWindow()
        AltTabMenu := false
    return      

    ; menu navigation by scrolling the mouse wheel:

    *WheelUp:: Send {Right} 
    *WheelDown:: Send {Left}

#If ; turn off context sensitivity

MouseCenterInActiveWindow(){
    WinGetPos,,Y, Width, Height, A ; get active window size
    Xcenter := Width/2        ; calculate center of active window
    Ycenter := Height/2 
    MouseMove, Xcenter, Ycenter, 0
}

Upvotes: 3

Related Questions