Sprint with Double Tap "W"

Ask gaming related questions (AHK v1.1 and older)
Treb
Posts: 4
Joined: 07 May 2018, 18:40

Sprint with Double Tap "W"

07 May 2018, 19:42

I've been looking for a macro to sprint by double tapping "W" for a while. I have looked for one for quite some time and have yet to find what I need. I have found macros that will sprint LShift + W but I cannot get one to double tap "W". I can edit simple macros with trial and error, but I haven't had any luck finidng something that will work. This is the best I found https://forums.warframe.com/topic/23839 ... orkaround/

I've searched on these forums, on reddit ahk forum and google and bing and no luck (plenty of requests for this feature, but no real solution). There were macros that helped, but they either toggled sprint or just held W + Shift permenantly running forward forever. I need "w" forward "w+w" sprint. I need to stop moving forward when I release "w" key.

I found this one on the AHK subreddit which is basically the same thing I would like: https://www.reddit.com/r/AutoHotkey/com ... ey_toggle/

"What I'm trying to do is make it so that when I double tap and HOLD a key, then it functions as holding down another key. For example, when I tap 'w' once, it should act as a normal 'w' keypress, but when I double tap it and hold down on the second tap, it makes it so that I'm holding both the 'w' key and another key until I release 'w'. I tried this but it seems to activate when I hold down the 'w' key as well, not just when I double tap it"

lasttime = 0

~W::

If (a_tickCount-lasttime < 300)

{

Send, {LShift down}

KeyWait, W, U

Send, {LShift up}

Return

}

lasttime:=a_tickCount

Return


This was a response someone gave which I tried to edit(replaced "x" to "u") but it did not work in game. I changed key setting in game from Shift to "u" for sprint. The game character did not move forward at all. This macro works on paper, but I can't get it to function in the game. Can't even take 1 step forward, but it does move a millimeter forward.

#SingleInstance, Force
return

; Sends wx if double tapped, otherwise it just sends w
$w::DoubleTap("x")

; Sends sz if double tapped, otherwise it just sends s
$s::DoubleTap("z")

DoubleTap(extra){
thisKey := SubStr(A_ThisHotkey, 0)

if (A_ThisHotkey = A_PriorHotkey) && (A_TimeSincePriorHotkey < 300)
while GetKeyState(thisKey, "P"){
SendInput, % thisKey extra
Sleep, 30
}
Else
while GetKeyState(thisKey, "P"){
SendInput, % thisKey
Sleep, 30
}
}

I'm sorry for the long post, but I hope this will help others like me that have looked for a macro which will move forward pressing W and Sprint when I double tap W. Game which I know that have this feature (double tap W Sprint) are Star Wars Battlefront 2 and Battlefield 3-4 and I believe Minecraft. Again, I've only been able to find other people making this request for numerous games and I hope that they'll be able to find what they need in this post. Thank you for your time and I hope we can save a few pinky fingers!

tl;dr Need macro Shift + W = Sprint, but I want one that will Sprint with double tap "w+w" and stop when I release "w" key. Thanks
User avatar
evilC
Posts: 4822
Joined: 27 Feb 2014, 12:30

Re: Sprint with Double Tap "W"

08 May 2018, 06:11

FYI, what you want is not very trivial to code, especially if you want to do it for more than one key.
I wrote a library that uses robust coding techniques to allow you to determine when multi-tap or press events happen, you can find it here: TapHoldManager.
However, it was not really designed for shift+key combos, but I had a play with some sample code it seemed to work (You need the library from the TapHoldManager thread to use this code):

Code: Select all

; If the library files are in a subfolder called Lib next to the script, use this
#include Lib\TapHoldManager.ahk
; If you placed all the library files in your My Documents\AutoHotkey\lib folder, use this
;#include <TapHoldManager>

#SingleInstance force

thm := new TapHoldManager()	; TapTime / Prefix can now be set here
thm.Add("*$w", Func("MyFunc1"))			; TapFunc / HoldFunc now always one function

MyFunc1(isHold, taps, state){
	shiftState := GetKeyState("Shift", "P")
	if (isHold){
		if (taps == 1){
			; "Normal" press of W - just pass through key events
			if (state){
				Send {Blind}{w Down}
			} else {
				Send {Blind}{w up}
			}
		} else {
			if (state && shiftState){
				;~ ToolTip SPRINT START
				Send +{w Down}
			} else {
				;~ ToolTip SPRINT END
				Send +{w Up}
			}
		}
	}
}
Treb
Posts: 4
Joined: 07 May 2018, 18:40

Re: Sprint with Double Tap "W"

08 May 2018, 22:13

Unfortunately, I was unable to get this to work. I had a bit of trouble getting the macro to start, but then I followed the directions in the read me file in your TapHoldManager.

I was able to move forward, but double tapping "w" did not engage the Shift key to start the sprint. This code is far more complex than I ever expected.
As you mentioned, it's not designed for combining the Shift+key combos.

I have the option to rebind my sprint (Shift) key to lets say "x" key. Maybe that will be a better workaround the Shift+key comobo dilema.
For now, maybe "x key" (sprint) & double tap "w" = sprint is a more viable option.

I believe that the (Shift+doubletap W) macro would be very helpful to numerous request I've ran into during my search for this particular macro.
I just didn't expect it to be so complex, but I am certainly appreciative of such the fast response and look forward to future input.
:beer:
Rangerbot
Posts: 31
Joined: 02 Mar 2018, 10:33

Re: Sprint with Double Tap "W"

13 May 2018, 06:30

Code: Select all

freq:=250

~w::
   wcount++
   SetTimer sub, %freq% ; make this number smaller for quicker successive presses
return

sub:
	if (wcount==2) {
		msgbox w double pressed in %freq% ms		
	}
	wcount := 0
return

pause::reload
I have no game to test it in currently... however:

Code: Select all

freq:=250

~$w::
   wcount++
   SetTimer sub, %freq% ; make this number smaller for quicker successive presses
return

sub:
	if (wcount==2) {
		Send {shift down}		
	}
	wcount := 0
return

{Shift} + w up::send {shift up}

pause::reload
Rangerbot
Posts: 31
Joined: 02 Mar 2018, 10:33

Re: Sprint with Double Tap "W"

14 May 2018, 02:48

Let us know if that works for you, you can also try this from the docs but it is a bit convoluted for beginners

Code: Select all

; Example #4: Detects when a key has been double-pressed (similar to double-click).
; KeyWait is used to stop the keyboard's auto-repeat feature from creating an unwanted
; double-press when you hold down the RControl key to modify another key.  It does this by
; keeping the hotkey's thread running, which blocks the auto-repeats by relying upon
; #MaxThreadsPerHotkey being at its default setting of 1.
; Note: There is a more elaborate script to distinguish between single, double, and
; triple-presses at the bottom of the SetTimer page.
~RControl::
if (A_PriorHotkey <> "~RControl" or A_TimeSincePriorHotkey > 400)
{
    ; Too much time between presses, so this isn't a double-press.
    KeyWait, RControl
    return
}
MsgBox You double-pressed the right control key.
return
Treb
Posts: 4
Joined: 07 May 2018, 18:40

Re: Sprint with Double Tap "W"

19 May 2018, 22:06

Rangerbot, I kind of got this working.

Code: Select all

freq:=250

~$w::
   wcount++
   SetTimer sub, %freq% ; make this number smaller for quicker successive presses
return

sub:
	if (wcount==2) {
		Send {shift down}		
	}
	wcount := 0
return

{Shift} + w up::send {shift up}

pause::reload
I tried this one, but I made an edit because it did not recognize "{Shift}" +w up::send {shift up}

I removed "shift" and just replaced it with "u" and the double tap W works perfectly, but the only issue is that it stays locked in as a toggle. It will be on stuck as sprint.
"u & w up::send {u up}"

It would work perfectly, if it was to stop sprinting when I released the W key. Then reactive sprint with the double tap.

I keep digging on the internet for answers. Here is a visual tutorial from the Blender Game Engine https://www.youtube.com/watch?v=V852dvfyaKI

Thanks for your help guys. It is halfway there. It just needs to reset whenever I release "W" key.

Code: Select all

freq:=150

~$w::
   wcount++
   SetTimer sub, %freq% ; make this number smaller for quicker successive presses
return

sub:
	if (wcount==2) {
		Send {u down}		
	}
	wcount := 0
return

u & w up::send {u up}

pause::reload
Double Tap Sprint is working, it just needs to cancel the toggle whenever W is released and have it reset.
Rangerbot
Posts: 31
Joined: 02 Mar 2018, 10:33

Re: Sprint with Double Tap "W"

21 May 2018, 10:37

freq:=150

~$w::
wcount++
SetTimer sub, %freq% ; make this number smaller for quicker successive presses
return

sub:
if (wcount==2) {
Send {u down}
}
wcount := 0
SetTimer,, off ;Stop this timer for "sub"
return

u & w up::send {u up}

pause::reload
Treb
Posts: 4
Joined: 07 May 2018, 18:40

Re: Sprint with Double Tap "W"

22 May 2018, 08:29

:superhappy: I finally got it! Pinkies can rejoice. After weeks of digging through the internet (forums), I have managed to get it working with your help of course.

I can't code, but I can copy and paste and through trial and error, I manged to make the sprint function with only one key (double tap) to turn on and off by simply releasing the key. :beer:

The infinite monkey theorem at work, but it only took a couple of weeks.

I tried to clean up what wasn't necessary to make it is simple as possible. If it can be improved and made easier to edit for others, then please do so.

Thank you evilC and Rangerbot for responding to my request.

Rangerbot, you got the double key working and I knew it was more than halfway to being completed. I honestly expected someone else to finish the job, but I was fortunate to find a solution.

I am happy that someone else won't have to dig around as much as I did in order to get a simple SPRINT macro that worked with a double tap on/off.
There were plenty of different sprint macros, but they were all permanent toggle only. This one works with a double tap key and automatically shuts off when the key is released.

I will post the first one I got working, then the edited version I cleaned up macro so others can see examples of what works.
I have mine set with a toggle. Feel free to add your own. PgUp:: , PgDn:: , Home::
I had changed from Shift + W to U + W, but I got the shift key to work so it made it easier. You can edit this and try a different key(s).

Original working

Code: Select all

freq:=150

~$w::
wcount++
SetTimer sub, %freq% ; make this number smaller for quicker successive presses
return

sub:
if (wcount==2) {
	Send {Shift down}
	KeyWait, w
	Send {Shift up} 
}

wcount := 0
SetTimer,, off ;Stop this timer for "sub"
return

shift & w up::send {shift up}

pause::reload
Clean and simplified version. :dance:

Code: Select all

freq:=150

~$w::
   wcount++
   SetTimer sub, %freq% ; make this number smaller for quicker successive presses
return

sub:
	if (wcount==2) {
		Send {Shift down}
		KeyWait, w
		Send {Shift up} 		
	}
	wcount := 0
return
"A person who has never made a mistake is a person who has never tried anything new." -Albert Einstein
DemolitionDoc

Re: Sprint with Double Tap "W"

29 Jun 2018, 23:56

this works in AHK Version 1.1.28.02

Code: Select all

$*w::
if (a_priorhotkey != a_thishotkey || a_timesincepriorhotkey > 400) {
    send % getkeystate("shift") ? "{shift up}{w down}" : "{w down}"
    keywait w
    send {w up}
} else {
    send {shift down}{w down}
		keywait W, U
		Send {shift up}{w up}
}
return
TANTRUMUS
Posts: 4
Joined: 26 Jan 2020, 07:49

Re: Sprint with Double Tap "W"

26 Jan 2020, 07:51

I know I'm late to party but I would like to thank you for this script and doing the testing. I got pretty bad tendonitis in my left forearm from the sublte movement of holding shift with my pinky to sprint in games. Thank you so much for letting me still have fun and now that can recover...
ascorad
Posts: 1
Joined: 23 Dec 2022, 16:33

Re: Sprint with Double Tap "W"

23 Dec 2022, 16:34

DemolitionDoc wrote:
29 Jun 2018, 23:56
this works in AHK Version 1.1.28.02

Code: Select all

$*w::
if (a_priorhotkey != a_thishotkey || a_timesincepriorhotkey > 400) {
    send % getkeystate("shift") ? "{shift up}{w down}" : "{w down}"
    keywait w
    send {w up}
} else {
    send {shift down}{w down}
		keywait W, U
		Send {shift up}{w up}
}
return
I just wanted to say thanks for this! I know it's an old thread but this saved me a ton of legwork!

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 44 guests