Page 1 of 1

Acc Lib, get Acc Object from focus (where Tab key is)

Posted: 12 Jun 2021, 09:31
by newvice
Hello,

I was wondering if it is possible to get an Acc Object from where the focus in a Windows currently is. More precisely where the focus is after using Tab Key.

I usually tab through the elemnts, check if the text matches and send Enter. I would like to achive this with Acc since the Windows 10 Settings windows are not accessible with the standard methods, however very good accessible with help of the Acc Lib.

I built a workaroud with Click to move the Cursor and using Acc_ObjectFromPoint. Its not very "reliable" tho.

How would I go about building something like "Acc_ObjectFromFocus"?

Thanks

Re: Acc Lib, get Acc Object from focus (where Tab key is)

Posted: 12 Jun 2021, 10:06
by doubledave22
Hi, I found this while searching a while back and it did seem to work.

Code: Select all


Acc_Focus(hWnd) 
{ 
	Acc := Acc_ObjectFromWindow(hWnd)
	While IsObject(Acc.accFocus)
		Acc := Acc.accFocus

	return Acc
}

I'm not totally sure how the while loop works so maybe someone can shed some light

Re: Acc Lib, get Acc Object from focus (where Tab key is)

Posted: 13 Jun 2021, 06:45
by newvice
This does work indeed. Shouldve asked before writting the workaround lol.

According to this site...
https://docs.microsoft.com/en-us/dotnet/api/accessibility.iaccessible.accfocus?view=net-5.0

...the accFocus "call" returns an Object if successful and an error code if not.
So the while loop returns the last object were the accFocus call returns successfully.

Reasonable enough for me. I'll use it.

Thanks a lot.

Re: Acc Lib, get Acc Object from focus (where Tab key is)

Posted: 25 Jul 2021, 15:13
by newvice
just doing this works too:

Code: Select all

if (IsObject(acc_window.accFocus))
	{
		acc_window_focus := acc_window.accFocus
	}
	else
	{
		msgbox % "nope"
	}

Re: Acc Lib, get Acc Object from focus (where Tab key is)

Posted: 27 Jul 2021, 11:13
by neogna2
newvice wrote:
25 Jul 2021, 15:13
just doing this works too:

Code: Select all

if (IsObject(acc_window.accFocus))
	{
		acc_window_focus := acc_window.accFocus
	}
	else
	{
		msgbox % "nope"
	}
That returns a different Acc object, at least for some windows, compared to the earlier function by doubledave22 with the While loop. That's at least what I see when I try both on the AutoHotkey.chm help file window.

It appears the function with the While loop first gets an Acc object with focus that is closest to the window's "root". Then it recursively calls accFocus to get a new object "downstream" from the previous one, one or more steps out on a branch in the Acc "tree". Until there is no more downstream child object that satisfies whatever test accFocus uses to determine if an object has keyboard focus.

The function with If only calls accFocus once and because of that returns the Acc object with keyboard focus that is closest to the window's "root".

An older thread about accFocus.

Re: Acc Lib, get Acc Object from focus (where Tab key is)

Posted: 30 Jul 2021, 17:52
by guest3456
neogna2 wrote:
27 Jul 2021, 11:13
newvice wrote:
25 Jul 2021, 15:13
just doing this works too:

Code: Select all

if (IsObject(acc_window.accFocus))
	{
		acc_window_focus := acc_window.accFocus
	}
	else
	{
		msgbox % "nope"
	}
That returns a different Acc object, at least for some windows, compared to the earlier function by doubledave22 with the While loop. That's at least what I see when I try both on the AutoHotkey.chm help file window.

It appears the function with the While loop first gets an Acc object with focus that is closest to the window's "root". Then it recursively calls accFocus to get a new object "downstream" from the previous one, one or more steps out on a branch in the Acc "tree". Until there is no more downstream child object that satisfies whatever test accFocus uses to determine if an object has keyboard focus.

The function with If only calls accFocus once and because of that returns the Acc object with keyboard focus that is closest to the window's "root".

An older thread about accFocus.
great explanation, thanks