Page 1 of 1

Toggle + Function, Refactoring for v2 from v1

Posted: 30 Apr 2024, 19:06
by ioh2
Hi, I'm trying to refactor the following from AHK v1. It's to have alt-click act as a click-and-drag that deactivates on the next click.

Code: Select all

!LButton::
	If (holdin) {
	Send("{LButton Up}")
	holdin := false
}
	else {
		Send("{LButton Down}")
		holdin := true
	}
	Return
}
~LButton:: holdin := false
It's not working, because the holdin variable isn't initialized when the alt-click hotkey is triggered.

I'm thinking this can be refactored for v2, but I am stuck. My thought it that it could be a single line that does Send("{LButton <ternary>}"), but I can't figure out the syntax. Thanks in advance.

Re: Toggle + Function, Refactoring for v2 from v1

Posted: 30 Apr 2024, 19:33
by mikeyww
Welcome to this AutoHotkey forum!

Code: Select all

#Requires AutoHotkey v2.0
!LButton::Send GetKeyState('LButton') ? '{LButton up}' : '{LButton down}'

Re: Toggle + Function, Refactoring for v2 from v1

Posted: 30 Apr 2024, 19:52
by Seven0528
 Questions are always welcome!

Code: Select all

holdin := false
!LButton::  {
    global holdin
    if (holdin)    {
        send("{LButton Up}")
        holdin := false
    }  else  {
        send("{LButton Down}")
        holdin := true
    }
}
~LButton::global holdin := false

Code: Select all

!LButton::  {
    global holdin
    if (!isSet(holdin))
        holdin:=false
    if (holdin)    {
        send("{LButton Up}")
        holdin := false
    }  else  {
        send("{LButton Down}")
        holdin := true
    }
}
~LButton::global holdin := false

Code: Select all

!LButton::  {
    global holdin
    if (holdin??false)    {
        send("{LButton Up}")
        holdin := false
    }  else  {
        send("{LButton Down}")
        holdin := true
    }
}
~LButton::global holdin := false

Code: Select all

!LButton::  {
    global holdin
    if (holdin??false)
        send("{LButton Up}")
    else
        send("{LButton Down}")
    holdin := !holdin
}
~LButton::global holdin := false

Code: Select all

!LButton::  {
    global holdin
    send("{LButton " ((holdin??false)?"Up":"Down") "}")
    holdin := !holdin
}
~LButton::global holdin := false

Code: Select all

!LButton::  {
    global holdin := holdin??false
    send(format("{1}"
        ,"{LButton " (holdin?"Up":"Down") "}"
        ,holdin := !holdin))
}
~LButton::global holdin := false

Code: Select all

!LButton::  {
    global holdin
    send (sendkey := "{LButton " ((holdin??false)?"Up":"Down") "}"
        ,holdin := !holdin
        ,sendkey)
}
~LButton::global holdin := false

Code: Select all

!LButton::send("{LButton " (HoldIn.state?"Up":"Down") "}"), HoldIn.state := !HoldIn.state
~LButton::HoldIn.state := false

class HoldIn    {
    static state := false
}

Code: Select all

!LButton::HoldIn.altLButton()
~LButton::HoldIn.t_LButton()

class HoldIn    {
    static _state := false
    static altLButton()    {
        send("{LButton " (this._state?"Up":"Down") "}"), this._state := !this._state
    }
    static t_LButton()    {
        this._state := false
    }
}

Code: Select all

!LButton::HoldIn.altLButton()
~LButton::HoldIn.t_LButton()

class HoldIn    {
    static _state := false
    static altLButton() => send((sendkey := "{LButton " (this._state?"Up":"Down") "}", this._state := !this._state, sendkey))
    static t_LButton() => this._state := false
}

Code: Select all

!LButton::
~LButton::  {
    HoldIn.onHotkey(A_ThisHotkey)
}

class HoldIn    {
    static _state := false
    static onHotkey(thisKey)    {
        switch (thisKey)
        {
            case "!LButton":        send("{LButton " (this._state?"Up":"Down") "}"), this._state := !this._state
            case "~LButton":        this._state := false
        }
    }
}

Re: Toggle + Function, Refactoring for v2 from v1

Posted: 01 May 2024, 22:36
by ioh2
Wow, these answers are really good! I understand using variables and ternary operator in AutoHotkey much better with these examples :)
Thank you!!!