Lock the Mouse Between Certain Coordinates of a Specific Window

Ask gaming related questions
bejaminn
Posts: 14
Joined: 30 Sep 2017, 20:50

Lock the Mouse Between Certain Coordinates of a Specific Window

Post by bejaminn » 22 Jan 2021, 16:58

Hello Guys! as the title says, I wish I could lock the mouse movements between certain coordinates of a specific window

I already have the code to lock the mouse movement within a specific window, but I would like to be more specific and add specific coordinates, for example: (Lock Between(551, 742, x) and Between(235, 494, y) of the "NameWindow"

this is my actual code:

Code: Select all

F21::
LockMouseToWindow("NameWindow")
Return

F24::
LockMouseToWindow()
Return


LockMouseToWindow(llwindowname="")
{
  VarSetCapacity(llrectA, 16)
  WinGetPos, llX, llY, llWidth, llHeight, %llwindowname%
  If (!llWidth AND !llHeight) {
    DllCall("ClipCursor")
    Return, False
  }
  Loop, 4 { 
    DllCall("RtlFillMemory", UInt,&llrectA+0+A_Index-1, UInt,1, UChar,llX >> 8*A_Index-8) 
    DllCall("RtlFillMemory", UInt,&llrectA+4+A_Index-1, UInt,1, UChar,llY >> 8*A_Index-8) 
    DllCall("RtlFillMemory", UInt,&llrectA+8+A_Index-1, UInt,1, UChar,(llWidth + llX)>> 8*A_Index-8) 
    DllCall("RtlFillMemory", UInt,&llrectA+12+A_Index-1, UInt,1, UChar,(llHeight + llY) >> 8*A_Index-8) 
  } 
  DllCall("ClipCursor", "UInt", &llrectA)
Return, True
}
thanks for the help! :bravo:
bejaminn
Posts: 14
Joined: 30 Sep 2017, 20:50

Re: Lock the Mouse Between Certain Coordinates of a Specific Window

Post by bejaminn » 22 Jan 2021, 18:09

Hello Mike!
thanks for your quick help and reply! :D

Sorry if i'm a bit clumsy with the thing of coding... but i tried to occupy the code of the link and it didn't work properly ...on the other hand i'm trying to limit the movement using the xy of a specific window, not the whole screen

Thank you very much for your availability and help!
User avatar
mikeyww
Posts: 5347
Joined: 09 Sep 2014, 18:38

Re: Lock the Mouse Between Certain Coordinates of a Specific Window

Post by mikeyww » 22 Jan 2021, 18:14

I don't see the problem at hand, but OK.
bejaminn
Posts: 14
Joined: 30 Sep 2017, 20:50

Re: Lock the Mouse Between Certain Coordinates of a Specific Window

Post by bejaminn » 22 Jan 2021, 18:19

When press F24 the the cursor is still stuck in the area (dont want to exit the app, just put off)
The code use coordinates of the whole screen

Code: Select all

OnExit("done")
F21::ClipCursor(True, 50, 350, 150, 550) ; On
F24::ClipCursor()                        ; Off
F4::ExitApp

ClipCursor(confine := False, x1 := 551 , y1 := 234, x2 := 742, y2 := 494) {
 ; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=81573
 VarSetCapacity(R, 16, 0), NumPut(x1, &R+0), NumPut(y1, &R+4), NumPut(x2, &R+8), NumPut(y2, &R+12)
 SoundBeep, % confine ? 1500 : 1000, 30
 Return confine ? DllCall("ClipCursor", UInt, &R) : DllCall("ClipCursor")
}

done() {
 ClipCursor()
}
User avatar
mikeyww
Posts: 5347
Joined: 09 Sep 2014, 18:38

Re: Lock the Mouse Between Certain Coordinates of a Specific Window

Post by mikeyww » 22 Jan 2021, 20:22

Although I don't have F24 on my keyboard, it worked with other function keys. Try F3. You can change the coordinates to whatever you like to match your window, right?

Code: Select all

OnExit("done")
F2::ClipCursor(True, "A") ; On
F3::ClipCursor()          ; Off
F4::ExitApp

ClipCursor(confine := False, wTitle := "") {
 If wTitle {
  WinGetPos, x1, y1, width, height, %wTitle%
  x2 := x1 + width - 1, y2 := y1 + height - 1
 } Else x1 := y1 := x2 := y2 := 0
 ; https://www.autohotkey.com/boards/viewtopic.php?f=76&t=81573
 VarSetCapacity(R, 16, 0), NumPut(x1, &R+0), NumPut(y1, &R+4), NumPut(x2, &R+8), NumPut(y2, &R+12)
 SoundBeep, % confine ? 1500 : 1000, 30
 Return confine ? DllCall("ClipCursor", UInt, &R) : DllCall("ClipCursor")
}

done() {
 ClipCursor()
}
I did notice that if you move the window, the clip seems to deactivate.
bejaminn
Posts: 14
Joined: 30 Sep 2017, 20:50

Re: Lock the Mouse Between Certain Coordinates of a Specific Window

Post by bejaminn » 22 Jan 2021, 20:41

In my code when i press F24 (or other function key) my cursor is released, but in this code that doesn't happen :roll: . (idk why)

Also the idea its to move the window freely, having to place a window in the same position would be very tedious.
I've been trying to get this code for hours and nothing :crazy:
User avatar
mikeyww
Posts: 5347
Joined: 09 Sep 2014, 18:38

Re: Lock the Mouse Between Certain Coordinates of a Specific Window

Post by mikeyww » 22 Jan 2021, 20:49

OK. It's a mystery. Are you running as admin? You are reloading the script, and exiting all other scripts?

Others might have better solutions for you.
bejaminn
Posts: 14
Joined: 30 Sep 2017, 20:50

Re: Lock the Mouse Between Certain Coordinates of a Specific Window

Post by bejaminn » 22 Jan 2021, 22:20

Reloaded the script, always running as admin.... I have not seen any topic that limits the coordinates in the way im tryng
User avatar
mikeyww
Posts: 5347
Joined: 09 Sep 2014, 18:38

Re: Lock the Mouse Between Certain Coordinates of a Specific Window

Post by mikeyww » 23 Jan 2021, 06:34

Here is a similar one that worked. Would first try as is, to see if it works at your end. Use a non-maximized window to test.

Code: Select all

Global uid

F3:: ; F3 = Confine cursor to the active window
uid := WinActive("A"), ShellMessage()
DetectHiddenWindows, On
WinGet, hwnd, ID, % "ahk_pid " DllCall("GetCurrentProcessId")
DllCall("RegisterShellHookWindow", UInt, hWnd)
OnMessage(MsgNum := DllCall("RegisterWindowMessage", Str,"SHELLHOOK"), "ShellMessage")
Return

F4:: ; F4 = Stop confining cursor to the active window; exit
ClipCursor(False, 0, 0, 0, 0)
MsgBox,, Done, Exiting.
ExitApp

ShellMessage(wParam := 0, lParam := 0) {
 If !WinActive("ahk_id " uid)
  Gosub, F4
 WinGetPos, x1, y1, w, h
 Success := ClipCursor(True, x1, y1, x1 + w - 1, y1 + h - 1)
}

ClipCursor(confine, x1, y1, x2, y2) {
 ; https://autohotkey.com/board/topic/61753-confining-mouse-to-a-window/
 If !confine
  Return DllCall("ClipCursor")
 VarSetCapacity(R,16,0), NumPut(x1, &R, 0, "UInt"), NumPut(y1, &R, 4, "UInt")
 NumPut(x2, &R, 8, "UInt"), NumPut(y2, &R, 12, "UInt")
 Return DllCall("ClipCursor", UInt, &R)
}
bejaminn
Posts: 14
Joined: 30 Sep 2017, 20:50

Re: Lock the Mouse Between Certain Coordinates of a Specific Window

Post by bejaminn » 23 Jan 2021, 19:06

Thanks for ur intense help Mike! but sadly, same issue...
Dont want to exit the app, just put off, and idk why that thing is only working in my main code... also the code is using coordinates of the whole screen

I think the best idea is try to add coordinate to lock to this code, because i dont present the issues mentioned on this one

Code: Select all

F21::
LockMouseToWindow("NameWindow")
Return

F24::
LockMouseToWindow()
Return


LockMouseToWindow(llwindowname="")
{
  VarSetCapacity(llrectA, 16)
  WinGetPos, llX, llY, llWidth, llHeight, %llwindowname%
  If (!llWidth AND !llHeight) {
    DllCall("ClipCursor")
    Return, False
  }
  Loop, 4 { 
    DllCall("RtlFillMemory", UInt,&llrectA+0+A_Index-1, UInt,1, UChar,llX >> 8*A_Index-8) 
    DllCall("RtlFillMemory", UInt,&llrectA+4+A_Index-1, UInt,1, UChar,llY >> 8*A_Index-8) 
    DllCall("RtlFillMemory", UInt,&llrectA+8+A_Index-1, UInt,1, UChar,(llWidth + llX)>> 8*A_Index-8) 
    DllCall("RtlFillMemory", UInt,&llrectA+12+A_Index-1, UInt,1, UChar,(llHeight + llY) >> 8*A_Index-8) 
  } 
  DllCall("ClipCursor", "UInt", &llrectA)
Return, True
}
User avatar
mikeyww
Posts: 5347
Joined: 09 Sep 2014, 18:38

Re: Lock the Mouse Between Certain Coordinates of a Specific Window

Post by mikeyww » 23 Jan 2021, 19:19

OK. Sorry I wasn't more helpful!
Post Reply

Return to “Gaming”