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

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
newvice
Posts: 24
Joined: 12 Jun 2021, 09:20

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

12 Jun 2021, 09:31

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
doubledave22
Posts: 343
Joined: 08 Jun 2019, 17:36

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

12 Jun 2021, 10:06

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
newvice
Posts: 24
Joined: 12 Jun 2021, 09:20

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

13 Jun 2021, 06:45

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.
newvice
Posts: 24
Joined: 12 Jun 2021, 09:20

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

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"
	}
neogna2
Posts: 590
Joined: 15 Sep 2016, 15:44

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

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.
guest3456
Posts: 3463
Joined: 09 Oct 2013, 10:31

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

30 Jul 2021, 17:52

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


Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Billykid, dinodragon, Google [Bot], mcd and 143 guests