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:
Code: Select all
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!