MouseQuadrant()

Post your working scripts, libraries and tools for AHK v1.1 and older
CyL0N
Posts: 211
Joined: 27 Sep 2018, 09:58

MouseQuadrant()

20 Nov 2018, 06:40

Given a scope for Top,Bottom,Left & RIght, it returns where the cursor is relative to the Monitor(default) or ActiveWindow. It Returns Top,Bottom,Left,Right & Center.

I wrote it for use with GetMouseGesture(),to make Context Sensitive Mouse Gestures relative to where the cursor is relative to Screen/ActiveWindow, see second example.

Code: Select all

;returns if screen is on top,bottom,center,left,right area of screen given the defined scope in percent of each area...see example.
/*
					Top
	___________________________________
	L	|						|	R
	E	|						|	y
	F	|		C E N T E R		|	T
	T	|						|
	____|_______________________|______
					Bottom
*/
Loop{
;	ToolTip % MouseQuadrant(10,90,10,90,"window")		;to use active window as reference frame instead of entire monitor
	ToolTip % MouseQuadrant(10,90,10,90)	;left <= 10% of screen width, right >= 90% of screen width, top <= 10% of screen height, bottom >= 90% of screen height
	Sleep 250
}

;defined scopes are in 'percent',i.e left scope means anything below defined % is designated left,right scope is anything above defined %...
MouseQuadrant(leftScope,rightScope,topScope,bottomScope,coordMode:="screen"){	;coordMode should either be 'window' or 'screen'
	CoordMode, Mouse, % coordMode
	MouseGetPos, mX, mY, mHwnd, mCtrl
	WinGetPos, wX, wY, wW, hH, A
	
	If (mX <= leftScope/100*(coordMode = "screen" ? A_ScreenWidth : wW) && mY >= topScope/100*(coordMode = "screen" ? A_ScreenHeight : hH) && mY <= bottomScope/100*(coordMode = "screen" ? A_ScreenHeight : hH))
		Return "LEFT"
	Else If (mX >= rightScope/100*(coordMode = "screen" ? A_ScreenWidth : wW) && mY >= topScope/100*(coordMode = "screen" ? A_ScreenHeight : hH) && mY <= bottomScope/100*(coordMode = "screen" ? A_ScreenHeight : hH))
		Return "RIGHT"
	Else If (mY <= topScope/100*(coordMode = "screen" ? A_ScreenHeight : hH))
		Return "TOP"
	Else If (mY >= bottomScope/100*(coordMode = "screen" ? A_ScreenHeight : hH))
		Return "BOTTOM"
	Else
		Return "CENTER"
	
}
Context Sensitive Mouse Gesture Example: Perform Gesture In Various parts of Screen to test it out...

Code: Select all

~Ctrl::		;gesture hotkey, i imagine most would use a mousekey for gestures,ctrl is simply an example suitable for testing as i've got radial menu running...
	GetMouseGesture(True)
	While GetKeyState(LTrim(A_ThisHotkey,"~")){
		ToolTip % MG := GetMouseGesture()
		Sleep 150
	}
	;left <= 20% of screen width, right >= 80% of screen width, top <= 20% of screen height, bottom >= 80% of screen height
	MQ := SubStr(MouseQuadrant(20,80,20,80),1,1)	;take only the first letter of the quadrant,for simplified function names...
	IsFunc(MG "_" MQ) ? %MG%_%MQ%() : (IsFunc(MG) ? %MG%() : "")	;example allows creation of gestures by defining functions that comprise U,D,R,L as function name,with no upper limit on the extent of the gesture,i.e UDUDUDUDL for ex.
	ToolTip
	GetMouseGesture(True)
Return


UDL(){
	MsgBox % "Function Run,if no function was defined for specific quadrant. Or Quadrant Has No Assigned Function.`n`n CurrentQuadrant:`n`t" MouseQuadrant(20,80,20,80)
}

UDL_T(){	;function defined gesture example for example below
	MsgBox Gesture On Top
}

UDL_L(){	;function defined gesture example for example below
	MsgBox Gesture To The LEFT
}

UDL_R(){	;function defined gesture example for example below
	MsgBox Gesture To The Right
}


;returns if screen is on top,bottom,center,left,right area of screen given the defined scope in percent of each area...see example.
/*
					Top
	___________________________________
	L	|						|	R
	E	|						|	y
	F	|		C E N T E R		|	T
	T	|						|
	____|_______________________|______
					Bottom
*/
;defined scopes are in 'percent',i.e left scope means anything below defined % is designated left,right scope is anything above defined %...
MouseQuadrant(leftScope,rightScope,topScope,bottomScope,coordMode:="screen"){	;coordMode should either be 'window' or 'screen'
	CoordMode, Mouse, % coordMode
	MouseGetPos, mX, mY, mHwnd, mCtrl
	WinGetPos, wX, wY, wW, hH, A
	
	If (mX <= leftScope/100*(coordMode = "screen" ? A_ScreenWidth : wW) && mY >= topScope/100*(coordMode = "screen" ? A_ScreenHeight : hH) && mY <= bottomScope/100*(coordMode = "screen" ? A_ScreenHeight : hH))
		Return "LEFT"
	Else If (mX >= rightScope/100*(coordMode = "screen" ? A_ScreenWidth : wW) && mY >= topScope/100*(coordMode = "screen" ? A_ScreenHeight : hH) && mY <= bottomScope/100*(coordMode = "screen" ? A_ScreenHeight : hH))
		Return "RIGHT"
	Else If (mY <= topScope/100*(coordMode = "screen" ? A_ScreenHeight : hH))
		Return "TOP"
	Else If (mY >= bottomScope/100*(coordMode = "screen" ? A_ScreenHeight : hH))
		Return "BOTTOM"
	Else
		Return "CENTER"
	
}


GetMouseGesture(reset := false){
	Static
	mousegetpos,xpos2, ypos2
	dx:=xpos2-xpos1,dy:=ypos1-ypos2
	,( abs(dy) >= abs(dx) ? (dy > 0 ? (track:="u") : (track:="d")) : (dx > 0 ? (track:="r") : (track:="l")) )	;track is up or down, left or right
	,abs(dy)<4 and abs(dx)<4 ? (track := "") : ""	;not tracking at all if no significant change in x or y
	,xpos1:=xpos2,ypos1:=ypos2
	,track<>SubStr(gesture, 0, 1) ? (gesture := gesture . track) : ""	;ignore track if not changing since previous track
	,gesture := reset ? "" : gesture
	Return gesture
}
live ? long & prosper : regards
hasantr
Posts: 933
Joined: 05 Apr 2016, 14:18
Location: İstanbul

Re: MouseQuadrant()

31 Dec 2019, 06:05

CyL0N wrote:
20 Nov 2018, 06:40
Given a scope for Top,Bottom,Left & RIght, it returns where the cursor is relative to the Monitor(default) or ActiveWindow. It Returns Top,Bottom,Left,Right & Center.

I wrote it for use with GetMouseGesture(),to make Context Sensitive Mouse Gestures relative to where the cursor is relative to Screen/ActiveWindow, see second example.

Code: Select all

;returns if screen is on top,bottom,center,left,right area of screen given the defined scope in percent of each area...see example.
/*
					Top
	___________________________________
	L	|						|	R
	E	|						|	y
	F	|		C E N T E R		|	T
	T	|						|
	____|_______________________|______
					Bottom
*/
Loop{
;	ToolTip % MouseQuadrant(10,90,10,90,"window")		;to use active window as reference frame instead of entire monitor
	ToolTip % MouseQuadrant(10,90,10,90)	;left <= 10% of screen width, right >= 90% of screen width, top <= 10% of screen height, bottom >= 90% of screen height
	Sleep 250
}

;defined scopes are in 'percent',i.e left scope means anything below defined % is designated left,right scope is anything above defined %...
MouseQuadrant(leftScope,rightScope,topScope,bottomScope,coordMode:="screen"){	;coordMode should either be 'window' or 'screen'
	CoordMode, Mouse, % coordMode
	MouseGetPos, mX, mY, mHwnd, mCtrl
	WinGetPos, wX, wY, wW, hH, A
	
	If (mX <= leftScope/100*(coordMode = "screen" ? A_ScreenWidth : wW) && mY >= topScope/100*(coordMode = "screen" ? A_ScreenHeight : hH) && mY <= bottomScope/100*(coordMode = "screen" ? A_ScreenHeight : hH))
		Return "LEFT"
	Else If (mX >= rightScope/100*(coordMode = "screen" ? A_ScreenWidth : wW) && mY >= topScope/100*(coordMode = "screen" ? A_ScreenHeight : hH) && mY <= bottomScope/100*(coordMode = "screen" ? A_ScreenHeight : hH))
		Return "RIGHT"
	Else If (mY <= topScope/100*(coordMode = "screen" ? A_ScreenHeight : hH))
		Return "TOP"
	Else If (mY >= bottomScope/100*(coordMode = "screen" ? A_ScreenHeight : hH))
		Return "BOTTOM"
	Else
		Return "CENTER"
	
}
Context Sensitive Mouse Gesture Example: Perform Gesture In Various parts of Screen to test it out...

Code: Select all

~Ctrl::		;gesture hotkey, i imagine most would use a mousekey for gestures,ctrl is simply an example suitable for testing as i've got radial menu running...
	GetMouseGesture(True)
	While GetKeyState(LTrim(A_ThisHotkey,"~")){
		ToolTip % MG := GetMouseGesture()
		Sleep 150
	}
	;left <= 20% of screen width, right >= 80% of screen width, top <= 20% of screen height, bottom >= 80% of screen height
	MQ := SubStr(MouseQuadrant(20,80,20,80),1,1)	;take only the first letter of the quadrant,for simplified function names...
	IsFunc(MG "_" MQ) ? %MG%_%MQ%() : (IsFunc(MG) ? %MG%() : "")	;example allows creation of gestures by defining functions that comprise U,D,R,L as function name,with no upper limit on the extent of the gesture,i.e UDUDUDUDL for ex.
	ToolTip
	GetMouseGesture(True)
Return


UDL(){
	MsgBox % "Function Run,if no function was defined for specific quadrant. Or Quadrant Has No Assigned Function.`n`n CurrentQuadrant:`n`t" MouseQuadrant(20,80,20,80)
}

UDL_T(){	;function defined gesture example for example below
	MsgBox Gesture On Top
}

UDL_L(){	;function defined gesture example for example below
	MsgBox Gesture To The LEFT
}

UDL_R(){	;function defined gesture example for example below
	MsgBox Gesture To The Right
}


;returns if screen is on top,bottom,center,left,right area of screen given the defined scope in percent of each area...see example.
/*
					Top
	___________________________________
	L	|						|	R
	E	|						|	y
	F	|		C E N T E R		|	T
	T	|						|
	____|_______________________|______
					Bottom
*/
;defined scopes are in 'percent',i.e left scope means anything below defined % is designated left,right scope is anything above defined %...
MouseQuadrant(leftScope,rightScope,topScope,bottomScope,coordMode:="screen"){	;coordMode should either be 'window' or 'screen'
	CoordMode, Mouse, % coordMode
	MouseGetPos, mX, mY, mHwnd, mCtrl
	WinGetPos, wX, wY, wW, hH, A
	
	If (mX <= leftScope/100*(coordMode = "screen" ? A_ScreenWidth : wW) && mY >= topScope/100*(coordMode = "screen" ? A_ScreenHeight : hH) && mY <= bottomScope/100*(coordMode = "screen" ? A_ScreenHeight : hH))
		Return "LEFT"
	Else If (mX >= rightScope/100*(coordMode = "screen" ? A_ScreenWidth : wW) && mY >= topScope/100*(coordMode = "screen" ? A_ScreenHeight : hH) && mY <= bottomScope/100*(coordMode = "screen" ? A_ScreenHeight : hH))
		Return "RIGHT"
	Else If (mY <= topScope/100*(coordMode = "screen" ? A_ScreenHeight : hH))
		Return "TOP"
	Else If (mY >= bottomScope/100*(coordMode = "screen" ? A_ScreenHeight : hH))
		Return "BOTTOM"
	Else
		Return "CENTER"
	
}


GetMouseGesture(reset := false){
	Static
	mousegetpos,xpos2, ypos2
	dx:=xpos2-xpos1,dy:=ypos1-ypos2
	,( abs(dy) >= abs(dx) ? (dy > 0 ? (track:="u") : (track:="d")) : (dx > 0 ? (track:="r") : (track:="l")) )	;track is up or down, left or right
	,abs(dy)<4 and abs(dx)<4 ? (track := "") : ""	;not tracking at all if no significant change in x or y
	,xpos1:=xpos2,ypos1:=ypos2
	,track<>SubStr(gesture, 0, 1) ? (gesture := gesture . track) : ""	;ignore track if not changing since previous track
	,gesture := reset ? "" : gesture
	Return gesture
}
It would be nice if there were corners. :)
User avatar
iilabs
Posts: 296
Joined: 07 Jun 2020, 16:57

Re: MouseQuadrant()

06 Dec 2020, 11:56

How would one go about converting this to a 3x3 grid GUI and use mouse gesture to edge grab?

Could one make a GUI with 3x3 with Button and use this mouse gesture to only work if the GUI is set to active?

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 54 guests