{"id":71293,"date":"2004-10-05T14:35:00","date_gmt":"2004-10-05T14:35:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/10\/05\/how-can-i-pause-a-script-and-then-resume-it-when-a-user-presses-a-key-on-the-keyboard\/"},"modified":"2004-10-05T14:35:00","modified_gmt":"2004-10-05T14:35:00","slug":"how-can-i-pause-a-script-and-then-resume-it-when-a-user-presses-a-key-on-the-keyboard","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-pause-a-script-and-then-resume-it-when-a-user-presses-a-key-on-the-keyboard\/","title":{"rendered":"How Can I Pause a Script and Then Resume It When a User Presses a Key on the Keyboard?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! I would like to be able to pause my script, then have it resume as soon as the user presses any key on the keyboard. How do I do that?<BR><BR>&#8212; AL<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, AL. Boy, this question takes us back: \u201cPress any key to continue,\u201d along with \u201cAbort, Retry, or Fail,\u201d has to be one of the most famous phrases in computing history. Excuse us for a second while we sigh and think about the good old days. Sigh \u2026.<\/P>\n<P>Back in the old days \u201cPress any key to continue\u201d was a cornerstone of nearly every batch file ever written. All you had to do back then was insert a pause command in the middle of the batch file; at that point, the batch file would halt and &#8211; like a faithful old dog &#8211; just sit there and wait for you to press a key on the keyboard. As soon as you pressed a key &#8211; any key &#8211; the batch file would resume again. Anyone interested in taking a walk down memory lane can try this simple little batch file to see how it all worked:<\/P><PRE class=\"codeSample\">echo off\npause\necho The batch file is complete.\n<\/PRE>\n<P>Note: Save this as a .bat file, not a .vbs file.<\/P>\n<P>No doubt many of you are thinking, \u201cWow, scripting is so much better and so much more powerful than batch files. I bet there\u2019s a <I>really<\/I> cool way to do Press any key in a script.\u201d To tell you the truth, we would have thought that way, too. But guess what: there <I>isn\u2019t<\/I> a way to replicate this function in a script.<\/P>\n<P>OK, there might be a goofy workaround of some kind, but off the top of our heads we couldn\u2019t find one. The problem is that Windows Script Host isn\u2019t designed to be an event-driven environment; in other words, WSH doesn\u2019t know how to sit around and wait until an event (such as a key being pressed) occurs. Combine that with a limited ability to interact with the command line, and you end up with a scripting environment that can\u2019t replicate the functionality of the pause command.<\/P>\n<P>We know: that wasn\u2019t exactly the answer you were hoping to get. But we do have a consolation prize for you. We can\u2019t replicate the pause command, in which the computer sits and waits until you press any key on the keyboard. However, we can give you some code that will sit and wait until you press the ENTER key on the keyboard (and, yes, it <I>has<\/I> to be the ENTER key). This isn\u2019t exactly what you wanted, but it will work:<\/P><PRE class=\"codeSample\">strMessage = &#8220;Press the ENTER key to continue. &#8221;\nWscript.StdOut.Write strMessage<\/p>\n<p>Do While Not WScript.StdIn.AtEndOfLine\n   Input = WScript.StdIn.Read(1)\nLoop\nWScript.Echo \u201cThe script is complete.\u201d\n<\/PRE>\n<P>What this script does is display a message on screen, and then use StdIn (found only in WSH 5.6, 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; as soon as you press ENTER, the script resumes. What if you press some key other than ENTER? No big deal: the value of the key you pressed will appear on screen, as will the values of any other keys you press. The script will simply display the value of any key you press until you finally hit ENTER. At that point, the script breaks out of the loop and continues.<\/P>\n<P>In case you\u2019re wondering, StdIn is really intended as a way for you to use the command line to enter data into a script. For example, your script might prompt you to enter a user name or a file name. That\u2019s why it waits for you to press the ENTER key, as a way of indicating that data entry is complete. Otherwise, you might type the first letter of the user name only to see StdIn grab that and start running with it. The ENTER key is the official signal that data entry is done.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I would like to be able to pause my script, then have it resume as soon as the user presses any key on the keyboard. How do I do that?&#8212; AL Hey, AL. Boy, this question takes us back: \u201cPress any key to continue,\u201d along with \u201cAbort, Retry, or Fail,\u201d has to [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[2,3,4,5],"class_list":["post-71293","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I would like to be able to pause my script, then have it resume as soon as the user presses any key on the keyboard. How do I do that?&#8212; AL Hey, AL. Boy, this question takes us back: \u201cPress any key to continue,\u201d along with \u201cAbort, Retry, or Fail,\u201d has to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71293","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=71293"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71293\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=71293"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71293"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71293"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}