Instead of using a fixed location, which might behave differently on other screen sizes, you could use WinGetPos
and set the X-Y coordinates dynamically. To prevent a "frozen" mouse when users switch with Atl+Tab you can make the mouse lock only when your game is running by checking with IfWinActive
. Instead of a loop with a 100 ms wait, I opted for a 100 ms timer to call the script, but using a loop or while would work just as well.
SetTitleMatchMode, 2
SetTimer, CheckWin, 100
CheckWin:
{
IfWinActive, YourAppNameHere ; ONLY runs when Your App is Active
{
WinGetPos, XStart, YStart, Width, Height, YourAppNameHere ; Use AHK Window Spy
XPos:=XStart+(Width/2)
YPos:=YStart+(Height/2)
MouseMove, %XPos%, %YPos%, 90
Return
}
}
^F1:: ; Ctrl+F1 to stop the timer based loop
SetTimer, CheckWin, Off
Return
Here is another solution:
#SingleInstance Force
#installKeybdHook
#Persistent
Xbutton2:: ; Toggle fix mouse in Center
Confine := !Confine
ClipCursor( Confine, A_ScreenWidth/2, A_ScreenHeight/2, A_ScreenWidth/2, A_ScreenHeight/2 )
Return
ClipCursor( Confine=True, x1=0 , y1=0, x2=1, y2=1 )
{
VarSetCapacity(R,16,0), NumPut(x1,&R+0),NumPut(y1,&R+4),NumPut(x2,&R+8),NumPut(y2,&R+12)
Return Confine ? DllCall( "ClipCursor", UInt,&R ) : DllCall( "ClipCursor" )
}
while (0=0)
is kinda confusing since it simply evaluates towhile (true)
– mikew