Skip to content

Instantly share code, notes, and snippets.

@tKe
Last active August 5, 2022 18:27
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tKe/a14df94a7bf4a7aa6e5d to your computer and use it in GitHub Desktop.
Save tKe/a14df94a7bf4a7aa6e5d to your computer and use it in GitHub Desktop.
AutoHotkey Focus Elite: Dangerous on Joystick Movement
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
;#Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#SingleInstance force
SetFormat, float, 03 ;remove decimals from joystick values
;This script will activate your game window whenever
;any joystick axis changes. Handy for when you're
;Alt-Tabbed out of the game on your second monitor.
;This script is influenced by http://redd.it/294q99 by Norgz
;Configuration
MonitoredJoysticks=1,2 ; comma-separated list of joystick IDs to monitor
MonitorInterval=200 ; the time (in ms) between checking the state of the joystick
GameWindowId=ahk_class FrontierDevelopmentsAppWinClass ; the game window identifier
;Initialise the static variables and joystick API
; for some reason these values may change?
Loop, parse, MonitoredJoysticks, `,,
GetJoyState(A_LoopField)
Sleep 500
Loop, parse, MonitoredJoysticks, `,,
GetJoyState(A_LoopField)
;Main loop
Loop {
If WinExist(GameWindowId) {
Loop, parse, MonitoredJoysticks, `,,
{
joyChanges := GetJoyState(A_LoopField)
If not WinActive(GameWindowId) {
if joyChanges <>
{
ToolTip, Switching to Elite due to %joyChanges% on #%A_LoopField%
SetTimer, RemoveToolTip, 2000
WinActivate
Break
}
}
}
} else {
ToolTip, Elite - Dangerous window not detected. Exiting
Sleep 3000
Return
}
Sleep %MonitorInterval%
}
;Returns 1 if the axis value has changed by more than minDelta since the last time
; it was called for this joy/axis combo or 0 if the change is less than minDelta or no change
HasAxisChanged(joy, axis, minDelta=5) {
static
if axis = P
axis = POV
delta := 0
GetKeyState, current, %joy%Joy%axis%
delta := current - last%axis%%joy%
if (delta < 0) {
delta := delta * -1
}
last%axis%%joy% := current
return delta > minDelta
}
;Returns 1 if the set of down buttons has changed since the last time
; it was called for this joy or 0 if the set of buttons hasn't changed
HasButtonsChanged(JoystickNumber) {
static
GetKeyState, joy_buttons, %JoystickNumber%JoyButtons
buttons_down =
Loop, %joy_buttons%
{
GetKeyState, joy%a_index%, %JoystickNumber%joy%a_index%
if joy%a_index% = D
buttons_down = %buttons_down%,%a_index%
}
changed := buttons_down <> last%JoystickNumber%
last%JoystickNumber% := buttons_down
return changed
}
;Returns the set of changed axis (P=POV, B=Buttons) since the last change
GetJoyState(JoystickNumber=0) {
GetKeyState, joy_info, %JoystickNumber%JoyInfo
joy_axis := "XY" joy_info
changes =
Loop, parse, joy_axis
if HasAxisChanged(JoystickNumber, A_LoopField)
changes = %changes%%A_LoopField%
if HasButtonsChanged(JoystickNumber)
changes = %changes%B
return changes
}
RemoveToolTip:
SetTimer, RemoveToolTip, Off
ToolTip
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment