Stop script on any key press

Ask gaming related questions (AHK v1.1 and older)
Birphon
Posts: 2
Joined: 05 Jan 2022, 19:19

Stop script on any key press

Post by Birphon » 31 Jan 2023, 02:52

There is a game that I play and a lot of time is spent running/walking around the place. Holding down W for a long time can become rather uncomfortable however the game doesn't have an auto run function so I am making one with AHK - currently just testing using Notepad however I am running into three issues:
1) the whole thing doesn't actually work - I launch the .AHK, press F5 with Notepad open and nothing happens when it should be sending W until F5 is pressed again or Esc is pressed.
2) I get errors saying that using Send, {w down} can't happen when it should? I would like to replace the generic W with this and have a up version before the break
3) I want to make it so that any key can be pressed to turn off the script however I can't seem to find anything related to this - is this not possible with AHK?

Code:

Code: Select all

#NoEnv	; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input	; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%	; Ensures a consistent starting directory.
#MaxThreadsPerHotkey 2

#IfWinActive, ahk_exe Notepad.exe
F5::
    if Running
    {
        Running := false
        Return
}
    
Running := true

Loop {
    Send, w
    if not Running
        Break
}

Running := false
return

Esc::
ExitApp

[Mod action: Moved topic to “Gaming”]

User avatar
DuckingQuack
Posts: 219
Joined: 20 Jan 2023, 18:20

Re: Stop script on any key press

Post by DuckingQuack » 31 Jan 2023, 06:54

(3)
You’ll need to rework quite a bit to be able to incorporate this into the script and have it function properly, but I believe what you are looking for is something like this:

Code: Select all

If A_PriorKey != k
		Break
Best of Luck,
The Duck

User avatar
DuckingQuack
Posts: 219
Joined: 20 Jan 2023, 18:20

Re: Stop script on any key press

Post by DuckingQuack » 31 Jan 2023, 07:08

(1&2)
Perhaps this will help you?

Code: Select all

#Requires AutoHotkey v2.0
F5:: {
 Static on := False
 SetTimer () => Send “w” , 1 * on := !on
}
This is adapted from @mikeyww and should be a completed script that does what you’re asking. (Although in V2)

Testing is suggested as the input interval may need adjustment.
This also does not include the aforementioned priorkey suggestion which would need to be written with the result of the if being either a settimer 0 or another on := !on… maybe even both?
Best of Luck,
The Duck

User avatar
DuckingQuack
Posts: 219
Joined: 20 Jan 2023, 18:20

Re: Stop script on any key press

Post by DuckingQuack » 31 Jan 2023, 07:29

This one isn’t tested and probably has syntax errors.

Code: Select all

#Requires AutoHotkey v2.0
F5:: {
 Static on := False
 SetTimer () => Send “w” , 1 * on := !on
If A_PriorKey != {F5} {
SetTimer 0
on := !on
}
}
Best of Luck,
The Duck

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

Re: Stop script on any key press

Post by mikeyww » 31 Jan 2023, 07:49

The original script provides a good example of the importance of understanding your own code. If you delete line 2 (SendMode), the script works.

"Recommended" is generic! This means that it does not apply well to all situations.

User avatar
DuckingQuack
Posts: 219
Joined: 20 Jan 2023, 18:20

Re: Stop script on any key press

Post by DuckingQuack » 31 Jan 2023, 08:10

@mikeyww Awesome! I skimmed right past that section. :(
I’ll try to pay better attention to that next time.
Good job. :clap:
Best of Luck,
The Duck

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

Re: Stop script on any key press

Post by mikeyww » 31 Jan 2023, 08:49

And DuckingQuack: your solution may be equally good, something like:

Code: Select all

#Requires AutoHotkey v2.0
F5:: {
 Static on := False
 SetTimer () => Send('w'), 20 * on := !on
}
Explained: Function Call Statements
If the return value of the function is not needed and the function name is written at the start of the line (or in other contexts which allow a statement, such as following else or a hotkey), the parentheses can be omitted. In this case, the remainder of the line is taken as the function's parameter list. Parentheses can also be omitted when calling a method in this same context, but only when the target object is either a variable or a directly named property, such as myVar.myMethod or myVar.myProp.myMethod.
Last edited by mikeyww on 31 Jan 2023, 08:55, edited 1 time in total.

User avatar
DuckingQuack
Posts: 219
Joined: 20 Jan 2023, 18:20

Re: Stop script on any key press

Post by DuckingQuack » 31 Jan 2023, 08:54

@mikeyww Thank you for the syntax corrections. How would one correctly incorporate a “kill argument” that uses a_priorkey to kill the sub if any key other than the hotkey is pressed?
Best of Luck,
The Duck

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

Re: Stop script on any key press

Post by mikeyww » 31 Jan 2023, 09:54

Mostly works:

Code: Select all

; This script starts a timer, and ends it with any key
#Requires AutoHotkey v2.0
go := () => SendEvent('w')

F5:: {
 If !(ih ?? False) {
  SetTimer go, 35
  ih := InputHook("VI"), ih.KeyOpt("{All}", "E")
  ih.Start()
  SoundBeep 1500
  ih.Wait()
 }
 SetTimer go, 0
 ih.Stop()
 SoundBeep 1000
}

Post Reply

Return to “Gaming Help (v1)”