Page 1 of 1

Simple hold key code. A better solution?

Posted: 16 Mar 2018, 15:36
by Teh
This is a basic working code, which I use to circumvent (and unify) bad controls for racing games as they all have at least keyboard support. Joy18 acts as a modifier that makes joy22 either do a single action (f), or a continuous hold (a) as long as joy22 is pressed. Ideally it should stop without the modifier. But it works good enough.

Anyway, I have only basic understanding of AHK, or any sort of programming for that matter, so i was wondering if there is a more elegant or resource efficient solution to this?

Code: Select all

$5joy22::
{
	if GetKeyState("5joy18","p")
	{ 
		while GetKeyState("5joy22","p")
		send {a down}
		sleep 50
		send {a up}
	}
	else
	{
		send {f down}
		sleep 50
		send {f up}
	}
}
return

Re: Simple hold key code. A better solution?

Posted: 16 Mar 2018, 22:42
by Nwb
I don't think resources is the problem here, ahk barely takes any load for small scripts.

However you should avoid using while (GetKeystate)
https://autohotkey.com/boards/viewtopic.php?t=19745

Here is an example of an alternative using SetTimer instead.

Code: Select all


$5joy22::
if GetKeyState("5joy18","p")
	SetTimer, Press, 0
else
	{
	send {f down}
	sleep 50
	send {f up}
	}
return


Press:
If not GetKeyState("5joy18","p")
	SetTimer, Press, Off
else {
		send {a down}
		sleep 50
		send {a up}
	}
return
You can use another if not GetKeyState if you don't want it to work while only joy18 is held.

Re: Simple hold key code. A better solution?

Posted: 17 Mar 2018, 07:57
by Teh
Don't understand it, but i'll see if i can figure it out.

But this actually works better for my use case, as it's much more comfortable.

Thank you very much.

PS: unfortunately, it's not quite working out as it's not really continuous, but quick repeat, which is not suitable for the application.

PPS: while GetKeyState also really heavily tanks my FPS i just noticed. Keywait instead works well.