16

I want a script where pressing F1 makes AutoHotkey hold down the left mouse button. I then want the script to release the mouse once I press the key again.

How can I do that?

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
Pizza Overflow
  • 277
  • 1
  • 3
  • 11
  • 4
    To those willing to close the thread: this is a programming question, AutoHotkey is a programming language. – PhiLho Jan 02 '10 at 22:53
  • @PhiLho: Thanks for pointing that out. I nearly closed this myself. – Bill the Lizard Jan 03 '10 at 02:16
  • 1
    @Pizza Overflow: You might want to consider including a short code snippet in AHK questions to avoid any confusion. If you just include a short bit of code showing what you've tried so far, that should be enough that people won't mistakenly close your questions as "not programming related." – Bill the Lizard Jan 03 '10 at 02:18

3 Answers3

15

I would use Click down and Click up

Click is generally preferred over MouseClick because it automatically compensates if the user has swapped the left and right mouse buttons via the system's control panel.

F1::
    alt := not alt
    if (alt)
    {
        Click down
    }
    else
    {
        Click up
    }
Return
Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
DaMacc
  • 691
  • 3
  • 9
  • I've needed to add `Return` at the end of the hotkey procedure code to make it work for me. `F1:: alt := not alt if(alt) { Click down } else { Click up } Return` – Michał Oniszczuk Aug 21 '10 at 10:15
7

Here is a one-liner in case anyone is interested:

F1::Click % GetKeyState("LButton") ? "Up" : "Down"
Forivin
  • 14,780
  • 27
  • 106
  • 199
  • Can I use this one-liner for **Right Mouse Click**? Changing ``LButton`` -> ``RButton`` still appears to be trigger the Left Mouse Click.... – Alex Johnson Aug 06 '17 at 22:51
  • 1
    Yes. `F1::Click right % GetKeyState("RButton") ? "Up" : "Down"` might do the job. If not do `F1::Click % "right " (GetKeyState("RButton") ? "Up" : "Down")` – Forivin Aug 06 '17 at 23:14
  • Perfect! ``F1::Click % "right " (GetKeyState("RButton") ? "Up" : "Down")`` works as intended! Thanks Forivin! – Alex Johnson Aug 07 '17 at 16:32
0

Mmm, I am a bit rusty in AHK programming, but here is what I tried, seems to work:

F1::
  alt := not alt
  If (alt)
  {
    MouseClick Left, 217, 51, , , D
  }
  Else
  {
    MouseClick Left, 217, 51, , , U
  }
Return
PhiLho
  • 40,535
  • 6
  • 96
  • 134
  • Using MouseClick is overkill and makes it look more complicated. Click Up/ Down should suffice. – syaz Feb 23 '10 at 09:12