Search code examples
autohotkeyexit

How to do a left click up on exit?


I was writing a pretty simple script to hold down left click. I am most likely over-complicating this, but I want to be able to exit the script when I want and have left click go up on exit. I tried a loop but I could not quite figure out how I would go about pausing it, since I want it to immediately go back to mouse down on an unpause. Anyway, here is the current code I am working with:

=::

click, down

-::ExitApp
OnExit("ClickUp")

ClickUp(ExitReason)
{   
  if ExitReason in Exit
  {
  click, up
  }
}
return

Solution

  • The main problem is your OnExit("ClickUp") line being unreachable code.
    It'll never get executed, and therefore your script doesn't have function defined to run on exit.
    It's unreachable code, because your script ends code execution when the first hotkey label (=::) is reached.
    This is called the auto-execute section.

    To fix this, you'd just set the OnExit("ClickUp") line to be in your auto-execute section. Maybe make it the very first line in your script.

    And since I called that the main problem, there has to be some other problems as well. I'd call the other problems cursed code.


    =::
    
    click, down
    

    You never end the hotkey label's code execution. By luck there's nothing to be executed below, but this would very easily cause unwanted behavior.
    End the code execution with a Return or use a single line hotkey as you can in this case:
    =::click, down


    if ExitReason in Exit
    You were probably looking to do this:
    if (ExitReason = "Exit")
    Though, I wouldn't recommend it. I don't think you want this check at all, all the exit reasons should be fine(?)


    And then the Return on the last line serves no purpose.
    Here's your finished script:

    OnExit("ClickUp")
    
    =::Click, Down
    -::ExitApp
    
    ClickUp()
    {   
        Click, Up
    }
    
    /*
    Or, if the exit reason checking
    was somehow needed, use this version
    
    ClickUp(ExitReason)
    {   
        if (ExitReason = "Exit")
            Click, Up
    }
    */