How Can I Intermission a Script and So Resume It When a User Presses a Key on the Keyboard?

Hey, Scripting Guy! Question

Hey, Scripting Guy! I would like to be able to pause my script, then have it resume as soon as the user presses any primal on the keyboard. How exercise I do that?

— AL

Spacer Hey, Scripting Guy! Answer Script Center

Hey, AL. Boy, this question takes united states dorsum: "Press whatever cardinal to continue," forth with "Abort, Retry, or Fail," has to exist one of the about famous phrases in calculating history. Alibi us for a second while we sigh and think nigh the adept old days. Sigh ….

Back in the onetime days "Printing any key to proceed" was a cornerstone of nearly every batch file e'er written. All you had to practice back then was insert a pause command in the centre of the batch file; at that indicate, the batch file would halt and – similar a faithful old domestic dog – only sit down there and wait for yous to press a key on the keyboard. Every bit presently as yous pressed a cardinal – any key – the batch file would resume once again. Anyone interested in taking a walk down memory lane tin effort this simple little batch file to see how it all worked:

echo off pause echo The batch file is complete.        

Note: Save this as a .bat file, not a .vbs file.

No dubiousness many of you are thinking, "Wow, scripting is so much meliorate and then much more powerful than batch files. I bet there's a really cool way to do Press any key in a script." To tell you the truth, nosotros would have thought that way, as well. But guess what: at that place isn't a way to replicate this function in a script.

OK, there might be a goofy workaround of some kind, but off the top of our heads we couldn't find i. The problem is that Windows Script Host isn't designed to be an event-driven environment; in other words, WSH doesn't know how to sit around and wait until an outcome (such every bit a key being pressed) occurs. Combine that with a limited ability to interact with the command line, and you terminate upwardly with a scripting environment that can't replicate the functionality of the pause command.

Nosotros know: that wasn't exactly the answer you were hoping to get. But we do have a consolation prize for y'all. We can't replicate the break control, in which the computer sits and waits until yous press any fundamental on the keyboard. However, we can give you some code that will sit down and wait until you lot printing the ENTER key on the keyboard (and, yes, it has to exist the ENTER fundamental). This isn't exactly what you wanted, but it volition piece of work:

strMessage = "Press the ENTER key to go along. " Wscript.StdOut.Write strMessage          

Practise While Not WScript.StdIn.AtEndOfLine Input = WScript.StdIn.Read(ane) Loop WScript.Echo "The script is complete."

What this script does is display a bulletin on screen, and then employ StdIn (plant only in WSH 5.vi, which means this script runs only if you have WSH 5.6 installed) to wait for the user to input data from the command line. StdIn sits there until you press the ENTER key; every bit soon every bit y'all press ENTER, the script resumes. What if y'all printing some key other than ENTER? No big deal: the value of the central yous pressed will appear on screen, every bit volition the values of whatsoever other keys y'all press. The script will simply display the value of whatsoever primal you printing until you finally hit ENTER. At that point, the script breaks out of the loop and continues.

In example you're wondering, StdIn is really intended as a mode for you to utilize the command line to enter data into a script. For example, your script might prompt yous to enter a user name or a file proper noun. That's why it waits for you to press the ENTER primal, every bit a way of indicating that data entry is complete. Otherwise, yous might type the first letter of the user name only to run across StdIn catch that and start running with it. The ENTER key is the official signal that data entry is done.