Last active
July 14, 2023 20:46
-
-
Save L0laapk3/3b91784b21963c26d682556c72d27944 to your computer and use it in GitHub Desktop.
Example usage of the vjoy library with autohotkey
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#SingleInstance Force | |
#DllLoad "C:\Program Files\vJoy\x64\vJoyInterface.dll" | |
; I configured vjoy to only have the X axis, all the other buttons etc turned off | |
GAME_NAME := "YOUR GAME HERE" | |
RID := 1 | |
AXIS := 48 ; X axis, starts at 48 for whatever reason | |
; Init stuff | |
if (DllCall("vJoyInterface\AcquireVJD", "UInt", RID) != 1) { | |
MsgBox("Failed to claim vJoy device " . RID) | |
ExitApp | |
} | |
if (DllCall("vJoyInterface\ResetVJD", "UInt", RID) != 1) { | |
MsgBox("Failed to reset vJoy device " . RID) | |
ExitApp | |
} | |
if (DllCall("vJoyInterface\GetVJDAxisExist", "UInt", RID, "UInt", AXIS) != 1) { | |
MsgBox("Axis " . AXIS . " doesn't exist") | |
ExitApp | |
} | |
if (DllCall("vJoyInterface\SetAxis", "Int", 16384, "UInt", RID, "UInt", AXIS) != 1) { ; test axis by centering it | |
MsgBox("Failed to set axis " . AXIS) | |
ExitApp | |
} | |
; End of init stuff | |
limit := 1.0 ; float | |
UpdateSteering() { | |
global limit | |
steering := GetKeyState("Right", "P") - GetKeyState("Left", "P") | |
steering := steering * limit | |
steering := floor((1 + steering) * 2**14) ; from -1..1 to 0..2**15 and convert float to int | |
DllCall("vJoyInterface\SetAxis", "Int", steering, "UInt", RID, "UInt", AXIS) | |
} | |
UpdateLimit(newLimit) { | |
global limit | |
limit := newLimit | |
UpdateSteering() | |
} | |
#HotIf WinActive("ahk_class " . GAME_NAME) | |
; The actual hotkeys | |
Left::UpdateSteering() | |
Left up::UpdateSteering() | |
Right::UpdateSteering() | |
Right up::UpdateSteering() | |
vk41::UpdateLimit(0.3) | |
vk5A::UpdateLimit(0.67) | |
vk45::UpdateLimit(0.8) | |
vk52::UpdateLimit(1.0) | |
loop { | |
WinWaitActive("ahk_class " . GAME_NAME) | |
; some games dont like mixed input methods and alt tabbing | |
if (DllCall("vJoyInterface\ResetVJD", "UInt", RID) != 1) { | |
MsgBox("Failed to reset vJoy device " . RID) | |
ExitApp | |
} | |
UpdateLimit(1.0) | |
WinWaitNotActive("ahk_class " . GAME_NAME) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment