Disable mouse clicks doesn't always work Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
arytinki
Posts: 2
Joined: 17 Oct 2017, 09:11

Disable mouse clicks doesn't always work

17 Oct 2017, 09:33

Hello. I have made this code that for a certain application, only should allow mouse clicks inside a certain region and also change the pointer to a "stop" pointer outside that region. I'm having the problem that this particular application still handles mouse clicks fine outside the allowed region, even though in other apps it blocks the clicks just like it should! What other (maybe more robust) ways are there to disable mouse left- and right clicks?

Code: Select all

CursorSTOP = 0

SetTimer, MouseCheck, 50	; Check mouse position every 50 ms

MouseCheck:
MouseGetPos, MouseX, MouseY

	IfWinActive, ApplicationTitle1
	{
	IsInsideB1 = 0
	IsInsideB2 = 0

	; Box 1 - An area inside the App, the allowed region
	; ----------
	B1_leftX = 0
	B1_rightX = 370
	B1_topY = 85
	B1_bottomY = 485

		If MouseX > %B1_leftX%
		{
			If MouseX < %B1_rightX%
			{
				If MouseY > %B1_topY%
				{
					If MouseY < %B1_bottomY%
					{
						IsInsideB1 = 1
					}
				}
			}
		}
	; ----------

	; Box 2 - The area of the entire App
	; ----------
	B2_leftX = 0
	B2_rightX = 830
	B2_topY = 0
	B2_bottomY = 630

		If MouseX > %B2_leftX%
		{
			If MouseX < %B2_rightX%
			{
				If MouseY > %B2_topY%
				{
					If MouseY < %B2_bottomY%
					{
						IsInsideB2 = 1
					}
				}
			}
		}
	; ----------

	
		If (IsInsideB1 = 0)
		{
			If (IsInsideB2 = 1)
			{
				If CursorSTOP = 0	; If we are not inside the allowed region and are inside the app, then set the pointer to stop symbol
				{
					SetSystemCursor()
					CursorSTOP = 1
				}
			}

			Else
			{
				RestoreCursors()
				CursorSTOP = 0
			}
		}

		Else
		{
			RestoreCursors()
			CursorSTOP = 0
		}
	}

	Else
	{
		RestoreCursors()
		CursorSTOP = 0
	}

Return

Pause::
ExitApp

LButton::
   If (CursorSTOP = 0) 	; If the pointer is not the stop symbol, click as normal and otherwise just return
   {
      MouseClick, Left,,,,, D
      KeyWait, LButton
      MouseClick, Left,,,,, U
   }
return

RButton::
   If (CursorSTOP = 0) 
   {
      MouseClick, Right,,,,, D
      KeyWait, RButton
      MouseClick, Right,,,,, U
   }
return

SetSystemCursor()
{
	IDC_NO := 32648
	CursorHandle := DllCall( "LoadCursor", Uint, 0, Int, IDC_NO )
	Cursors = 32512,32513,32514,32515,32516,32640,32641,32642,32643,32644,32645,32646,32648,32649,32650,32651

	Loop, Parse, Cursors, `,
	{
		DllCall( "SetSystemCursor", Uint, CursorHandle, Int, A_Loopfield )
	}
}

RestoreCursors()
{
	SPI_SETCURSORS := 0x57
	DllCall( "SystemParametersInfo", UInt, SPI_SETCURSORS, UInt, 0, UInt, 0, UInt, 0 )
}
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Disable mouse clicks doesn't always work  Topic is solved

17 Oct 2017, 10:55

By chance, your Application wouldn't happen to be running with administrator rights, would it? I don't know for sure that would interfere, but it may. It could prevent AHK from intercepting the mouse clicks intended for it.

Can you confirm that the CoordMode's default settings for MouseClick, which is relative (so coordinates move as the window itself is moved to different places on screen), is what you want?

And can you confirm that the pointer is changing to a STOP correctly?

On a side note, I think you can simplify your If statement tree quite nicely like this. If (MouseX > B1_leftX) && (MouseX < B1_rightX) && (MouseY > B1_topY) && (MouseY < B1_BottomY). This uses expression syntax by including the parentheses, so you don't need the %'s around the variable names. Then the && is the AND operator. The same can be applied for your other If statement trees.
arytinki
Posts: 2
Joined: 17 Oct 2017, 09:11

Re: Disable mouse clicks doesn't always work

18 Oct 2017, 04:19

The app runs as admin, and in fact I got it working by compiling the script and running it as admin as well. Thanks! :)

I'm aware of the ugly if structure, it will be changed once I'm done with it all.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: filipemb and 310 guests