Need expert advice on simple AHK script

Ask gaming related questions (AHK v1.1 and older)
sean7
Posts: 8
Joined: 13 Jun 2021, 06:12

Need expert advice on simple AHK script

13 Jun 2021, 06:47

Hi guys,

Looking to ease my hand pain whilst playing a game that involves a lot of button mashing. So far I have come up with the following script that helps a lot, however there is an issue.

Code: Select all



list=w12349qae,w12349qae,w12349qae
stringsplit, list, list,`,
Counter = 0

$w::                                
{
	Counter := (Counter=list0) ? (1) : (Counter+1)
	send % list%counter%
	sleep, 50
}


d::

	Click down
	Send {d down}
	KeyWait, d
	Click up
	Send {d up}

return
The issue is when I am holding the "d" key I cannot continue to press the "w" key and have those commands go off while holding "d". The script seems to pause awaiting me to release the "d" key. I am at a loss of how to make them both work simultaneously. Help is greatly apprectiated. Thanks!
User avatar
mikeyww
Posts: 26654
Joined: 09 Sep 2014, 18:38

Re: Need expert advice on simple AHK script

13 Jun 2021, 09:46

Within each routine, or in a new SetTimer routine, or in a new combined routine, you could check the GetKeyState for a key, to decide what to do. I am not sure whether you can actually send a key while a mouse button is down, so keep in mind that AHK might actually release a button temporarily to send a key. The KeyHistory can show you what is happening with the keys.
sean7
Posts: 8
Joined: 13 Jun 2021, 06:12

Re: Need expert advice on simple AHK script

13 Jun 2021, 14:06

Thanks Mike!

I am not holding the mouse button. I am holding "d" which is pressing and holding the mouse button as long as its held down. I would just like to also send other key commands whilst that is happening. Is that not something AHK can do?
User avatar
mikeyww
Posts: 26654
Joined: 09 Sep 2014, 18:38

Re: Need expert advice on simple AHK script

13 Jun 2021, 14:39

Within one routine, you can use GetKeyState() to check the state of the other key. You can then act accordingly. If you combine the routines into one, it might be best. A general idea of how to do that is below.

Code: Select all

$d::
w::
If GetKeyState("w", "P")
 Send x
If GetKeyState("d", "P")
 Send d
Return
Although you are not pressing the mouse button, AHK is pressing the button for you, because that is what your script indicates. I think that you will run into difficulty when you send keys while the button is down, but you can always try it.

Pressing a hotkey interrupts the routine that is already running, unless you use Critical. After the interrupting routine ends, the interrupted routine will resume.
sean7
Posts: 8
Joined: 13 Jun 2021, 06:12

Re: Need expert advice on simple AHK script

13 Jun 2021, 23:45

So I tried what you suggested Mike and I need it to actually hold down "d" not just press it. So I ended up with this....

Code: Select all

#If, GetKeyState("CapsLock", "T")

w::
 	Send w12349qae

$d::
if GetKeyState("d", "P")
	Send {a down}
	Send {d down}
else
 	Send {a up}
	Send {d up}
Return

#if
It does not work, says there is no matching "if" to my "else". If I do not put the ups in there it just pushes and holds forever.

Basically I am trying to hold down a and d keys, then be able to hit "w" and send some keys with that press whilst holding down the "d'" key that is pressing and holding "d" and "a". Is this not possible with AHK? What is the state for a released key, how do I say getkeystate "d", "released".
sean7
Posts: 8
Joined: 13 Jun 2021, 06:12

Re: Need expert advice on simple AHK script

14 Jun 2021, 00:01

By using ~ and not blocking D I am able to remove it from the AHK entirely as its bound in game. Its very close now however I seem to lose my keystate for "d" and it stops spamming "a" whenever I press the "w" key. Until I release and press "d" again that is.

Code: Select all

#If, GetKeyState("CapsLock", "T")

~d::
w::
If GetKeyState("w", "P")
 Send w12349aqe
If GetKeyState("d", "P")
 Send a
Return

#if
User avatar
mikeyww
Posts: 26654
Joined: 09 Sep 2014, 18:38

Re: Need expert advice on simple AHK script

14 Jun 2021, 06:58

See GetKeyState(). The value is 1 for pressed, and 0 otherwise. The tilde may cause trouble in your particular situation, because that always "sends the key through". You have a complicated script looking at the pressing of more than two keys. You may find inconsistent behavior. Remember, too, that when AHK sends a key, it might release other keys automatically to make it "work"-- perhaps giving you an unintended effect. A useful way to think of this is imagining that AHK can replicate what you can do manually. If you cannot do it manually, then AHK might not be able to handle it. I am skeptical that you could manually hold A and D and W and then also have other keys sent successfully at the same time, while those first keys are also registered as held. You have some basic limitations in what the keyboard and drivers can do.
sean7
Posts: 8
Joined: 13 Jun 2021, 06:12

Re: Need expert advice on simple AHK script

14 Jun 2021, 15:05

Doing it manually is what I have been doing. It gets taxing on the hand after a while thus the need for this script. Would this work if I broke it up into 2 scripts running simultaneously?
User avatar
mikeyww
Posts: 26654
Joined: 09 Sep 2014, 18:38

Re: Need expert advice on simple AHK script

14 Jun 2021, 15:18

You could try the following.

Code: Select all

#If GetKeyState("CapsLock", "T")
d::
$w::
d Up::
w Up::SetTimer, Go, % (on := GetKeyState("w", "P") | GetKeyState("d", "P")) ? 25 : "Off"
#If on
d::
w::Return
#If
Go:
Send % (GetKeyState("w", "P") ? "w12349aqe" : "") (GetKeyState("d", "P") ? "a" : "")
Return
You can always try two scripts, but they can sometimes conflict. I had good luck with the script above. It's not ideal, as the timer gets reset. There are other ways to configure, but you can see if this works.
sean7
Posts: 8
Joined: 13 Jun 2021, 06:12

Re: Need expert advice on simple AHK script

15 Jun 2021, 06:13

Mike you are a genius! That works great. I have one more small ask to make it perfect. How do I add

#IfWinActive, ahk_class POEWindowClass

to this script. I just stuckit ontop of the capslock if and it does not function. Thank you so, so much for this. Really helps out
User avatar
mikeyww
Posts: 26654
Joined: 09 Sep 2014, 18:38

Re: Need expert advice on simple AHK script

15 Jun 2021, 07:01

You could change the first line:

Code: Select all

#If WinActive("ahk_class POEWindowClass") && GetKeyState("CapsLock", "T")
sean7
Posts: 8
Joined: 13 Jun 2021, 06:12

Re: Need expert advice on simple AHK script

15 Jun 2021, 16:35

You are the best, thanks again and good luck out there.
sean7
Posts: 8
Joined: 13 Jun 2021, 06:12

Re: Need expert advice on simple AHK script

15 Jun 2021, 17:03

Actually Mike, I spoke too soon. Adding that new winactive line breaks the whole script and somehow sends "x" a bunch
User avatar
mikeyww
Posts: 26654
Joined: 09 Sep 2014, 18:38

Re: Need expert advice on simple AHK script

15 Jun 2021, 18:30

Explanation for the strange characters (swagfag found): https://www.autohotkey.com/boards/viewtopic.php?f=14&t=75399

The following is a workaround.

Code: Select all

#IfWinActive ahk_class POEWindowClass
$d::
$w::
If on
 Return
If !GetKeyState("CapsLock", "T") {
 Send % StrReplace(A_ThisHotkey, "$")
 Return
} Else on := True
SetTimer, Go, 25
SoundBeep, 1500
Return

#If on
d Up::
w Up::
If on := GetKeyState("w", "P") | GetKeyState("d", "P")
 Return
SetTimer, Go, Off
SoundBeep, 1000
Return
#If

Go:
Send % (GetKeyState("w", "P") ? "w12349aqe" : "") (GetKeyState("d", "P") ? "a" : "")
Sleep, 10
Return

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 41 guests