Neutron.ahk icon indicating copy to clipboard operation
Neutron.ahk copied to clipboard

Problem setting focus on first form element after GUI is generated

Open oyhel opened this issue 3 years ago • 3 comments

First of all, huge thanks for your work! This has helped me immensely with quickly getting survey forms up in a clinical setting, filling out and pasting the resulting info.

However, one issue I am struggling with is that forms are not receiving focus after the GUI/Neutron app appears. I have to physically click the window with the Neutron app and the click tab in order for the cursor be positioned in the form (5 radio groups with 5 elements each) forcing me to use the mouse initially instead of keyboard only.

I have tried to play around with

WinActivate, neutron.ahk ; activating the window with the Neutron app Sleep, 200 ControlClick, X170 Y170, neutron.ahk ; to send that needed click Send, {tab} ; to get the cursor positioned

However a) it does not seem to work when code is placed within the app (as part of launching of the Neutron app) and it is also a bit brittle.

Is it possible to force focus on first form elements in a more elegant way?

oyhel avatar Aug 25 '21 19:08 oyhel

I use neutron.doc.focus() to focus the document, but it also works if you want to focus a specific form element. MDN Element.focus()

But you may also need to focus the window immediately after showing it, which you can do more reliably with

WinActivate, % "ahk_id " neutron.hWnd

Also both of these should be after the neutron.Show() call

SaifAqqad avatar Aug 25 '21 19:08 SaifAqqad

Thank you very much for the quick reply!

I added after neutron.Show()

neutron.Show()
neutron.doc.focus()
WinActivate, % "neutron.ahk" neutron.hWnd
Send, {tab}
;WinActivate, neutron.ahk
return

...and it worked as intended, setting focus to the first radio group! Thanks again!

oyhel avatar Aug 25 '21 20:08 oyhel

You actually don't even need the Send, {tab}, you can just set an id for the radio group (ex. "radio_group") and do neutron.doc.getElementById("radio_group").focus()

and you also don't need the window name in WinActivate, just give it the hWnd WinActivate, % "ahk_id " neutron.hWnd

SaifAqqad avatar Aug 25 '21 20:08 SaifAqqad