4 Replies Latest reply on Sep 12, 2011 8:02 AM by vze26m98

    EventListener consuming keyboard input


      Hi all-


      I have a ScriptUI dialog that has textinput boxes, and companion dropdownlists that fill them with data. I wrote an EventListener to handle cursor-up/cursor-down keypresses to select items from the dropdownlist and enter them into the textinput box. This arrangement works fine, but if I type directly into the textinputbox, it retains only the last character typed. In other words, if I type "ez", then only the "z" is displayed in the textinputbox. The code looks like this ("Level" is the type name of the data I'm entering):


        w.p.add('statictext', [116,25,176,50], 'Level:');
        w.p.boxLevel = w.p.add('edittext', [116,50,176,72], DATA(0, colLevel));
        w.p.ddlLevels = w.p.add('dropdownlist', [177,50,202,72], lstLevels);
        w.p.ddlLevels.selection = setSel(w.p.boxLevel.text, lstLevels);
        w.p.ddlLevels.onChange = function (){
          // Don't take the descriptions as data:
          w.p.boxLevel.text = w.p.ddlLevels.selection.text.split('\t', 1);
        w.p.boxLevel.addEventListener('keydown', function (k)
          w.p.ddlLevels.active = true;
          if (k.keyName === 'Down') { w.p.ddlLevels.selection += 1; }
          if (k.keyName === 'Up') { w.p.ddlLevels.selection -= 1; }
          w.p.boxLevel.active = true;


      I think I've reread all the ScriptUI documentation, but I'm not clear what I need to do to display typed strings in textinputbox. Is there an incantation to pass the data along to rest of the event hierarchy? Or do I need to concatenate what's already in the textinputbox with the keypress received in the EventListener? Or some other technique?


      Thanks for any help!


      Best, Charles

        • 1. Re: EventListener consuming keyboard input
          Marc Autret Level 4

          Hi Charles,


          Does your code illustrate the issue you are talking about? I don't understand what you want to do "when [you] type directly into the textinputbox" (except for Up/Down keys). Maybe you have problems with EditText.texselection, but your snippet is not related to such things.


          What we can say is that the following line sounds wrong:

          w.p.boxLevel.text = w.p.ddlLevels.selection.text.split('\t', 1);

          since String.split() returns an array…


          Apart from that, I don't see any critical problem in the ScriptUI stuff. Your general pattern is:


          var     listLevels = ["Level1","Level2","Level3","Level4"],
               // ---
               w = new Window('dialog'),
               eLevel = w.add('edittext'),
               dlLevels = w.add('dropdownlist', undefined, listLevels);
          dlLevels.onChange = function()
               { // Sync eLevel with dlLevels sel.
               eLevel.text = this.selection.text;
          eLevel.addEventListener('keydown', function (ev)
               { // Controls dlLevels sel. from eLevel with Up/Down arrow keys
               var t = ev.keyName,
                    dl = dlLevels;
               t = ('Down'==t)-('Up'==t); // +1 | 0 | -1
               if( t ) dl.selection += t;
               dl = null;
          dlLevels.selection = 0;


          which works for me.


          So, tell us more about the issue.




          • 2. Re: EventListener consuming keyboard input
            Peter Kahrel Adobe Community Professional & MVP

            [Comment Beside the point]

            • 3. Re: EventListener consuming keyboard input
              vze26m98 Level 1

              Hi Marc-


              Thanks for your interest.


              Concerning split(), I guess it's returning an array with one string element in it. Perhaps a problem, but not the one I'm concerned with. I appreciate you pointing it out.


              Perhaps a sequence of screenshots will help. Here's my dialog on open, with data from a TextFrame in the Levels textinputbox:



              Here I hit "Cursor Down" to take me to the next item in the dropdownlist:




              If I try to type "L1" into the box instead of using the cursor, when I type "L", I get this:




              which of course is what you'd expect. Bu then when I type "1", I get this:




              where the "1" overwrites the "L". (Please note that I want "free typing" here. I'm not trying to limit typing to only the items in the dropdownlist.)


              My assumption is that I've interposed an EventListener (ie Handler) in the normal event chain that enables "correct" typing in the textinputbox. Typing in the box is now no longer being handled correctly, and I have to manage the typing myself in the EventListener.


              I had assumed that my EventListener was just looking for two keypresses (Cursor Up/Cursor Down), but if they were not found, that control and data would simply pass on to the next part of the event chain. What I'm unsure about is whether I've stopped the event chain, and now need to "pass" control to the remaining links of the chain, or handle the buffering of the textinputbox myself (or some other solution). Something like:


              w.p.boxLevel.text += k.keyName


              at the end of the EventListener. (Actually, the above is bad because it wouldn't handle BackSpace, etc. I'm unsure where I'd get the keyboard data such that I could just concatenate onto the textinputbox data.)


              So I hope that's clear. Again thanks to you (and anyone else) who might have insight into a "correct" solution.


              Best, Charles

              • 4. Re: EventListener consuming keyboard input
                vze26m98 Level 1

                To answer my own question, the EventListener code in my first post switches focus to dropdownlist and back to editbox for ANY keypress. The change of focus is what was causing the overwrite of single typed characters.


                It was, however, valuable to get other people's assesment of the code. Given they saw not much wrong, I was more confident that my problem might be a logic error.


                Thanks, Charles