Sorry for the necro, but this thread favors well in the searches I've done and is very precisely what I was searching for except without a working solution to actually doing it in AHK, so this is what I cobbled together:
Code: Select all
#SingleInstance Force
InstallMouseHook
CoordMode("mouse", "screen")
OnExit (*) => SystemCursor("Show") ; Ensure the cursor is made visible when the script exits.
winTitle := "ahk_exe NieR Replicant ver.1.22474487139.exe"
waitTime := 750 ; milliseconds
SetTimer(Main, 10)
Main() {
if WinActive(winTitle) {
if (A_TimeIdlePhysical > waitTime)
SystemCursor("Hide")
else
SystemCursor("Show")
} else
SystemCursor("Show")
}
SystemCursor(cmd) { ; cmd = "Show|Hide|Toggle|Reload"
static visible := true, c := Map()
static sys_cursors := [32512, 32513, 32514, 32515, 32516, 32642, 32643, 32644, 32645, 32646, 32648, 32649, 32650]
if (cmd = "Reload" or !c.Count) { ; Reload when requested or at first call.
for i, id in sys_cursors {
h_cursor := DllCall("LoadCursor", "Ptr", 0, "Ptr", id)
h_default := DllCall("CopyImage", "Ptr", h_cursor, "UInt", 2, "Int", 0, "Int", 0, "UInt", 0)
h_blank := DllCall("CreateCursor", "Ptr", 0, "Int", 0, "Int", 0, "Int", 32, "Int", 32, "Ptr", Buffer(32 * 4, 0xFF), "Ptr", Buffer(32 * 4, 0))
c[id] := { default: h_default, blank: h_blank }
}
}
switch cmd {
case "Show": visible := true
case "Hide": visible := false
case "Toggle": visible := !visible
default: return
}
for id, handles in c {
h_cursor := DllCall("CopyImage", "Ptr", visible ? handles.default : handles.blank, "UInt", 2, "Int", 0, "Int", 0, "UInt", 0)
DllCall("SetSystemCursor", "Ptr", h_cursor, "UInt", id)
}
}
It very simply checks if the game (in my case Nier Replicant) is the active window and hides the cursor after 750 milliseconds of mouse idle time, otherwise the cursor is shown
The SystemCursor function is copied from the DllCall docs:
https://www.autohotkey.com/docs/v2/lib/DllCall.htm#ExHideCursor