Using the Return/Enter key...

I'm using a script that someone provided in lieu of using the Auto Complete feature. It uses a Dropdown box and two buttons (one button to add the info, and the other button to delete the info). What I'd like to do is modify the script so the user can just hit the Return/Enter key instead of clicking the button to add the info into the Dropdown box. Then hit the Delete key to delete the info from the Dropdown box. So, then I could eliminate the buttons. Here's the script I'm using for the buttons.

var f = this.getField("Dropdown1");
f.deleteItemAt();

var f = this.getField("Dropdown1");
var ni = f.numItems;
f.insertItemAt(f.value, "", ni);

Is it possible to still keep using this script but modify it so the Return/Enter key enters the info into the Dropdown box and the Delete key will remove the info, or will it require a completely different script? If it does what would that look like? Can someone please help me? Thank you all.

Using Acrobat X on a Mac.


Joel Browne


2 Answers

Voted Best Answer

According to me, it is definitively too complicated with the present API.
Consider this snippet in a Custom Keystroke Script:

var f = event.target;
if (event.commitKey === 2) {
    event.commitKey = 0;
    f.insertItemAt(event.value, event.value, f.numItems);
    event.rc = true;
}

you can add a new item when Enter is pressed but it is not easy to reset the box for the next entry. I found a way by setting event.value="" in the chained Calculate Script upon a conditional test of a global variable set above but it is clearly not linear...

Removing entries is even trickier: I think you would necessarily need another field to focus on and immediately get back to the dropdown box. That is required to force a commit and have the list items up to date after pressing CANC (I didn't tick the "commit selected value immediately" check box but used event.change to parse the list items and find the index of the item to be removed).

A possible workaround is to use a check box next to the dropdown: if checked you would use "commit selected value immediately" (it can be done programmatically by commitOnSelChange) and upon selection try to deleteItemAt.


By Ricardo Falegnami   

Follow-up on Joel's request for improvement of the sample code to insert new values into a dropdown box on pressing Enter and reset it to blank ("commit selected value immediately" not necessary)

Custom Keystroke Script

var f = event.target;
if (event.commitKey === 2) {
    var insertAt = f.numItems;
    try {
        f.insertItemAt(event.value, event.value, insertAt);
    }
    catch (e) {
        // do nothing
    }
    this.customProp = 1;
}

Custom Calculation Script:

if (typeof this.customProp != "undefined") {
    if (this.customProp === 1) {
        event.value = "";
        this.customProp = -1;
    }
}

Things to do now: reset focus on the dropdown so that entries can be entered consecutively.


Ricardo Falegnami   


Please specify a reason: