[javascript] determine if field has focus  SOLVED

Forum for the PDF-XChange Editor - Free and Licensed Versions

Moderators: TrackerSupp-Daniel, Tracker Support, Paul - Tracker Supp, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Ivan - Tracker Software, Tracker Supp-Stefan

Mathew
User
Posts: 239
Joined: Thu Jun 19, 2014 7:30 pm

[javascript] determine if field has focus

Post by Mathew »

Is there a way to determine which field has focus from javascript? Or if a field has focus?

I want to be able to programmatically add some text and a date to the current field, but can't figure out how to know which field is active.
User avatar
Tracker Supp-Stefan
Site Admin
Posts: 17960
Joined: Mon Jan 12, 2009 8:07 am
Location: London

Re: [javascript] determine if field has focus

Post by Tracker Supp-Stefan »

Hello Mathew,

Would listening for the Field/Focus event work for you?
image.png
Or do you need to check which is the active field at a time when a script is run manually and is not constantly listening for that event?

Kind regards,
Stefan
You do not have the required permissions to view the files attached to this post.
Mathew
User
Posts: 239
Joined: Thu Jun 19, 2014 7:30 pm

Re: [javascript] determine if field has focus

Post by Mathew »

Stefan,

Yes, I'm looking to add text to the currently selected field.

But you bring up an interesting approach, although it's very intrusive because it makes changes to the current document whether I plan to add text to it or not: I could add the OnFocus and OnBlur actions to set a custom property on all text fields. Then I just check for that property in a script that's run from the menu.

Code: Select all

{
let f = this.getField("Text1");
f.setAction("OnFocus", "this.focus=true;");
f.setAction("OnBlur", "this.focus=false;");
}
Unfortunately I think it clobbers the current action (if any) on the field, and I don't think there's a way to access that current action (?), so can't restore it or add to it. Would need to somehow monitor for newly opened document, add the events. I think it's problematic as a general tool though. :(
User avatar
Roman - Tracker Supp
Site Admin
Posts: 306
Joined: Sun Nov 21, 2004 3:19 pm

Re: [javascript] determine if field has focus

Post by Roman - Tracker Supp »

Hi Mathew,
I do not currently see an non-intrusive way to achieve that. We will consider adding hasFocus property to Field object, as an PDF-XChange extension to the Acrobat JS API.
Mathew
User
Posts: 239
Joined: Thu Jun 19, 2014 7:30 pm

Re: [javascript] determine if field has focus

Post by Mathew »

Thank you Roman and Stefan. For now, I'll figure out some sort of workaround, but I'll look forward to the day of .hasFocus as an additional property...
- Mathew.
User avatar
TrackerSupp-Daniel
Site Admin
Posts: 8624
Joined: Wed Jan 03, 2018 6:52 pm

[javascript] determine if field has focus

Post by TrackerSupp-Daniel »

:)
Dan McIntyre - Support Technician
Tracker Software Products (Canada) LTD

+++++++++++++++++++++++++++++++++++
Our Web site domain and email address has changed as of 26/10/2023.
https://www.pdf-xchange.com
Support@pdf-xchange.com
User avatar
Roman - Tracker Supp
Site Admin
Posts: 306
Joined: Sun Nov 21, 2004 3:19 pm

Re: [javascript] determine if field has focus  SOLVED

Post by Roman - Tracker Supp »

Mathew wrote: Wed Mar 27, 2024 6:25 pm I'll look forward to the day of .hasFocus as an additional property...
The property has been implemented and will be available in the next release of the Editor.
Mathew
User
Posts: 239
Joined: Thu Jun 19, 2014 7:30 pm

Re: [javascript] determine if field has focus

Post by Mathew »

wow great.
User avatar
Roman - Tracker Supp
Site Admin
Posts: 306
Joined: Sun Nov 21, 2004 3:19 pm

Re: [javascript] determine if field has focus

Post by Roman - Tracker Supp »

Actually, we have implemented it as a method, not property, to be more consistent with the existing Acrobat API ( Field.setFocus() ).