fixed ControllerMonitor.ahk:
#Requires AutoHotkey v2.0
#SingleInstance Force
; Include the XInput library
#include XInput.ahk
global isConnected := false
SetTimer(CheckController, 16) ; 60 times per second (16ms interval)
; Define the hotkey to exit the script
#!x::ExitApp ; Terminates the script
CheckController() {
global isConnected ; Explicitly declare the global variable
state := Buffer(16, 0) ; Ensure the buffer is correctly allocated
if (XInput_GetState(0, state) == 0) { ; 0 means connected
if (!isConnected) {
isConnected := true
}
} else {
if (isConnected) {
isConnected := false
Send("p") ; Send 'p' on disconnection
}
}
}