How to make key do different actions based on whether it's held down, pressed once or pressed twice?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Faizan
Posts: 1
Joined: 22 Jul 2018, 02:06

How to make key do different actions based on whether it's held down, pressed once or pressed twice?

22 Jul 2018, 02:11

I want to make an autohotkey script for playing my game in campaign mode.

What I want is,

- If I press "f" twice with 300 ms delay, then "i" is pressed once.

- If I press "f" once, then "LButton" is pressed once.

- If I hold "f" down, then "LButton" is held down until I leave f button.

So far I tried this codes but it does not do exactly what I want.

Code: Select all

 f::
        if 1_presses > 0
        {
            1_presses += 1
            SetTimer Key1, 300
            return
        }
        1_presses = 1
        SetTimer Key1, 300
        return
    
        Key1:
        SetTimer Key1, off
        if 1_presses = 2
          Send {i}
        else
           (not GetKeyState("LButton" , "P") )
            Click down
           1_presses = 0
        return
    f Up::Click up
The mistake in this code is that,

- if I press f once, then LButton is pressed permanently

- if I press f twice, then i is pressed once but also LButton is again pressed permanently

- if I hold f then nothing happens.


P.S. Please try to keep it as simple as possible so that I get the least input lag.
Rohwedder
Posts: 7622
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: How to make key do different actions based on whether it's held down, pressed once or pressed twice?

22 Jul 2018, 02:48

Hallo,
try:

Code: Select all

f::
If !Presses := 0 Presses + 1
    SetTimer, Tf, -300
Return
Tf:
If Presses ;f twice
    Send, i
Else If GetKeyState("f","P") ;f hold
{
    Send, {LButton Down}
	KeyWait, f
	Send, {LButton Up}
}
Else ;f once
    Click
Presses =
Return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 170 guests