How to make SendInput work the same way as x::y? Topic is solved

Ask gaming related questions (AHK v1.1 and older)
McDeJay

How to make SendInput work the same way as x::y?

10 Sep 2018, 09:58

Hey Guys!

So I'm trying to make a controls remapping tool for a game that doesn't have that feature. The game uses left, right, and space for moving left, right and jumping.
However I want to share this with other people in our community, so I want it to be usable without working with the code.

The best way that works perfectly is deciding on the keys beforehand, and hard-coding them into my program. This way they behave like you would expect, you hold down a key, and the character moves smoothly, without pauses. I'm talking about this way:

Code: Select all

a::left
d::right
w::space
However because I want it to be remapable, I need the keys to be variables, and use labels. The below code is the only way I have found so far, but with it, the movement of the character is not smooth, it acts the same way as your cursor would, it steps once to the left, the waits, then it takes more steps more often. But it is never the smooth movement that you get from "a::left", it is always quick succession of button presses, and it also stops when you press a new button.

Code: Select all

Gui Controls: New,, Controls
Gui Controls: Show, w300 h183
Gui Controls: Add, Text, x10 y13 w50 Right, Left
Gui Controls: Add, Hotkey, x70 y10 w150 vLeftKey, %LeftKey%
Gui Controls: Add, Text, x10 y43 w50 Right, Right
Gui Controls: Add, Hotkey, x70 y40 w150 vRightKey, %RightKey%
Gui Controls: Add, Text, x10 y73 w50 Right, Jump
Gui Controls: Add, Hotkey, x70 y70 w150 vJumpKey, %JumpKey%
Gui Controls: Add, Button, x225 y143 w70 h30 gControlsGuiClose, Apply
Return

ControlsGuiClose:
	Gui Controls: Submit, NoHide

	Hotkey, %LeftKey%, LeftKey
	Hotkey, %RightKey%, RightKey
	Hotkey, %JumpKey%, JumpKey
Return

LeftKey:
SendInput {Left}
Return
RightKey:
SendInput {Right}
Return
LeftKey:
SendInput {Space}
Return
So my question is: how can I make SendInput act the same way as "x::y" would, where the keys are actually held down when I hold a button, and not pressed repeatedly. I went trough most articles, and options I could add, or the different Send methods, but nothing works. Or maybe I'm missunderstanding how a basic function works, and it is quite easy to do, and I'm just a moron.

Anyway thanks for your time reading this, and I appreciate any help I get.
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: How to make SendInput work the same way as x::y?

10 Sep 2018, 10:03

Remappings are a shorthand for two hotkeys. You can find the example on the remappings help page: https://autohotkey.com/docs/misc/Remap.htm
SO use those to set up six hotkeys for your three keys.
McDeJay
Posts: 2
Joined: 10 Sep 2018, 09:59

Re: How to make SendInput work the same way as x::y?

10 Sep 2018, 10:57

Sorry, but I don't really understand what you mean by "remappings are a shorthand for two hotkeys", and "SO use those to set up six hotkeys for your three keys."

As far as I understood from the help page, it only remaps the lowercase AND uppercase to the appropriate keys, but I feel like that's not what you meant, as I don't see how that would help me.
Would you please provide me an example on what other hotkey I need to implement?

Code: Select all

LeftKey := "a"

Hotkey, %LeftKey%, Left
Return

Left:
Send {Left}
Return
So what other hotkey I need to implement here for it to work? Thank you!
User avatar
Nextron
Posts: 1391
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win10 AHK: Unicode x32

Re: How to make SendInput work the same way as x::y?  Topic is solved

10 Sep 2018, 11:39

The docs say:
When a script is launched, each remapping is translated into a pair of hotkeys. For example, a script containing a::b actually contains the following two hotkeys instead:

Code: Select all

*a::
SetKeyDelay -1   ; If the destination key is a mouse button, SetMouseDelay is used instead.
Send {Blind}{b DownR}  ; DownR is like Down except that other Send commands in the script won't assume "b" should stay down during their Send.
return

*a up::
SetKeyDelay -1  ; See note below for why press-duration is not specified with either of these SetKeyDelays.
Send {Blind}{b up}
return
So you should do something like:

Code: Select all

Hotkey, *%LeftKey%, LeftKey
Hotkey, *%LeftKey% up, LeftKeyUp

;-------------------

LeftKey:
SetKeyDelay -1   ; If the destination key is a mouse button, SetMouseDelay is used instead.
Send {Blind}{Left DownR}  ; DownR is like Down except that other Send commands in the script won't assume "b" should stay down during their Send.
return

LeftKeyUp:
SetKeyDelay -1  ; See note below for why press-duration is not specified with either of these SetKeyDelays.
Send {Blind}{Left up}
return
And similar for the other two keys.

Normally when you hold down a key, it repeats down events until you release it or you press another key. When you release it, a single up event is sent. So a game knows the key is down not because of the repeating down events but the lack of an up event. Your method repeatedly sends down and up events which already causes different behavior. If you press another key, the first will likely not be noticed anymore too. (If you throw in modifier keys, such as control/alt there a lot more problems you would experience.)
McDeJay
Posts: 2
Joined: 10 Sep 2018, 09:59

Re: How to make SendInput work the same way as x::y?

10 Sep 2018, 12:52

I have no idea how I missed that. Thank you for your help, it works flawlessly!

Return to “Gaming Help (v1)”

Who is online

Users browsing this forum: No registered users and 90 guests