Simple hold key code. A better solution?

Ask gaming related questions
Teh
Posts: 6
Joined: 09 Jul 2016, 14:08

Simple hold key code. A better solution?

16 Mar 2018, 15:36

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
User avatar
Nwb
Posts: 444
Joined: 29 Nov 2016, 08:56

Re: Simple hold key code. A better solution?

16 Mar 2018, 22:42

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.
I am your average ahk newbie. Just.. a tat more cute. ;)
Teh
Posts: 6
Joined: 09 Jul 2016, 14:08

Re: Simple hold key code. A better solution?

17 Mar 2018, 07:57

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.

Return to “Gaming”

Who is online

Users browsing this forum: No registered users and 15 guests