0

I use LaTeX to create a fillable PDF document. How can I detect that a \CheckBox has been clicked?

It seems to react only to onfocus={...}, not to onclicked={...} or onchanged{...}.

Do I need to register an EventListener in an insDLJS environment?

Code:

\documentclass{article}
\usepackage{hyperref}

\begin{document}
\begin{Form}
  \CheckBox[name=mytestbox,onchange={app.alert('Hello');},onclick={app.alert('Hello2');}]{Test CheckBox}
\end{Form}

\end{document}
0

The events have other names, in hpdftex.def I find onfocus, onblur, onmousedown, onmouseup, onenter, onexit. There is also onclick, but it seems to be implemented only for Pushbuttons.

Not every event makes really sense for check boxes, e.g. the mouse event seem to conflict with the checking.

\documentclass{article}
\usepackage{hyperref}


\begin{document}
\begin{Form}
\CheckBox[name=mytestbox,onenter={app.alert('Hello');}]{Test CheckBox}

\CheckBox[name=mytestboxb,onexit={app.alert('Hello2');}]{Test CheckBox}
\end{Form}
\end{document}

An alternative to hyperref is eforms (acrotex), which has more options (and also more documentation). See e.g. https://tex.stackexchange.com/a/390882/2388

  • Thanks for your response. onenter and onexit are already triggered when the cursor hovers over the element (respectively leaves it). Onfocus would be more handy in that situation. I took a look into the eform documentation. There doesn't seem to be an onclick for the \CheckBox element either. Did I overlook something? – MPW Nov 5 '18 at 15:15
  • By the way, onmousedown and onmouseup don't work, unfortunately. I'll probably have to solve this with onfocus and onblur, which is only partially satisfying. I'm still wondering if an EventListener code be added manully... – MPW Nov 5 '18 at 15:27
  • \checkBox[\AA{\AAMouseDown{\JS{app.alert("Mouse Down!")}}}]{myCheck}{10bp}{10bp}{On} works fine for me with eforms. – Ulrike Fischer Nov 5 '18 at 15:44
0

The event you are looking for is called validate:

\documentclass{article}
\usepackage{hyperref}

\begin{document}
\begin{Form}
  \CheckBox[name=mytestbox,validate={app.alert('Hello');}]{Test CheckBox}
\end{Form}

\end{document}

According to the hyperref documentation validate contains "JavaScript code to validate the entry", so using it as a general "changed" event might look like a hack, but in reality it generates a /V entry in the form fields additional actions dictionary. While this entry can be used for validation, that's only one possible use. It is specified for PDF 1.7 as a general "onchange" event:

(Optional; PDF 1.3) A JavaScript action that shall be performed when the field’s value is changed. This action may check the new value for validity. (The name V stands for “validate.”)

The advantage of using validate instead of e.g. onmousedown is that validate still works if a user uses another input method, for example selecting the field using the keyboard instead of clicking it with the mouse.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.