Creating a "Buffer" to force an interval of time between the sending of each keypress Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Akliz
Posts: 2
Joined: 14 Aug 2022, 16:37

Creating a "Buffer" to force an interval of time between the sending of each keypress

Post by Akliz » 14 Aug 2022, 18:22

Hello!

Short story: a program I'm using can't recognize keypresses if they are too close together (not sure exactly how close, but assume 50ms ish). So I thought I'd make a script that would notice these "simultaneous" keypresses and put some space between them. There's a bonus challenge, but it sounds like more work.

Long story (for google searchability!): I connected a midi keyboard to my pc, and run that input through a program that translates the notes into specific keys on my keyboard, which allows me to play a virtual piano in a game (FFXIV) with my real one. However, it cannot take more than one key at once, which makes playing harder (more tiresome) as you have to stagnate your notes. A would like to make an AutoHotkey script that plays my keys as close together as possible, without me having to worry about it.

Bonus: Ideally, keys would be given priority in a set order, for example, pressing 43251 simultaneously would turn into 12345. This sounds like more code and it's only a minor thing but it'd help for what I'm using it.

I'm a total noob so after like an hour of digging through documentation I could only come up with this. It doesn't work, but I hope it can at least give an idea of what I'm thinking... maybe.

Code: Select all


Array := []

Loop,{
	Array.push(A_ThisHotkey)

	InProcess := Array.RemoveAt(0)

	Send, %InProcess%
	Sleep, 1000
	}
I then tried

Code: Select all

Array := []

Loop,{
	Input, var

	Send, var
	Sleep, 1000
	}
I hope my code didn't make you want to facepalm... But at least I can say I gave it a shot... :(

Thanks

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Creating a "Buffer" to force an interval of time between the sending of each keypress  Topic is solved

Post by swagfag » 14 Aug 2022, 19:23

Code: Select all

global g_BufferedKeys := []

ih := InputHook("L0 I1") ; no limit; ignore SendLevel < 1 (prevents recursion)
ih.KeyOpt("{All}", "N")
ih.OnKeyDown := Func("OnKeyDown")
ih.Start()

OnKeyDown(InputHook, VK, SC) {
	g_BufferedKeys.Push({VK: VK, SC: SC})
}

SetTimer ConsumeKeys, 1
ConsumeKeys() {
	if g_BufferedKeys.Count()
	{
		Key := g_BufferedKeys.RemoveAt(1) ; pop front
		Send % Format("{vk{:02X}sc{:03X}}", Key.VK, Key.SC)
		Sleep 50
	}
}

Akliz
Posts: 2
Joined: 14 Aug 2022, 16:37

Re: Creating a "Buffer" to force an interval of time between the sending of each keypress

Post by Akliz » 15 Aug 2022, 17:37

swagfag wrote:
14 Aug 2022, 19:23

Code: Select all

global g_BufferedKeys := []

ih := InputHook("L0 I1") ; no limit; ignore SendLevel < 1 (prevents recursion)
ih.KeyOpt("{All}", "N")
ih.OnKeyDown := Func("OnKeyDown")
ih.Start()

OnKeyDown(InputHook, VK, SC) {
	g_BufferedKeys.Push({VK: VK, SC: SC})
}

SetTimer ConsumeKeys, 1
ConsumeKeys() {
	if g_BufferedKeys.Count()
	{
		Key := g_BufferedKeys.RemoveAt(1) ; pop front
		Send % Format("{vk{:02X}sc{:03X}}", Key.VK, Key.SC)
		Sleep 50
	}
}
WOW... Thank you so much!! That code is way over my level, but DAMN it works perfectly!!! It's amazing... I tested it thoroughly on a notepad and it works amazing. It has already made playing 90% easier, although in-game it still snags even though it shouldn't, but I think it has to do with the connection to the servers, so basically 100% the game's fault.
Thanks again... so cool, and quick too... :)))

Post Reply

Return to “Ask for Help (v1)”