[SOLVED] Press and hold, restart if hotkey pressed again

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Alco
Posts: 15
Joined: 15 Dec 2015, 04:55

[SOLVED] Press and hold, restart if hotkey pressed again

Post by Alco » 02 Dec 2023, 07:10

Hey guys
I have a script that presses and holds "e" for 10 seconds when i press num5, which works fine:

Code: Select all

#IfWinActive, ahk_class UnrealWindow
Numpad5::
Send {e down}
Sleep 10250 ; 10000 milliseconds = 10 seconds
Send {e up}
#IfWinActive
return
However, if i press num5 again, it will not start the script over, which is what i want it to do, ie:

press num5
script presses and holds e for 10 seconds.
5 seconds into the runtime i press num5 again
scripts needs to "Let go" of e, then instantly press it again and hold for 10 seconds all over again.

Tried looking around but not having much luck, can anyone help?

-Alco
Last edited by Alco on 03 Dec 2023, 03:46, edited 2 times in total.

User avatar
mikeyww
Posts: 27686
Joined: 09 Sep 2014, 18:38

Re: Press and hold, restart if hotkey pressed again

Post by mikeyww » 02 Dec 2023, 08:17

Hello,

By default, the maximum number of threads per hotkey is 1, but you can increase that number.
If a hotkey has a max of 1 and it is pressed again while its subroutine is already running, the press will be ignored.

Alco
Posts: 15
Joined: 15 Dec 2015, 04:55

Re: Press and hold, restart if hotkey pressed again

Post by Alco » 02 Dec 2023, 09:51

Code: Select all

#IfWinActive, ahk_class UnrealWindow
#MaxThreadsPerHotkey 3
Numpad5::
Send {e down}
Sleep 10250 ; 10000 milliseconds = 10 seconds
Send {e up}
#IfWinActive
return
So like this?
It doesn't work sadly

User avatar
mikeyww
Posts: 27686
Joined: 09 Sep 2014, 18:38

Re: Press and hold, restart if hotkey pressed again

Post by mikeyww » 02 Dec 2023, 13:27

Code: Select all

#Requires AutoHotkey v1.1.33
winTitle := "ahk_class UnrealWindow"

#If WinActive(winTitle)
Numpad5 Up::
Send {e up}{e down}
SetTimer Eee, -10250
Return
#If

Eee:
Send {e up}
SoundBeep 1000
Return

Alco
Posts: 15
Joined: 15 Dec 2015, 04:55

Re: Press and hold, restart if hotkey pressed again

Post by Alco » 02 Dec 2023, 15:33

image.png
image.png (8.47 KiB) Viewed 340 times
Am I doing something wrong here? I don't fully understand what is happening in this code, I'm still sort of a novice at ahk

User avatar
mikeyww
Posts: 27686
Joined: 09 Sep 2014, 18:38

Re: Press and hold, restart if hotkey pressed again

Post by mikeyww » 02 Dec 2023, 15:45

1. Install the latest version of AHK v1. Use the setup installer with default options.
2. Close all running AHK scripts.
3. Copy and paste, exactly, the entire script as posted. On the forum page, click "Expand view" to see the entire script. Put the script's entire text into a new plain text file such as "test.ahk", with no changes and no other code.
4. Run the script.
5. Activate the target window as indicated in the script.
6. Press and release Numpad5.

Alco
Posts: 15
Joined: 15 Dec 2015, 04:55

Re: Press and hold, restart if hotkey pressed again

Post by Alco » 03 Dec 2023, 03:45

Okay yes my bad, didn't realize i had to click "expand" to see the full code.
It works now, thank you soo much you absolute legend! :D

Post Reply

Return to “Ask for Help (v1)”