Looping a key

Ask gaming related questions
sephorax
Posts: 1
Joined: 12 Jan 2024, 05:55

Looping a key

12 Jan 2024, 05:58

Hi my name is Sephorax and I'd like to achieve maximum laziness by making a script that can hold down my "k" key. If toggleable that would be amazing, and if its any help I would need the key to be held down for 7 minutes I appreciate you master coders, I hope you see this as a challenge
User avatar
mikeyww
Posts: 27315
Joined: 09 Sep 2014, 18:38

Re: Looping a key

12 Jan 2024, 08:58

Welcome to this AutoHotkey forum!

Discontinue an action manually or via timer

The following script provides an example of how to discontinue an action via manual action or a timer. In this example, "reset" is the function that discontinues the action, which in this example is holding K. The reset function is called when the hotkey is triggered a second time, or when the timer expires-- whichever occurs first.

The reset timer disables itself. If the timer had already expired, this has no additional effect. If the timer had not already expired-- when the hotkey was triggered the second time-- then disabling the timer will prevent the reset from being repeated when the timer expires.

The hotkey toggles the "on" flag, but the reset always resets the flag. This reset ensures that when the hotkey is triggered again, the desired action will be started instead of stopped.

The flag is defined at the top of the script. This is often a good idea. All variables must be defined before accessing them.

The reset function is called when the script exits. This is a good idea for this script because if the action is active, then a key is being held at this time. Exiting the script while a key is being held might have unintended subsequent effects. The reset sends the key up just before the script exits.

This script uses a hotkey as a toggle. With the default maximum number of threads per hotkey, which is one thread, a hotkey cannot interrupt itself while its own subroutine is still executing. Because this hotkey enables a timer instead of using a loop, sleep, or other prolonged action, it will finish executing quickly. Enabling a timer does not cause any particular delay with the hotkey subroutine itself. Therefore, this hotkey subroutine will complete quickly, so that the hotkey can quickly be triggered again as expected.

Code: Select all

#Requires AutoHotkey v2.0
on := False
OnExit reset

F3:: {
 Static duration := 60000 * 7
 Global on
 If on ^= True {
  Send '{k down}'
  SetTimer reset, -duration
  SoundBeep 1500
 } Else reset
}

reset(exitReason := '', exitCode := '') {
 SetTimer reset, 0
 Send '{k up}'
 SoundBeep 1000
 Global on := False
}

Return to “Gaming”

Who is online

Users browsing this forum: No registered users and 9 guests