Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Confining Mouse to a window


  • Please log in to reply
17 replies to this topic
x79animal
  • Members
  • 1021 posts
  • Last active: May 14 2013 04:21 PM
  • Joined: 01 May 2010
As per request of another thread. Here is a script which will confine the mouse to the active window, good for RTS games where you can only scroll if the mous is at the edge (but not outside)




f1::     ;on
WinGetPos, VarX, VarY, Width, Height, A
VarX2 := VarX + Width
VarY2 := VarY + Height
ClipCursor( True, VarX, VarY, VarX2, VarY2)
Return

f2::ClipCursor( False,0,0,0,0)      ;off

If your playing windowed then this script will clip the border and title bar too
f1::     ;on
WinGetPos, VarX, VarY, Width, Height, A
Varx := VarX + 10
Vary := VarY + 30
VarX2 := VarX + Width - 20    ;10 + 10
VarY2 := VarY + Height - 40   ;30 + 10
ClipCursor( True, VarX, VarY, VarX2, VarY2)
Return





you need the function too

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" )
}

To interested veterans, it seems no-one noticed that the first two coords are the top left of the box and the next two are the bottom left. [in clipcursor function[

i saw the screenheight thing and i only thought you could block per axis and not in boxes, good thing i guessed and got it right. Whoever made the function is a clever guy =]

  • Guests
  • Last active:
  • Joined: --
veterans know where the script comes from (Corrupt).......and that it was intended to limit cursor in active window...............

MsgBox, <ctrl><alt>a to lock mouse to Notepad window`n<ctrl><alt>s to release
Run, Notepad
Return 

^!a::
LockMouseToWindow("Untitled - Notepad")
Return

^!s::
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
}


x79animal
  • Members
  • 1021 posts
  • Last active: May 14 2013 04:21 PM
  • Joined: 01 May 2010
can you link the source of that code?

MasterFocus
  • Moderators
  • 4323 posts
  • Last active: Jan 28 2016 01:38 AM
  • Joined: 08 Apr 2009

it seems no-one noticed that the first two coords are the top left of the box and the next two are the bottom left

This is known. :| If in doubt, just check the MSDN page.

Also, please refer to where you got that function from. Provide a link, at least.
( <!-- m -->http://www.autohotke... ... 413#293413<!-- m --> )

Reminds me of this code of mine.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Antonio Fran├ža -- git.io -- github.com -- ahk4.net -- sites.google.com -- ahkscript.org

Member of the AHK community since 08/Apr/2009. Moderator since mid-2012.


x79animal
  • Members
  • 1021 posts
  • Last active: May 14 2013 04:21 PM
  • Joined: 01 May 2010
ive had the function for a long time just never used it, so didnt have a reference at hand.


Reminds me of this code of mine.


I wonder why i have never come across this, at one point i was searching the forums for that function but never found one

Xia Mey
  • Members
  • 105 posts
  • Last active: Oct 01 2015 01:55 PM
  • Joined: 23 Dec 2012

veterans know where the script comes from (Corrupt).......and that it was intended to limit cursor in active window...............
 

MsgBox, <ctrl><alt>a to lock mouse to Notepad window`n<ctrl><alt>s to release
Run, Notepad
Return 

^!a::
LockMouseToWindow("Untitled - Notepad")
Return

^!s::
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
}

hello. Could you make lock mouse by just click on the window?

^!s release. click on notpade to lock again.

 

Many many thanks

WinWaitActive, Warcraft III
LockMouseToWindow("Warcraft III")
Return

^space::
LockMouseToWindow()
Return

how to repeat this action?



noname
  • Members
  • 650 posts
  • Last active:
  • Joined: 12 Nov 2011

Could only use Notepad to test ...


#Persistent 
SetTitleMatchMode, 2
settimer,alert,1000
return

alert:
IfWinActive, Notepad
  {
  LockMouseToWindow("Notepad")
  SetTimer, alert,off
  }
return


^space::
LockMouseToWindow()
sleep 3000           ; to have some time to deactivate window!
SetTimer, alert,on
return

esc::
LockMouseToWindow()
exitapp




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
}

winXP  and ahk unicode


Xia Mey
  • Members
  • 105 posts
  • Last active: Oct 01 2015 01:55 PM
  • Joined: 23 Dec 2012

 

Could only use Notepad to test ...


#Persistent 
SetTitleMatchMode, 2
settimer,alert,1000
return

alert:
IfWinActive, Notepad
  {
  LockMouseToWindow("Notepad")
  SetTimer, alert,off
  }
return


^space::
LockMouseToWindow()
sleep 3000           ; to have some time to deactivate window!
SetTimer, alert,on
return

esc::
LockMouseToWindow()
exitapp




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
}

I meant that. After deactivate LockMouse, I don't want to press "control space" again and again when I come back to the windowed. I would like it to auto activate LockMouse when I just click on the windowed.

 

Many thanks



noname
  • Members
  • 650 posts
  • Last active:
  • Joined: 12 Nov 2011

Resizing the window will also exit the mouse locking!

#Persistent 
SetTitleMatchMode, 2
SetTitleMatchMode, slow
return




^space::
LockMouseToWindow()
sleep 3000           ; to have some time to deactivate window!
Hotkey, Lbutton,on
return


esc::
LockMouseToWindow()
exitapp

#IfWinExist Notepad
~Lbutton::
MouseGetPos, ,,hwnd
WinGetTitle, title,ahk_id %hwnd%
  ifinstring, title,Notepad
      {
      LockMouseToWindow("Notepad")
      Hotkey, Lbutton,off
      }
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
}

winXP  and ahk unicode


Xia Mey
  • Members
  • 105 posts
  • Last active: Oct 01 2015 01:55 PM
  • Joined: 23 Dec 2012

 

Resizing the window will also exit the mouse locking!

#Persistent 
SetTitleMatchMode, 2
SetTitleMatchMode, slow
return




^space::
LockMouseToWindow()
sleep 3000           ; to have some time to deactivate window!
Hotkey, Lbutton,on
return


esc::
LockMouseToWindow()
exitapp

#IfWinExist Notepad
~Lbutton::
MouseGetPos, ,,hwnd
WinGetTitle, title,ahk_id %hwnd%
  ifinstring, title,Notepad
      {
      LockMouseToWindow("Notepad")
      Hotkey, Lbutton,off
      }
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
}

Thank you



chaz
  • Members
  • 192 posts
  • Last active: Oct 01 2015 02:42 AM
  • Joined: 26 Mar 2013

I wonder if it would be possible to implement something like this to fix the problem multi-monitor users have when they find it hard to put their cursors in the corner of the monitor (e.g. the Show Desktop button)? Windows 8 fixes this problem by adding little stops at the corners, but the problem still exists in Windows 7.


Find me at the other forum as timeFlies.


hoppfrosch
  • Members
  • 399 posts
  • Last active: Feb 26 2016 05:31 AM
  • Joined: 25 Jan 2006

I wonder if it would be possible to implement something like this to fix the problem multi-monitor users have when they find it hard to put their cursors in the corner of the monitor (e.g. the Show Desktop button)? Windows 8 fixes this problem by adding little stops at the corners, but the problem still exists in Windows 7.

WindowPadX allows confining the mouse to the current screen via hotkey ...



chaz
  • Members
  • 192 posts
  • Last active: Oct 01 2015 02:42 AM
  • Joined: 26 Mar 2013

WindowPadX allows confining the mouse to the current screen via hotkey ...

 

That's not what I meant. What I meant was like what Windows 8 has, where the mouse is unrestricted and acts like normal unless the mouse is within 20-or-so pixels of the horizontal edge of the screen, when it will hit a stop if you try to move onto the other monitor. If the mouse is brought up a bit it can go onto the other monitor. This was implemented in Windows 8 because of the "hot-corners." Just use a Windows 8 dual-monitor setup and you'll see what I mean.


Find me at the other forum as timeFlies.


Rijul Ahuja
  • Members
  • 763 posts
  • Last active: Nov 25 2013 10:16 AM
  • Joined: 14 Mar 2012

There are some limitations within this method. The ClipCursor is reset every time the active window is changed / a new one is created, etc.

To avoid this, use shell - code shows a basic example.

Success:=ClipCursor(true,1200,100,A_ScreenWidth,A_ScreenHeight)
DetectHiddenWindows, On
WinGet, hwnd, ID, % "ahk_pid " DllCall("GetCurrentProcessId")
DllCall( "RegisterShellHookWindow", UInt,hWnd )
MsgNum := DllCall( "RegisterWindowMessage", Str,"SHELLHOOK" )
OnMessage( MsgNum, "ShellMessage" )
Return

ShellMessage( wParam,lParam ) {
Success:=ClipCursor(True,1200,100,A_ScreenWidth,A_ScreenHeight)
}

ClipCursor(Confine,x1,y1,x2,y2) 
	{
	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)
	}

Abandon the forum. The community has decided in a democratic vote to leave this website because of inactive and perverse administration.

Very few of the contributing members remain here.


chaz
  • Members
  • 192 posts
  • Last active: Oct 01 2015 02:42 AM
  • Joined: 26 Mar 2013

Can't test it now, but I'll let you know if it works.


Find me at the other forum as timeFlies.