FORUMS: list search recent posts

Define maximum wait time during "do... while" loop?

COW Forums : Adobe After Effects

<< PREVIOUS   •   FAQ   •   VIEW ALL   •   PRINT   •   NEXT >>
Thiago Costa
Define maximum wait time during "do... while" loop?
on Feb 25, 2019 at 8:57:14 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!


Return to posts index

Oleg Pirogov
Re: Define maximum wait time during "do... while" loop?
on Feb 25, 2019 at 10:34:12 pm

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


Return to posts index

Thiago Costa
Re: Define maximum wait time during "do... while" loop?
on Feb 25, 2019 at 11:01:48 pm

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


Return to posts index


Dave LaRonde
Re: Define maximum wait time during "do... while" loop?
on Feb 26, 2019 at 5:31:29 am

Would you please be so kind as to explain why you want to do something interactive in an After Effects project.

I DO NOT see the need for such a thing.

Dave LaRonde
Promotion Producer
KGAN (CBS) & KFXA (Fox) Cedar Rapids, IA


Return to posts index

Thiago Costa
Re: Define maximum wait time during "do... while" loop?
on Feb 26, 2019 at 10:38:03 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.


Return to posts index

Dave LaRonde
Re: Define maximum wait time during "do... while" loop?
on Feb 26, 2019 at 3:34:24 pm

Thank you for the explanation.
It certainly would NOT be my preferred way of working. All the easy keyboard shortcuts in After Effects already do something.

Dave LaRonde
Promotion Producer
KGAN (CBS) & KFXA (Fox) Cedar Rapids, IA


Return to posts index


Walter Soyka
Re: Define maximum wait time during "do... while" loop?
on Feb 26, 2019 at 5:01:29 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 [twitter]   |   RenderBreak [blog]   |   Profile [LinkedIn]


Return to posts index

Thiago Costa
Re: Define maximum wait time during "do... while" loop?
on Feb 26, 2019 at 8:57:44 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!


Return to posts index

<< PREVIOUS   •   VIEW ALL   •   PRINT   •   NEXT >>
© 2019 CreativeCOW.net All Rights Reserved
[TOP]