Skip to main content
Inspiring
May 27, 2016
Answered

[JS SUI] How to set enter key event in listbox?

  • May 27, 2016
  • 2 replies
  • 1128 views

var w = new Window ("dialog");

var myList = w.add ("listbox", undefined, ["one", "two", "three"]);

myList.addEventListener ("keydown", function (kd) {pressed (kd)});

function pressed (k)

{

    if(k.keyName == "Enter")

    {

        var idx = myList.selection.index;

        alert(idx);

    }

}

w.show ();

Hi.

Only the enter key event does not working in this script.

I want to occur enter key event and get index of listbox item.

Any ideas?

Thanks.

This topic has been closed for replies.
Correct answer TᴀW

Couple more observations:

1. The original script works if you press Shift-Enter, or Ctrl-Enter. (Edit: Peter beat me to it!)

2. Changing the event from "keydown" to "keyup" makes it work (though not repeatedly on the same list item unless the selection is changed in-between, which is a little strange).

Ariel

2 replies

TᴀW
TᴀWCorrect answer
Legend
May 27, 2016

Couple more observations:

1. The original script works if you press Shift-Enter, or Ctrl-Enter. (Edit: Peter beat me to it!)

2. Changing the event from "keydown" to "keyup" makes it work (though not repeatedly on the same list item unless the selection is changed in-between, which is a little strange).

Ariel

id-extras.com | InDesign tools & scripts for typesetters, form designers, and translators
Legend
May 27, 2016

Hi,

Try this ...

var w = new Window ("dialog"); 

var myList = w.add ("listbox", undefined, ["one", "two", "three"]); 

 

w.addEventListener ("keydown", function (kd) {pressed (kd)});

function pressed (k)

{

    if(k.keyName == "Enter") 

    { 

        var idx = myList.selection.index; 

        alert(idx); 

    }

}

w.show ();

Regards

Peter Kahrel
Community Expert
Community Expert
May 27, 2016

> Try this ...

That doesn't work, Ronald.

But the highly sophisticated approach of pressing various keys thinking 'You never know' did the trick again: Ctrl+Enter (on Windows) gives the desired result, both in K159's and in Ronald's version.

In CS6, Enter works fine, so Enter not working is a CC problem.

P.