Help needed - multiple keystrokes while holding down a key Topic is solved

Ask gaming related questions (AHK v1.1 and older)
kevvy
Posts: 3
Joined: 21 Sep 2018, 12:26

Help needed - multiple keystrokes while holding down a key

21 Sep 2018, 12:45

Hello all, I'm trying to make a script that sends multiple keystrokes(a key being held down followed by a keystroke) while holding down a key.

I want to do the following:
a) I hold down '1'.
b) While '1' is being held down I want '2' to be pressed down for two seconds.
c) Afterwards I want '3' to be pressed once followed by a 500ms sleep.
The above repeats until I release '1'.

So far I have the following code:

Code: Select all

1::
while getkeystate("1","p") 
{
 send {2 down}
 sleep 1950 
 send {2 up}
 sleep 50
 send 3
 sleep 500
 return
}
Two issues I am having with the above are:
1) '2' is only pressed once. I want it to act as if I am pressing '2' on a notepad so that it would look like 22222222222222222222222222222222222222222223.
2) The script is carried out until the end even if I release my '1' after one second. When I lift '1' one second after it has pressed '2', a moment later it still presses '3'. I want it to end regardless of what sequence it is in immediately when I release '1'.

I'm not too good at articulating my words so please excuse me for any lack on my part.

Thanks in advance!
Kev
User avatar
Onimuru
Posts: 107
Joined: 08 Sep 2018, 18:35
Contact:

Re: Help needed - multiple keystrokes while holding down a key  Topic is solved

21 Sep 2018, 13:39

I spent allot of time time trying to break a long loop I made while it's executing (i.e. in the middle of the loop) and eventually just settled for reload. I don't think AHK can do it without adding a break clause after every action but maybe someone else will give you a better script.

Code: Select all

#SingleInstance Force
SendMode Input

Exit

*$1::	;Add "~" (~*$1) to allow 1 to send normally in addition to the following:
	KeyWait, 1, T0.250	;Make sure 1 is being held down, can remove this.
	If ErrorLevel
	{
		Loop
		{
			Send {2 Down}
			Sleep 1950 
			Send {2 Up}
			Sleep 50
			Send 3
			Sleep 500
		}
		Until !GetKeyState("1", "P")	;Break loop (still completes the loop though).
	}
	Return

1 Up::Reload	;Breaks even if in the middle of the loop. This will unfortunately reset any variables you have running so if you have a complex script you will have to track variables in an ini file or something. 
kevvy
Posts: 3
Joined: 21 Sep 2018, 12:26

Re: Help needed - multiple keystrokes while holding down a key

21 Sep 2018, 14:07

Onimuru wrote:I spent allot of time time trying to break a long loop I made while it's executing (i.e. in the middle of the loop) and eventually just settled for reload. I don't think AHK can do it without adding a break clause after every action but maybe someone else will give you a better script.
Thank you very much for your reply and help!

Your codes have helped end the script exactly when I lift up '1'.

As for the issue I had with '2' not being pressed, it turns out that it is indeed pressing '2' down but in some applications it only registers once, while some registers it endlessly(even after releasing '1' it keeps pressing down '2' until I exit the script), but I think at that point the issue is with the application rather than AHK.

Thanks again Onimuru, much appreciated!
kevvy
Posts: 3
Joined: 21 Sep 2018, 12:26

Re: Help needed - multiple keystrokes while holding down a key

21 Sep 2018, 14:16

Just updating my own post, I decided to use another loop for '2' so that instead of pressing down, it'll send multiple keystrokes of '2'.

Code: Select all

#SingleInstance Force
SendMode Input

Exit

*$1::	;Add "~" (~*$1) to allow 1 to send normally in addition to the following:
	KeyWait, 1, T0.250	;Make sure 1 is being held down, can remove this.
	If ErrorLevel
	{
		Loop
		{
			Loop 4
			{
				Send 2
				Sleep 500 
			}
			Send 3
			Sleep 500
		}
		Until !GetKeyState("1", "P")	;Break loop (still completes the loop though).
	}
	Return

1 Up::Reload
So it'll press '2' four times in the span of two seconds then proceeding with 3 onward. To simulate being pressed down, I should probably increase the loop amount and decrease the sleep, and that would help my situation better.

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 45 guests