Prevent user to mouse click within spesific areas

Get help with using AutoHotkey and its commands and hotkeys
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Prevent user to mouse click within spesific areas

18 Jan 2015, 19:52

Hi.

I'm about to solve a little challenge. I want to make a script to prevent a user to click within a specific area in a program. In this particular case, it is Firefox and it's tool/bookmark toolbar.

The script is easy enough:

Code: Select all

#ifWinActive, ahk_class MozillaWindowClass

LButton::
	MouseGetPos, musX, musY
	If (musY < 131)
		MouseMove, musX, 130
	Else
		Click
Return

RButton::
	MouseGetPos, musX, musY
	If (musY < 131)
		MouseMove, musX, 130
	Else
		Click right
Return
However, this gives me two problems.

1. It's not possible to trag the mouse in browser window. The click doesn't keep the mouse button down. That makes it impossible to select any text with the mouse.

2. If the user change the active window, it will still be possible to click on the area because Firefox won't be the active window while the user click's the mouse. That way the user might go around the limitation I wish to set.
MJs
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: Prevent user to mouse click within spesific areas

18 Jan 2015, 20:16

how about #If and creating its conditions
read about #If documentations
set your conditions for your hotkeys (e.g LButton, RButton)
it would work fine, and simplify your script
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Re: Prevent user to mouse click within spesific areas

18 Jan 2015, 21:21

Thanks for the tip, I'll try to use #IF to determine if Firefox is the actiwe vindow && position of mouse pointer.

Any idea of the second problem?
MJs
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: Prevent user to mouse click within spesific areas

18 Jan 2015, 21:54

Code: Select all

MouseGetPos, musX, musY
    If (musY < 131)
in other words the condition is based on the position of X (left) and Y(Top) of your mouse
if Firefox is the actiwe vindow && position of mouse pointer
a = (get win active = firefox)
b = (get the pos of mouse = where you want it to be)
if a && b
User avatar
Raccoon
Posts: 53
Joined: 26 Jul 2014, 16:15

Re: Prevent user to mouse click within spesific areas

18 Jan 2015, 23:13

Code: Select all

myfunc()
{
  MouseGetPos, mx, my, hwnd
  WinGetClass, wclass, ahk_id %hwnd%
  if (wclass = MozillaWindowClass) 
    && (mx > whatever) && (my > whatever)
    Return True
}

#IF myfunc()
LButton:: Return
RButton:: Return
#IF ; end if
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Re: Prevent user to mouse click within spesific areas

19 Jan 2015, 16:28

Hello again.

I've decided to make two separate functions and got this code to work:

Code: Select all

#If (firefoxIsActiveWIndow() && MouseAtIllegalPosition(122))

LButton::
RButton::
	MouseGetPos, musX, musY, hoverWinName
	MouseMove, %musX%, 123
	;MsgBox, Mouse hovers over window named %hoverWinName%
	
	; 0x300360
Return

firefoxIsActiveWIndow() {
	Haystack := "ahk_class MozillaWindowClass"
	WinGetClass, activeWindowTitle, A
	IfInString, Haystack, %activeWindowTitle%
		Return 1
	Else
		Return 0
}

MouseAtIllegalPosition(ypos) {
	MouseGetPos, mmusX, mmusY, hoverWinName
	If mmusY < 0
		Return 0	; If mouse is located outside Firefox window (above)
	If hoverWinName = 0x300360
		If not firefoxIsActiveWIndow()
			Return 0	; if mouse hovers over Firefox window AND Firefox is not the active window
	returnValue := mmusY <= 122 ? 1 : 0
	Return returnValue
}
However I tested and the user can still click onto the restricted area if another window is active (but doesn't cover the illegal area) at the moment the user click.
jpginc
Posts: 124
Joined: 29 Sep 2013, 22:35

Re: Prevent user to mouse click within spesific areas

19 Jan 2015, 18:48

You can check which window the mouse is over, even if it's not active, using something like this.

Code: Select all

mouseIsOverFirefox()
{
  MouseGetPos, x, y, windowId
  WinGetClass, windowClass, % "ahk_id " windowId
  return instr(windowClass, "ahk_class MozillaWindowClass")
} 
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Re: Prevent user to mouse click within spesific areas

19 Jan 2015, 19:24

jpginc wrote:You can check which window the mouse is over, even if it's not active, using something like this.

Code: Select all

mouseIsOverFirefox()
{
  MouseGetPos, x, y, windowId
  WinGetClass, windowClass, % "ahk_id " windowId
  return instr(windowClass, "ahk_class MozillaWindowClass")
} 
Thanks, that's a good idea. But I just cannot get the Instr() to work correctly (both lines I have commented out return always false).

So the function I get to work is this:

Code: Select all

mouseIsOverFirefox()
{
  MouseGetPos, x, y, windowId
  WinGetClass, windowClass, % "ahk_id " windowId
  ; MsgBox, Variabelen windowClass = %windowClass%
  ;return instr(windowClass, "ahk_class MozillaWindowClass")
  ;return instr(windowClass, "MozillaWindowClass")
  If (windowClass = "MozillaWindowClass")
	Return 1
} 
User avatar
Gio
Posts: 1095
Joined: 30 Sep 2013, 10:54
Location: Brazil

Re: Prevent user to mouse click within spesific areas

19 Jan 2015, 19:27

Hello there.

This works for me:

Code: Select all

Loop, %A_AppData%\Mozilla\Firefox\Profiles\*, 2, 0
{
Line = `nmenu[label=`"Bookmarks`"] {display: none`;}`nmenu[label=`"Tools`"] {display: none`;}
FileAppend, %Line%, %A_AppData%\Mozilla\Firefox\Profiles\%A_LoopFileName%\chrome\userChrome.css
}
:beer:
User avatar
Almost_there
Posts: 404
Joined: 30 Sep 2014, 10:32

Working version of script - prevent click on Firefox toolbar

19 Jan 2015, 19:34

Then I got to assemble a script that I'm satisfied with:

Code: Select all

#If ((firefoxIsActiveWIndow() && MouseAtIllegalPosition(122)) || (not firefoxIsActiveWIndow() && mouseIsOverFirefox()))

LButton::
RButton::
	
	If (not firefoxIsActiveWIndow()) {
		WinActivate, ahk_class MozillaWindowClass
		Return
	}
	MouseGetPos, musX, musY, hoverWinName
	MouseMove, %musX%, 123
Return

firefoxIsActiveWIndow() {
	Haystack := "ahk_class MozillaWindowClass"
	WinGetClass, activeWindowTitle, A
	IfInString, Haystack, %activeWindowTitle%
		Return 1
	Else
		Return 0
}

MouseAtIllegalPosition(ypos) {
	MouseGetPos, mmusX, mmusY
	If mmusY < 0
		Return 0	; If mouse is located outside Firefox window (above)
	returnValue := mmusY <= 122 ? 1 : 0
	Return returnValue
}

mouseIsOverFirefox()
{
  MouseGetPos, x, y, windowId
  WinGetClass, windowClass, % "ahk_id " windowId
  If (windowClass = "MozillaWindowClass")
	Return 1
}
Thanks for all help :D
MJs
Posts: 454
Joined: 23 Sep 2014, 03:29

Re: Prevent user to mouse click within spesific areas

19 Jan 2015, 20:06

OK, glad it works
you may take a look at this, it may not be what you need:

Code: Select all

isMOZILLAACTIVEandMOUSETOPLESS131(){
if !(isMOZILLA := WinActive("ahk_class MozillaWindowClass")) ; is the active window Mozilla
	return false ; if not return false

MouseGetPos,, musY ; verify the top pos
if !(isMOUSE131 := (musY < 131)) ; is the top position less than
	return false ; if not return false

return true

; you can do it like that: one return at the end
/* isMOZILLA := WinActive("ahk_class MozillaWindowClass")
MouseGetPos,, musY
isMOUSE123:= (musY < 131)
return, isMOZILLA && isMOUSE123
*/
}

#if isMOZILLAACTIVEandMOUSETOPLESS131() ; return true if the active window is mozilla and the top mouse pos is lass than 131
; since both are to run the same code, put them one after another
LButton::
RButton::
MouseMove, musX, 130
return

Return to “Ask For Help”

Who is online

Users browsing this forum: Google [Bot], JoeWinograd, Kellyzkorner_NJ, mad3d, mikeyww, Quisquose and 60 guests