interrupt a sequence of key presses with any key input. Topic is solved

Ask gaming related questions
gyrosav
Posts: 17
Joined: 30 Dec 2020, 02:48

interrupt a sequence of key presses with any key input.

Post by gyrosav » 22 Jan 2021, 22:43

I have a code that runs a sequence of key presses. it's pretty long but it's pretty simple. There's one issue I am having. This sequence will not stop until it is finished. Sometimes, I don't want the inputs to finish and I need to press a different key in the middle of the sequence. How can I go about doing this? If I press another key, that key should register and the script should stop. It should also start from the very beginning if I press x, not where it last stopped. The tricky part is that I want ANY key input to stop the sequence from running. Sometimes I need to press another key during the sequence and I need that key to go through instead.

The script I currently have is this:

Code: Select all

x::
Send q
sleep 50
Send w
sleep 55
Send e
sleep 55
Send r
sleep 50
Send t
sleep 55
Send y
sleep 55
Send u
sleep 25
Send i
sleep 25
Send o
sleep 25
Send p
sleep 25
Send a
Return
User avatar
Spawnova
Posts: 456
Joined: 08 Jul 2015, 00:12
Contact:

Re: interrupt a sequence of key presses with any key input.  Topic is solved

Post by Spawnova » 22 Jan 2021, 23:47

Here's one way to do it

Code: Select all

#InstallKeybdHook
keys := [] 
keys.push({key:"q",delay:50})
keys.push({key:"w",delay:55})
keys.push({key:"e",delay:55})
keys.push({key:"r",delay:50})
keys.push({key:"t",delay:55})
keys.push({key:"y",delay:55})
keys.push({key:"u",delay:25})
keys.push({key:"i",delay:25})
keys.push({key:"o",delay:25})
keys.push({key:"p",delay:25})
return

x::
keywait,x ;wait to release x or it will stop early
lastIdle := A_TimeIdleKeyboard
for k,v in keys
{
	send % v.key
	sleep % v.delay
	if (A_TimeIdleKeyboard < lastIdle) {
		break
	}
	lastIdle := A_TimeIdleKeyboard
}
return
loop through an array or keys and if any key has been pressed break the loop
Some of my AHK programs: 3D Voxel Game - Platformer Game - Langton's Ant - Raycast light/Pixel Water - Creating HD map of any game
Or check out my Youtube for these plus many more projects! :D
gyrosav
Posts: 17
Joined: 30 Dec 2020, 02:48

Re: interrupt a sequence of key presses with any key input.

Post by gyrosav » 26 Jan 2021, 22:50

@Spawnova

that's insane. it works. that's incredible. so complicated. I would never have been able to do that. thank you!

Spawn, if I wanted to add mouse clicks in the sequence, how would it look like? Something like this?
keys.push({key:"Click"})
User avatar
Spawnova
Posts: 456
Joined: 08 Jul 2015, 00:12
Contact:

Re: interrupt a sequence of key presses with any key input.

Post by Spawnova » 26 Jan 2021, 23:44

@gyrosav
Typing in "lbutton" and "rbutton" should work I think, however by editing the hotkey function a little bit you can add mouse support including position information like so

Code: Select all

#InstallKeybdHook
keys := [] 
keys.push({key:"q",delay:50})
keys.push({key:"w",delay:55})
keys.push({key:"e",delay:55})
keys.push({key:"r",delay:50})
keys.push({key:"t",delay:55})
keys.push({key:"y",delay:55})
keys.push({key:"u",delay:25})
keys.push({key:"i",delay:25})
keys.push({key:"o",delay:25})
keys.push({key:"p",delay:25})

keys.push({mouse:"left",delay:50})  ;this will click the mouse are current mouse position
keys.push({mouse:"left",x:200,y:200,delay:50})  ;this will click the mouse at 200,200
keys.push({mouse:"right",delay:500}) ;right click
return

x::
keywait,x ;wait to release x or it will stop early
lastIdle := A_TimeIdleKeyboard
for k,v in keys
{
	if (v.mouse) {  ;if this key event is a mouse event
		mouseButton := v.mouse
		if (v.x and v.y) {
			x := v.x, y := v.y
			click,%mouseButton%,%x%,%y% ;if this key event has x and y coordinates then use them
		} else {
			click,% mouseButton  ;if it doesn't just click where the mouse is
		}
	} else {
		send % v.key
	}
	sleep % v.delay
	if (A_TimeIdleKeyboard < lastIdle) {
		break
	}
	lastIdle := A_TimeIdleKeyboard
}
return
Some of my AHK programs: 3D Voxel Game - Platformer Game - Langton's Ant - Raycast light/Pixel Water - Creating HD map of any game
Or check out my Youtube for these plus many more projects! :D
gyrosav
Posts: 17
Joined: 30 Dec 2020, 02:48

Re: interrupt a sequence of key presses with any key input.

Post by gyrosav » 27 Jan 2021, 08:13

@Spawnova I really appreciate your help! I fine tuned your script to try and get it exactly how I want but im running into two issues.

1. for some reason when I press the 3 key during the sequence, the script still continues. but when I press most other keys, it stops. Is there a way to fix this? I need to press 3 in the middle of the sequence and I need the 3 input to go through as normal. It should also stop the sequence.

2. When I add this code to my ahk file that has some other codes, the code you gave me does not work. It only works when I make a new ahk file and run it separately. How can I make it so that it is compatible with other codes?

Here is what I have so far:

Code: Select all

#InstallKeybdHook
keys := [] 
keys.push({key:"1",delay:50})
keys.push({key:"q",delay:55})
keys.push({mouse:"left",delay:50})
keys.push({key:"2",delay:55})
keys.push({key:"w",delay:50})
keys.push({mouse:"left",delay:55})
keys.push({key:"3",delay:55})
keys.push({key:"e",delay:55})
keys.push({mouse:"left",delay:55})
keys.push({key:"4",delay:25})
keys.push({key:"r",delay:25})
keys.push({mouse:"left",delay:25})
keys.push({key:"5",delay:25})
keys.push({key:"t",delay:25})
keys.push({mouse:"left",delay:25})
keys.push({key:"6",delay:25})
keys.push({mouse:"right",delay:25})
keys.push({key:"y",delay:25})
keys.push({mouse:"left"})
return

x::
keywait,x ;wait to release x or it will stop early
lastIdle := A_TimeIdleKeyboard
for k,v in keys
{
	if (v.mouse) {  ;if this key event is a mouse event
		mouseButton := v.mouse
		if (v.x and v.y) {
			x := v.x, y := v.y
			click,%mouseButton%,%x%,%y% ;if this key event has x and y coordinates then use them
		} else {
			click,% mouseButton  ;if it doesn't just click where the mouse is
		}
	} else {
		send % v.key
	}
	sleep % v.delay
	if (A_TimeIdleKeyboard < lastIdle) {
		break
	}
	lastIdle := A_TimeIdleKeyboard
}
return
User avatar
Spawnova
Posts: 456
Joined: 08 Jul 2015, 00:12
Contact:

Re: interrupt a sequence of key presses with any key input.

Post by Spawnova » 27 Jan 2021, 08:42

The reason it may not work in your other script is because you may not have included #InstallKeybdHook - This is necessary for A_TimeIdleKeyboard to function correctly, adding that to the top of your script should fix it

As far as your 3 key not interrupting the script, I'm not sure, any single key press on your keyboard should interrupt it since it's using A_TimeIdleKeyboard, I've tested the script myself and any key including 3 interrupts the script for me, do you have 3 macro'd to something maybe?
Some of my AHK programs: 3D Voxel Game - Platformer Game - Langton's Ant - Raycast light/Pixel Water - Creating HD map of any game
Or check out my Youtube for these plus many more projects! :D
gyrosav
Posts: 17
Joined: 30 Dec 2020, 02:48

Re: interrupt a sequence of key presses with any key input.

Post by gyrosav » 27 Jan 2021, 08:46

@Spawnova

you're right. i do have 3 macro'd to something else. hmm but I need it macro'd to something else too. Anyway I can still make it work?
gyrosav
Posts: 17
Joined: 30 Dec 2020, 02:48

Re: interrupt a sequence of key presses with any key input.

Post by gyrosav » 27 Jan 2021, 08:55

@Spawnova

never mind it works!
gyrosav
Posts: 17
Joined: 30 Dec 2020, 02:48

Re: interrupt a sequence of key presses with any key input.

Post by gyrosav » 27 Jan 2021, 09:06

@Spawnova

When you have some time, can you take a look at one more request? I'd like to add another sequence to my ahk file that is very similar to the one you created for me. but instead of 'x' as the key that begins the sequence, I want two other hotkeys (y and u) to begin the sequence, and the sequence is completely different. But I would like for this sequence to be in the same file as the one you created for me. Here is the sequence that I would like to create and have the ability to be interrupted at anytime like the sequence you made above. It looks a bit odd I know but it actually works for what I need:

Code: Select all

y or u::
Send 1
sleep 55
click, , , right
sleep 50
Send {LShift down}{k}
Send {k}{LShift up}
sleep 55
Send 2
sleep 55
click, , , right
sleep 55
Send {LShift down}{k}
Send {k}{LShift up}
sleep 55
Send 3
sleep 55
click, , , right
sleep 55
Send {LShift down}{k}
Send {k}{LShift up}
sleep 55
Send 4
sleep 55
click, , , right
sleep 55
Send {LShift down}{k}
Send {k}{LShift up}
sleep 55
Send 5
sleep 55
click, , , right
sleep 55
Send {LShift down}{k}
Send {k}{LShift up}
sleep 55
Send 6
sleep 25
click, , , right
sleep 25
Send {LShift down}{k}
Send {k}{LShift up}
Return
User avatar
Spawnova
Posts: 456
Joined: 08 Jul 2015, 00:12
Contact:

Re: interrupt a sequence of key presses with any key input.

Post by Spawnova » 27 Jan 2021, 09:24

Okay I've adjusted the script, it should be easier to add your own macros now

Code: Select all

#InstallKeybdHook
macro1 := [] 
macro1.push({key:"1",delay:50})
macro1.push({key:"q",delay:55})
macro1.push({mouse:"left",delay:50})
macro1.push({key:"2",delay:55})
macro1.push({key:"w",delay:50})
macro1.push({mouse:"left",delay:55})
macro1.push({key:"3",delay:55})
macro1.push({key:"e",delay:55})
macro1.push({mouse:"left",delay:55})
macro1.push({key:"4",delay:25})
macro1.push({key:"r",delay:25})
macro1.push({mouse:"left",delay:25})
macro1.push({key:"5",delay:25})
macro1.push({key:"t",delay:25})
macro1.push({mouse:"left",delay:25})
macro1.push({key:"6",delay:25})
macro1.push({mouse:"right",delay:25})
macro1.push({key:"y",delay:25})
macro1.push({mouse:"left"})

macro2 := []
macro2.push({key:"1",delay:55})
macro2.push({mouse:"right",delay:50})
macro2.push({key:"{lshift down}kk{lshift up}",delay:55})
macro2.push({key:"2",delay:55})
macro2.push({mouse:"right",delay:55})
macro2.push({key:"{lshift down}kk{lshift up}",delay:55})
macro2.push({key:"3",delay:55})
macro2.push({mouse:"right",delay:50})
macro2.push({key:"{lshift down}kk{lshift up}",delay:55})
macro2.push({key:"4",delay:55})
macro2.push({mouse:"right",delay:55})
macro2.push({key:"{lshift down}kk{lshift up}",delay:55})
macro2.push({key:"5",delay:55})
macro2.push({mouse:"right",delay:55})
macro2.push({key:"{lshift down}kk{lshift up}",delay:55})
macro2.push({key:"6",delay:55})
macro2.push({mouse:"right",delay:25})
macro2.push({key:"{lshift down}kk{lshift up}",delay:55})
return

$x::        ;$ is necessary to prevent virtual keypresses to trigger the hotkeys
keywait,x
ProcessMacro(macro1)
return



$y::
$u::
keywait,y
keywait,u
ProcessMacro(macro2)
return

ProcessMacro(macro) {   ;a custom function to process special macro arrays
	static lastIdle
	lastIdle := A_TimeIdleKeyboard
	for k,v in macro
	{
		if (v.mouse) {  ;if this key event is a mouse event
			mouseButton := v.mouse
			if (v.x and v.y) {
				x := v.x, y := v.y
				click,%mouseButton%,%x%,%y% ;if this key event has x and y coordinates then use them
			} else {
				click,% mouseButton  ;if it doesn't just click where the mouse is
			}
		} else {
			send % v.key
		}
		sleep % v.delay
		if (A_TimeIdleKeyboard < lastIdle) {
			break
		}
		lastIdle := A_TimeIdleKeyboard
	}
	return
}
Edit: woops removed some debug soundbeeps
Some of my AHK programs: 3D Voxel Game - Platformer Game - Langton's Ant - Raycast light/Pixel Water - Creating HD map of any game
Or check out my Youtube for these plus many more projects! :D
Post Reply

Return to “Gaming”