Tested with version 2.0-a103-56441b52
What is the alternative to VarSetCapacity? The docs state that VarSetCapacity may be removed later.
Comments/suggestions welcomed!
updated code:
Code: Select all
f1::
global locked_ID
toggle := !toggle
if toggle
{
locked_ID := WinGetID("A")
SetTimer("lock_engage", 250)
tooltip("Mouse cursor locked!")
}
else
{
current_ID := ""
SetTimer "lock_engage", "Delete"
ClipCursor(False,0,0,0,0)
tooltip("Mouse cursor unlocked!")
}
SetTimer () => ToolTip(), -3000
return
lock_engage() {
global current_ID
if !current_ID
current_ID := WinGetID("A")
if ( locked_ID == current_ID )
{
WinGetPos(VarX, VarY, Width, Height, "ahk_id " locked_ID)
VarX2 := VarX + Width - 6
VarY2 := VarY + Height - 6
ClipCursor(True, VarX, VarY, VarX2, VarY2)
}
else
{
SetTimer , "Delete"
ClipCursor(False,0,0,0,0)
tooltip("Different window active: mouse cursor unlocked automatically!")
SetTimer () => ToolTip(), -3000
}
}
ClipCursor(Confine:=True, x1:=0, y1:=0, x2:=1, y2:=1) {
local R := 0
if confine {
R := BufferAlloc(16)
NumPut("int", x1, R)
NumPut("int", y1, R, 4)
NumPut("int", x2, R, 8)
NumPut("int", y2, R, 12)
}
Return DllCall("ClipCursor", "Ptr", R)
}
Code: Select all
f1:: ;on
WinGetPos VarX, VarY, Width, Height, "A"
VarX2 := VarX + Width
VarY2 := VarY + Height
ClipCursor( True, VarX, VarY, VarX2, VarY2)
tooltip("Mouse cursor locked!")
SetTimer () => ToolTip(), -3000
Return
f2:: ;off
ClipCursor( False,0,0,0,0)
tooltip("Mouse cursor unlocked!")
SetTimer () => ToolTip(), -3000
return
ClipCursor( Confine:=True, x1:=0 , y1:=0, x2:=1, y2:=1 ) {
VarSetCapacity(R,16,0)
NumPut("UPtr",x1,&R+0)
NumPut("UPtr",y1,&R+4)
NumPut("UPtr",x2,&R+8)
NumPut("UPtr",y2,&R+12)
Return Confine ? DllCall( "ClipCursor", "UInt",&R ) : DllCall( "ClipCursor" )
}