WebContextMenuWaitShown

Post your working scripts, libraries and tools for AHK v1.1 and older
SundayProgrammer
Posts: 143
Joined: 25 Dec 2020, 12:26

WebContextMenuWaitShown

Post by SundayProgrammer » 09 May 2022, 11:26

Code: Select all

Tab & t::
XButton1::
XButton2::
	Send, {RButton}
	If WebContextMenuWaitShown(3)
		Send, t
	Else MsgBox, Oops...
	Return

WebContextMenuWaitShown(maxTolsec) {
	beg := A_TickCount, shown := False
	While, not (shown := WebContextMenuShown()) and A_TickCount - beg < maxTolsec * 1000
	{	MouseGetPos, x, y
		MouseMove, x+9, y+9
		If not (shown := WebContextMenuShown())
		{	MouseMove, x+9, y-9
			If not (shown := WebContextMenuShown())
			{	MouseMove, x-9, y+9
				If not (shown := WebContextMenuShown())
				{	MouseMove, x-9, y-9
					shown := WebContextMenuShown()
				}
			}
		}If shown
			Break
		Else MouseMove, x, y
	}Return shown
}
WebContextMenuShown() {
	Return GetFocusedParent().Name = "Web context"
}
GetFocusedParent() {	;based on the script from Descolada at https://www.autohotkey.com/boards/viewtopic.php?p=459451#p459451
	IUIAutomation := ComObjCreate(CLSID_CUIAutomation := "{ff48dba4-60ef-4201-aa87-54103eef594e}", IID_IUIAutomation := "{30cbe57d-d9d0-452a-ab13-7ac5ac4825ee}")
	DllCall(NumGet(NumGet(IUIAutomation+0)+8*A_PtrSize), "ptr",IUIAutomation, "ptr*",element)	;IUIAutomation::GetFocusedElement
	DllCall(NumGet(NumGet(IUIAutomation+0)+14*A_PtrSize), "ptr",IUIAutomation, "ptr*",walker)	;IUIAutomation::ControlViewWalker
	DllCall(NumGet(NumGet(walker+0)+3*A_PtrSize), "ptr",walker, "ptr",element, "ptr*",parent) ;IUIAutomationTreeWalker::GetParentElement
	DllCall(NumGet(NumGet(parent+0)+23*A_PtrSize), "ptr", parent, "ptr*", name)	;IUIAutomationElement::CurrentName
	DllCall(NumGet(NumGet(parent+0)+10*A_PtrSize), "ptr", parent, "uint",30045, "ptr", (VarSetCapacity(val,8+2*A_PtrSize)+NumPut(0,val,0,"short")+NumPut(0,val,8,"ptr"))*0+&val)	;IUIAutomationElement::GetCurrentPropertyValue
	ObjRelease(parent)
	ObjRelease(walker)
	ObjRelease(element)
	ObjRelease(IUIAutomation)
	Return {"Name":StrGet(name,"utf-16"), "Value":StrGet(NumGet(val,8,"ptr"),"utf-16")}
}

Return to “Scripts and Functions (v1)”