Button Holding Script Topic is solved

Ask gaming related questions (AHK v1.1 and older)
kalesandrancor

Button Holding Script

03 Jan 2018, 23:03

It will probably be apparent by what I'm about to post but what I'm trying to do is whenever I press WSAD for it to also hold down a specific button (ScrollLock), for as long as any of WSAD remain pressed, when I release the last one of these buttons I want it to also release that button, not working but I'm not entirely sure why, it seems to press the key down, but never release it again? Also pressing the wsad keys are actually blocking the key itself, I want the key to be pressed in conjunction with scrollock, is there a modifier I need to use?

w::
PressScrollLock()
Return

s::
PressScrollLock()
Return

a::
PressScrollLock()
Return

d::
PressScrollLock()
Return

w up::
ReleaseScrollLock()
Return

s up::
ReleaseScrollLock()
Return

a up::
ReleaseScrollLock()
Return

d up::
ReleaseScrollLock()
Return

PressScrollLock() {
GetKeyState, stateScrollLock, ScrollLock
if stateScrollLock = U
Send {ScrollLock down}
}

ReleaseScrollLock() {
GetKeyState, stateScrollLock, ScrollLock
GetKeyState, stateW, w
GetKeyState, stateS, s
GetKeyState, stateA, a
GetKeyState, stateD, d

if stateScrollLock = D and stateW = U and stateS = U and stateA = U and stateD = U
Send {ScrollLock up}
}
TygerByte
Posts: 96
Joined: 12 Aug 2016, 05:22

Re: Button Holding Script

04 Jan 2018, 02:19

I have a sprint macro you can look at it and see if you can get some inspiration. Basically does what you are doing, but I have some extra checks I put in and toggle on and off with other hotkeys not shown.
I think my code is long and could use improvement, but it works.
As for why I have the extra symbols on my hotkeys you can read what they do here https://autohotkey.com/docs/Hotkeys.htm#Symbols on the Table of Hotkey Prefix Symbols (Modifiers)
I toss sleep in my while loops because without it my cpu usage is more than I would like and 10ms is good enough.

Code: Select all

~$*W::
	; If I'm not set to sprint I'll start sprinting after holding w down for half a second. I use this because I toggle it for other activities and is my primary way of turning on the hotkeys.
	if (RunForestRun = 0) {
		KeyWait w, T0.5
		If ErrorLevel {
			RunForestRun = 1
		}
	}
	
	; While W is considered pressed and my toggle is on, I check first to make sure I actually need to press the Sprint key because I don't want multiple presses if it's already pressed. Combined with other checks.
	While GetKeyState("W") && (RunForestRun = 1) {
		if !GetKeyState(SprintKey) && (Aiming = 0) && (Firing = 0) && (Crouching = 0) && (Walking = 0) && (Secondary = 0) {
			Send, {Blind}{%SprintKey% Down}
		}
		Sleep, 10
	}
	Return

~$*W Up::
	; if any other movement is not being held and sprint is being pressed you can release sprint
	if (!(GetKeyState("A") || GetKeyState("S") || GetKeyState("D")) && GetKeyState(SprintKey)) {
		Send {Blind}{%SprintKey% Up}
	}
	Return

~$*A::
	While GetKeyState("A") && (RunForestRun = 1) {
		if !GetKeyState(SprintKey) && (Aiming = 0) && (Firing = 0) && (Crouching = 0) && (Walking = 0) && (Secondary = 0) {
			Send {Blind}{%SprintKey% Down}
		}
		Sleep, 10
	}
	Return
	
~$*A Up::
	if (!(GetKeyState("W") || GetKeyState("S") || GetKeyState("D")) && GetKeyState(SprintKey)) {
		Send {Blind}{%SprintKey% Up}
	}
	Return

~$*S::
	While GetKeyState("S") && (RunForestRun = 1) {
		if !GetKeyState(SprintKey) && (Aiming = 0) && (Firing = 0) && (Crouching = 0)  && (Walking = 0) && (Secondary = 0) {
			Send {Blind}{%SprintKey% Down}
		}
		Sleep, 10
	}
	Return
	
~$*S Up::
	if (!(GetKeyState("A") || GetKeyState("D") || GetKeyState("W")) && GetKeyState(SprintKey)) {
		Send {Blind}{%SprintKey% Up}
	}
	Return

~$*D::
	While GetKeyState("D") && (RunForestRun = 1) {
		if !GetKeyState(SprintKey) && (Aiming = 0) && (Firing = 0) && (Crouching = 0)  && (Walking = 0) && (Secondary = 0) {
			Send {Blind}{%SprintKey% Down}
		}
		Sleep, 10
	}
	Return
	
~$*D Up::
	if (!(GetKeyState("W") || GetKeyState("S") || GetKeyState("A")) && GetKeyState(SprintKey)) {
		Send {Blind}{%SprintKey% Up}
	}
	Return
GetKeyState() should return 1 if it's being pressed by something either ahk or you.
if GetKeyState() means if it is pressed then do { stuff in here }
if !GetKeyState() means if it's not pressed then do { stuff in here }
Last edited by TygerByte on 04 Jan 2018, 03:22, edited 2 times in total.
kalesandrancor

Re: Button Holding Script

04 Jan 2018, 02:38

Thanks I'll fiddle with this and see if it helps me figure out where I was going wrong!
kalesandrancor

Re: Button Holding Script

04 Jan 2018, 04:24

Your code did give me access to a better way to handle the boolean statements, but for the life of me I can't figure out why the following doesn't work now:

Code: Select all

~*w::
PressShift()
Return

~*s::
PressShift()
Return

~*a::
PressShift()
Return

~*d::
PressShift()
Return

~*w up::
ReleaseShift()
Return

~*s up::
ReleaseShift()
Return

~*a up::
ReleaseShift()
Return

~*d up::
ReleaseShift()
Return

PressShift() {
	if !GetKeyState("Shift")
		Send {Shift down}
}

ReleaseShift() {
	if GetKeyState("Shift") && !GetKeyState("w") && !GetKeyState("s") && !GetKeyState("a") && !GetKeyState("d")
		Send {Shift up}
}
I even put another send key to see when it should be pressing Shift up, but there are lots of time when it seems like it shouldn't have sent shift up, but shift is coming up... and no I'm not hitting shift itself on my keyboard... :/
kalesandrancor

Re: Button Holding Script

04 Jan 2018, 04:37

IRONY, the test command I was sending was the actual thing that was causing my issue, because it was sending a lowercase letter, that was forcing shift to come up... it seems to be working as posted.
BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Button Holding Script

04 Jan 2018, 04:38

Code: Select all

~*w::
~*s::
~*a::
~*d::
PressShift()
Return

~*w up::
~*s up::
~*a up::
~*d up::
ReleaseShift()
Return

PressShift() {
	if !GetKeyState("Shift")
		Send {Shift down}
}

ReleaseShift() {
	if GetKeyState("Shift") && !GetKeyState("w") && !GetKeyState("s") && !GetKeyState("a") && !GetKeyState("d")
		Send {Shift up}
}
Cosmetics.
kalesandrancor

Re: Button Holding Script

04 Jan 2018, 05:19

Thanks, that does look quite a bit nicer!

Any way you could give me a rudimentary toggle? I tried some things but not sure what I'm doing wrong with that.
TygerByte
Posts: 96
Joined: 12 Aug 2016, 05:22

Re: Button Holding Script

04 Jan 2018, 19:02

Code: Select all


F1::
Toggle := !Toggle
Return


#if Toggle = 1 ; You can just use Toggle, but I put the = 1 so I know.

~*w::
~*s::
~*a::
~*d::
PressShift()
Return

~*w up::
~*s up::
~*a up::
~*d up::
ReleaseShift()
Return

#if

PressShift() {
	if !GetKeyState("Shift")
		Send {Shift down}
}

ReleaseShift() {
	if GetKeyState("Shift") && !GetKeyState("w") && !GetKeyState("s") && !GetKeyState("a") && !GetKeyState("d")
		Send {Shift up}
}
or just add a Suspend hotkey.

Code: Select all

F1::Suspend
GreatGazoo
Posts: 69
Joined: 28 Dec 2017, 02:53

Re: Button Holding Script  Topic is solved

06 Jan 2018, 01:13

i forget how i used to do it

i think something like

getkeystate w
if D
send shift D
if U
send shift U
its been years since ive used AHK, im trying to teach myself again how to use it for fun, probably no help at all but im thinking

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 110 guests