IamMashed
IamMashed

Reputation: 1849

autohotkey how to remap mouse left click hold action

I would like to change the input of a mouse when I click the left button and hold it. The usual left click action must remain. Let left click behave as it is, but if you click and hold, let it perform something like holding keyboard button P (or any other keyboard button).

Currently what I got is something like:

~LButton:: 
sleep 100
While (Getkeystate("LButton","P"))
{   

    Send, {M down}

}
Send, {M up}

Return

I am trying to figure out how to remove the click action when I want just to spam letter M.

Upvotes: 1

Views: 2333

Answers (1)

Oleg
Oleg

Reputation: 6314

The following should do what you want:

threshold := 100
LButton::
    CoordMode, Mouse, Screen ; needed to prevent some issues when clicking changes focus
    MouseGetPos, mXclick, mYclick ; save mouse position before sleep

    sleep % threshold

    ; mouse up, do normal click
    if (!GetKeyState("LButton", "P")) {
        MouseGetPos, mXcurr, mYcurr ; save mouse position before click
        Click %mXclick%, %mYclick%
        MouseMove, %mXcurr%, %mYCurr% ; restore mouse position
        return
    }

    while (GetKeyState("LButton", "P")) {
        Send {M down}
        Sleep 30
    }
    Send {M up}
return

You can play with the threshold, try different values and see how it works. Also all the MouseGetPos and MouseMove might not be needed, depends on what you want to happen.

Currently doubleClick will also be a little problematic, you need to doubleClick slower than the threshold but fast enough to register a doubleClick. This can probably be solved, so tell me if it's something you need.

Upvotes: 1

Related Questions