Stop Sprinting, Resume Walking Issue

Ask gaming related questions (AHK v1.1 and older)
astute65_aardvark20
Posts: 43
Joined: 23 Dec 2022, 18:59

Stop Sprinting, Resume Walking Issue

12 Feb 2023, 04:15

OK, so I am playing a game where releasing "forward" (W) is the only way to stop "sprinting", once sprinting has been initiated (LShift).

The problem is that I would like to hold LShift to sprint, release LShift to stop sprinting, and, if I am still holding W, I want to go from sprinting back to walking forward.

Code: Select all

#NoEnv
#SingleInstance, Force
SendMode, Input
SetBatchLines, -1
SetWorkingDir, %A_ScriptDir%

Walking :=False
Sprinting := False

w::
Send {w down}
Walking := True
If (Sprinting)
    Send, {LShift}
Return

w Up::
Send {w Up}
Walking := False
Return

LShift::
Send {LShift}
Sprinting := True
Return

LShift Up::
If (Walking) {
    Send {w Up}
    Sleep, 30
    Send, {w Down}
}
Sprinting := False
Return
This works alright, except that if I release LShift and then W soon after, AHK sends {w Down} indefinitely.

I need a way for the script to check if W is pressed and either break the LShift Up function, or send {w Up}, or some other solution that I haven't thought of.
fernsehen123
Posts: 22
Joined: 24 Mar 2018, 13:50

Re: Stop Sprinting, Resume Walking Issue

12 Feb 2023, 07:26

Check if W is pressed, you can do this with

Code: Select all

GetKeyState("W", "P")
astute65_aardvark20
Posts: 43
Joined: 23 Dec 2022, 18:59

Re: Stop Sprinting, Resume Walking Issue

12 Feb 2023, 09:41

Ahh, I should have mentioned I tried GetKeyState instead of variables.

The problem is that the game does not recognize "Send, {w}" to trigger moving forward, so I have to use "{Send, {w Up} Sleep, 30 Send, {w Down}" to reset the movement to forward after releasing LShift.

What is causing AHK to indefinitely send {w Down} is if I release LShift and then W in quick succession, AHK begins the subroutine attached to "w Up::"while the subroutine for "LShift Up::" also is happening.

This is a bit of a brain buster, at least for a noob like me who isn't well-versed at thinking in code.

I tried to use a While/If GetKeyStates loop with a Break, but I don't know how to make that work with a "key up" routine.
astute65_aardvark20
Posts: 43
Joined: 23 Dec 2022, 18:59

Re: Stop Sprinting, Resume Walking Issue

12 Feb 2023, 12:10

So I wasn't 100% correct in my assessment of what was causing the problem.
if I release LShift and then W in quick succession, AHK begins the subroutine attached to "w Up::"while the subroutine for "LShift Up::" also is happening.
If I understand correctly, what is actually happening is that if W is released very soon after LShift is released, the thread for the "w Up::" routine interrupts the thread of the "LShift Up::" routine. The thread with the remainder of the "LShift Up::" routine gets buffered, or put in a queue and won't continue until the current thread of the most recent event ("w UP") is completed.

If the timing is just right, then "Send, {w Down}" gets buffered to the end of the thread queue and becomes the last action of the whole sequence.

While reading the reference on Threading Behavior, I discovered the entry regarding Critical, and applied it to the routine that I do not want to be buffered under any circumstance:

Code: Select all

#NoEnv
#SingleInstance, Force
SendMode, Input
SetBatchLines, -1
SetWorkingDir, %A_ScriptDir%

Walking :=False
Sprinting := False

w::
Send {w down}
Walking := True
If (Sprinting)
    Send, {LShift}
Return

w Up::
Send {w Up}
Walking := False
Return

LShift::
Send {LShift}
Sprinting := True
Return

LShift Up::
Critical
If (Walking) {
    Send {w Up}
    Sleep, 30
    Send, {w Down}
}
Sprinting := False
Return
And it has solved 99.9% of the errors. For some reason it still squeaks through very, very rarely, but that may be because I am spamming the keys over and over during testing, so it could be related to #MaxThreadsPerHotkey or something else that I don't quite understand.

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 30 guests