AutoRun / AutoWalk

Post gaming related scripts
User avatar
megnatar
Posts: 83
Joined: 27 Oct 2014, 20:49
GitHub: Megnatar
Location: The Netherlands

AutoRun / AutoWalk

21 Jun 2016, 05:17

How to use:
In most games W is used to "move" forward. Thus when calling this function
it will simulate a W key down. You can call this function from any hotkey in you're
script. Press the key once to move forward, press it again to
stop moving. Clicking the left mouse button or pushing W down also
will stop you're character from moving.

Place this file in you're authotkey library folder. Which is here:
%Userprofile%\documents\AutoHotkey\lib\

Give it the following name:
AutoWalk.ahk

Simply call this function from a hotkey in you're script. For example:

Mbutton::
AutoWalk()
Return

Code: Select all

/*
	How to use.
	In most games W is used to "move" forward. Thus when calling this function
	it will simulate a W key down. You can call this function from any hotkey
	in you're script. Press the key once to move forward, press it again to 
	stop moving. Clicking the left mouse button or press/release W also
	will stop you're character from moving.
	
	Place this file in you're Authotkey library folder. Which is here:
	%Userprofile%\documents\AutoHotkey\lib\
	
	Give it the following name:
	AutoWalk.ahk
	
	Simply call this function from some hotkey in you're script. For example:
	
	~w::
	AutoWalk()
	Return
	
*/
#InstallKeybdHook
#InstallMouseHook

global AnyKey			;Declaring variable AnyKey as global variable. Variables in functions are local to that function.

AutoWalk()
{
	AnyKey:=A_ThisHotkey					;Placing last pressed hotkey in variable AnyKey.
	AnyKey:=StrReplace(AnyKey, "~")			;Remove the tilde character from variable Anykey, for none blocked hotkeys.
	If(AnyKey = "w")						;Check to see if the hotkey is also the w key.
	{
		KeyWait, w, T0.5			;Waiting half a second for w to be physically released.
		If(errorlevel = 1)			;When w is still down after 500ms errorlevel is set to 1. You're probably walking manually.
		{
			KeyWait, w			;Waiting for w key to be released.
			exit				;W is physically released, so we can exit here since movement is stopped.
		}
	}	
	keywait, %AnyKey%							;Waiting for the hotkey to be released.
	If not W_VirtState:=GetKeyState("w")		;When the virtual status of the w key is not down, when W_VirtState is 0. Do whatever comes below.
	{
		SendInput, {w down}						;Virtually pressing w down. Virtual (logical state) is whatever your O.S. thinks the key status is in. Physical is the actual state on you're keyboard.
		keywait, %AnyKey%
		Loop
		{	;When you're hotkey, Lbutton or w key is physically pressed down, variable KeyState will be set to 1. The body of the "if" statement will then execute.
			If(KeyState:=GetKeyState(AnyKey, "P") || KeyState:=GetKeyState("Lbutton", "P") || KeyState:=GetKeyState("w", "P"))
			{
				keywait, %AnyKey%			;Waiting for you're hotkey to be released.
				SendInput, {w up}			;Virtually sending w up. Stop moving.
				break						;Breaking free from the loop.
			}
			sleep, 100			;Sleeping 1/10th of a second each time the loop is iterated. Prevents the function from using to much CPU cycles.
		}						;You can increase the sleep period, but the function will be less responsive.
	}
}
Return
Last edited by megnatar on 26 Jul 2016, 13:46, edited 12 times in total.
User avatar
megnatar
Posts: 83
Joined: 27 Oct 2014, 20:49
GitHub: Megnatar
Location: The Netherlands

Re: AutoRun / AutoWalk

02 Jul 2016, 14:36

Update:
The w key now also can be used as key to toggle autowalk on or off.
Snuffy

Re: AutoRun / AutoWalk

09 Oct 2016, 15:53

Thank you for this. I'm attempting to use it but I keep getting the "Error: Call to nonexistent function." I have it in the folder you specified and also a local directory to the script. Any ideas on what could be going on?
razgriz97
Posts: 3
Joined: 09 Aug 2019, 18:11

Re: AutoRun / AutoWalk

09 Aug 2019, 20:05

Congratulations, your script is the best I've found by far until now.

But I'm having a minor issue with it.

I'm using XButton2 instead of Mbutton on this

Code: Select all

Mbutton::
AutoWalk()
Return

if I start to use the script (press once the XButton2) and soon after try to manual walk using W my character stops for about a second and then starts to walk again.

I've tried to mess with the code to see if I could solve this, but haven't achieved any success yet.

The game I've been testing this is on Battlefield 4, IDK if this replicates on other games.

You got any ideia how to fix this?

Return to “Gaming”

Who is online

Users browsing this forum: PipeDreams and 43 guests