Toggle + Function, Refactoring for v2 from v1

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
ioh2
Posts: 2
Joined: 30 Apr 2024, 18:58

Toggle + Function, Refactoring for v2 from v1

30 Apr 2024, 19:06

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.
User avatar
mikeyww
Posts: 27158
Joined: 09 Sep 2014, 18:38

Re: Toggle + Function, Refactoring for v2 from v1

30 Apr 2024, 19:33

Welcome to this AutoHotkey forum!

Code: Select all

#Requires AutoHotkey v2.0
!LButton::Send GetKeyState('LButton') ? '{LButton up}' : '{LButton down}'
User avatar
Seven0528
Posts: 391
Joined: 23 Jan 2023, 04:52
Location: South Korea
Contact:

Re: Toggle + Function, Refactoring for v2 from v1

30 Apr 2024, 19:52

 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
        }
    }
}
  • English is not my native language. Please forgive any awkward expressions.
  • 영어는 제 모국어가 아닙니다. 어색한 표현이 있어도 양해해 주세요.
ioh2
Posts: 2
Joined: 30 Apr 2024, 18:58

Re: Toggle + Function, Refactoring for v2 from v1

01 May 2024, 22:36

Wow, these answers are really good! I understand using variables and ternary operator in AutoHotkey much better with these examples :)
Thank you!!!

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: kiwichick, ninjawisdom, ntepa and 26 guests