• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit Search
0

How to add a keypress (or combination of keys) to a field or document

Explorer ,
Jan 19, 2023 Jan 19, 2023

Copy link to clipboard

Copied

I would like to call a function (script) in the form after pressing a key or key combination.
I'd appreciate a simple example.

TOPICS
How to , JavaScript , PDF forms

Views

147

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 20, 2023 Jan 20, 2023

It is possible, in theory, to know whether Ctrl or Shift (not Alt, though) were held down when the user types into a text field. I tried it, though, and it only worked with Shift, and even then only when it was not a pre-defined shortcut of the application. In short, it's not very reliable.

 

The other approach is better, I think, and there is actually a way of keeping track of what was the last field a user clicked into. You can add a script to the On Blur event of all the fields in the table t

...

Votes

Translate

Translate
Community Expert ,
Jan 19, 2023 Jan 19, 2023

Copy link to clipboard

Copied

This can't be done at the document level. And the JavaScript model does not provide a simple way to add keyboard shortcuts. 

 

It is possible to create a menu item that contains a shortcut by creating a new menu item. If an "&" is placed in front of any letter in the label of the menu item, then that letter will be displayed with an underscore, which indicates that pressing that key will activate the menu item, when the menu is displayed. 

 

That's as good as you can get, and menu items cannot be created from a document script. 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 19, 2023 Jan 19, 2023

Copy link to clipboard

Copied

Thanks for the answer,
I want to get this functionality at the form design level. I have a large table in it and various possible actions to be performed on individual rows or cells, and creating a button for each such situation is terribly tedious and obscures the form. Creating one button, in turn, will result in the loss of information from where the action was triggered, i.e. from which place in the table, because if I press the button, the table element will lose focus a moment earlier ... and thus information from where it was triggered. So it would be wise to create a "hot key" that I can call at any time.
The solution may be a piece of script placed in the keystroke event handler, provided that you can recognize what was pressed (e.g. Ctrl+Alt+1) and call a function at the same time for a given field, treat these keys as invisible.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 20, 2023 Jan 20, 2023

Copy link to clipboard

Copied

It is possible, in theory, to know whether Ctrl or Shift (not Alt, though) were held down when the user types into a text field. I tried it, though, and it only worked with Shift, and even then only when it was not a pre-defined shortcut of the application. In short, it's not very reliable.

 

The other approach is better, I think, and there is actually a way of keeping track of what was the last field a user clicked into. You can add a script to the On Blur event of all the fields in the table that keeps their name saved into a doc-level variable. When you click the button you access that variable to know what field was last "visited" by the user, and then you can act based on that info. It's a bit tricky, but should work better than the key-combination approach.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 20, 2023 Jan 20, 2023

Copy link to clipboard

Copied

I was thinking exactly the same 🙂
Although, unfortunately, you need to add these few lines to each field in the event after losing focus..
Thank you for the explanation, although I thought there was an elegant way, but at least I won't waste another day searching the internet for "how to do it".... 😉

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 20, 2023 Jan 20, 2023

Copy link to clipboard

Copied

LATEST

Depending on the field names that shouldn't be too difficult. You could use a loop to process all of them, adding the same line of code to each one of their On Blur event. The best would be to just have it call a doc-level function, where all the actual code will be, of course.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines