Autohotkey How to hold a mouse button while a key is pressed?

Tag: autohotkey Author: a271641532 Date: 2013-05-26

In short: How do I write an autohotkey script so that when I press, say, F1, it presses the mouse button down and only lets it go after I let go of the key?

I did some research on both autohotkey forums (which redirect here for asking questions) and read through the documentation, but I'm no programer and have a difficult time grasping loops.

I tried doing while or if(getkeystate("F1") loops but I kept getting it wrong until finally I've ended up with this script, which almost works.

F2::Click down right
F2 Up::Click up right
F1::Click down
F1 Up::Click up

When I say almost, I mean it doesn't hold the mouse button down. What I get instead is the mouse clicking insanely fast. It's good enough for most situations like games, where it's basically the equivalent of holding the mouse button, or dragging windows, but when I want to highlight a long paragraph, it doesn't work. After a second or two the whole thing starts blinking or the selection resets, selects in the middle, etc. Basically, it does what would normally happen if you just kept clicking your mouse button really really fast.

This is also the same solution as posted in this question How to Hold Down Mouse while Key is Pressed?

I initially posted under it a follow-up question, but apparently this is not allowed and it got deleted so I have to create another question with the identical name because I want the same thing.

Please help or shed some light on what I'm doing wrong.

This is a duplicate of click me - but is a better question.

Best Answer

F1::
    if( not GetKeyState("LButton" , "P") )
        Click down
return

F1 Up::Click up

and same goes for right click

comments:

I knew I missed something easy, but having no coding experience, merely a basic understanding of how things work I could not wrap my head around the problem properly. Thank you very much for the clean and tidy solution. I would have never thought of simply adding a check to see if the button is already not pressed. Clever.