Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Define maximum wait time during “do… while” loop?

  • Define maximum wait time during “do… while” loop?

    Posted by Thiago Costa on February 25, 2019 at 8:57 pm

    Hi!

    I’m writing a script that needs user interaction to define what the next step is going to be. For that, I wrote a do..while loop that expects a keyboard input. So in the example below, you have to run the script and then press the key “1” or “2” and the script will alert the key name. It is working perfectly, as it follows:


    var keyPress = false;
    var whatToAlert = "No keys were pressed";

    do {
    var kN = ScriptUI.environment.keyboardState.keyName;
    switch(kN){

    case "1":
    whatToAlert = "You pressed the key \"1\"";
    keyPress=true;
    break;

    case "2":
    whatToAlert = "You pressed the key \"2\"";
    keyPress=true;
    break;

    }
    } while (keyPress == false);

    alert(whatToAlert);

    The script is useful as it is, but if the user do not press any key, he will be stuck forever inside the loop. That is why I wanted to add a “time out” function, for the script to stop running if the user did not press any of the two keys for 2 seconds. But I can’t figure out how to do it. I tried adding $.sleep(2000) to the “while” part of the code, but this would make the script wait for 2 seconds even if the user presses one of those keys… So I just run out of things to try…

    If anyone could help would be awesome!
    Thank you in advance!

    Thiago Costa replied 7 years, 5 months ago 3 Members · 5 Replies
  • 5 Replies
  • Oleg Pirogov

    February 25, 2019 at 10:34 pm

    How about setting var start = Date.now() before loop and adding && (Date.now()-start < 2000) to while?

  • Thiago Costa

    February 25, 2019 at 11:01 pm

    Awesome!
    This is exactly what I was looking for!
    Thank you very much!

  • Thiago Costa

    February 26, 2019 at 10:38 am

    Hi,
    I didn’t explain it earlier to focus more on the problem I was having and not take much of your time.

    The script I built is just a script launcher. You run it, and for each key you then press, it runs a specific script. I know there was ft_toolbar and kbar, and I really like those scripts, but I wanted to launch scripts using a custom keyboard or even an extra numpad, so that each key launches a specific script that I set up.

    So I set a shortcut in AE (ctrl+shift+\) to run the script, and configured the extra numpad to run macros. So each key is a combination of “ctrl+shift+\+1”, “ctrl+shift+\+2”, “ctrl+shift+\+3″… and so on…

    I have no idea if this is a long work around or if there is another easier of doing so… But this is working perfectly. The script reads a txt file, containing the key name and the path to the script I want to launch, so it would be easier configure whenever I felt like needed.

    I asked for help thinking about other scenarios, in which I would not have the external numpad and still wanted to use the script, but missed a key, or if the macros was not well configured and so we would not be stuck on a loop forever. Now I’ve made a custom keyboard with Arduino and will soon 3DPrint the enclosure.

    That’s it… I’m a big fan of DIY stuff and thanks to the help I got here I’m one step closer to get it done.

  • Walter Soyka

    February 26, 2019 at 5:01 pm

    You don’t need to write your own event handler; ScriptUI provides one for you.

    Try this sample:

    var win = new Window ("dialog", "Keypress");
    win.add ("button", undefined, "Cancel");
    win.addEventListener ("keydown", function (k) { myKeyDownEventHandler(k)} );

    function myKeyDownEventHandler(key) {
    writeLn("You pressed " + key.keyName);
    win.close();
    }

    win.show();

    When the script is invoked, it attaches an event handler to the window. When a key is pressed, it calls the “myKeyDownEventHandler” function. This is what you’d need to customize to run your macros; for now, this shows the key you pressed in the Info panel. After detecting a keypress, it automatically closes the window.

    Walter Soyka
    Designer & Mad Scientist at Keen Live [link]
    Motion Graphics, Widescreen Events, Presentation Design, and Consulting
    @keenlive   |   RenderBreak [blog]   |   Profile [LinkedIn]

  • Thiago Costa

    February 26, 2019 at 8:57 pm

    Hello Walter, awesome solution!
    It got me thinking about some other possibilities too, since I’m writing this to help my team’s workflow.
    I will make some adjustments to the code as soon as I get home.
    Thank you!

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy