Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sleep until any keystroke

1,324 views
Skip to first unread message

vdja...@gmail.com

unread,
May 22, 2007, 9:00:08 AM5/22/07
to
Hi,

Could you please help me to write a script which makes wscript.sleep
until any keystroke.
This is my attempt:

Dim Input
Input = Input & WScript.StdIn.Read(1)
If input is TRUE then
wscript.quit
Else wscript.sleep 5000
End If

Jeffery Hicks

unread,
May 22, 2007, 10:25:23 AM5/22/07
to

You need a loop like this:

Dim x
WScript.echo "Enter some text and press Enter twice:"

do while not wscript.StdIn.AtEndofLine
x=x & wscript.StdIn.ReadLine
Loop

WScript.echo "You entered " & x
wscript.quit


--
Jeffery Hicks
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com
VBScript & Windows PowerShell Training -
www.ScriptingTraining.com/classes.asp
Windows PowerShell? - www.SAPIENPress.com/powershell.asp

blog: http://blog.SAPIEN.com
blog: http://jdhitsolutions.blogspot.com

vdja...@gmail.com

unread,
May 23, 2007, 4:52:03 AM5/23/07
to
Hi Jeffery,

Thanks for the help.
This is not exactly what i need. The script should be running timer
(wscript.sleep) for some 10 minutes and the any keystroke would break
the sleep and
it will start another command.

Best regards,
Vitali.

On 22 май, 17:25, Jeffery Hicks <jhi...@sapien.com> wrote:

Jeffery Hicks

unread,
May 23, 2007, 5:37:13 AM5/23/07
to

In that case you can't use Wscript.Sleep What you might do is use
DateAdd to calculate a future time, how long you want to wait and create
an outer loop that runs until the current time matches the target time.
Within the loop, insert the Do While stdin example. Add a line to jump
to quit after the loop.

--
Jeffery Hicks
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

VBScript & Windows PowerShell Training -
www.ScriptingTraining.com/classes.asp
Windows PowerShell? - www.SAPIENPress.com/powershell.asp

blog: http://blog.SAPIEN.com
blog: http://jdhitsolutions.blogspot.com

vdja...@gmail.com

unread,
May 23, 2007, 6:58:19 AM5/23/07
to
Dim StartTime, EndTime, input

StartTime = Time
EndTime = DateAdd("s", 2, Time)

Do Until StartTime=EndTime

WScript.echo "press enter"
do while not wscript.StdIn.AtEndofLine
input=input & wscript.StdIn.ReadLine
Loop
wscript.quit
Loop

Where and how should i add wscript.quit after the timer has been run?

On 23 май, 12:37, Jeffery Hicks <jhi...@sapien.com> wrote:
> vdjat...@gmail.com wrote:
> > Hi Jeffery,
>
> > Thanks for the help.
> > This is not exactly what i need. The script should be running timer
> > (wscript.sleep) for some 10 minutes and the any keystroke would break
> > the sleep and
> > it will start another command.
>
> > Best regards,
> > Vitali.
>

Jeffery Hicks

unread,
May 23, 2007, 1:39:54 PM5/23/07
to
It sounded good in my head. I can't find a way to abort the inner loop
if the timer expires which I think is what you want. The user has X
number of seconds to press enter or whatever before the script
terminates. Does this have to be using StdIn? Are you expecting the
user to enter some information or respond to a message?

--
Jeffery Hicks
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

VBScript & Windows PowerShell Training -
www.ScriptingTraining.com/classes.asp
Windows PowerShell? - www.SAPIENPress.com/powershell.asp

blog: http://blog.SAPIEN.com
blog: http://jdhitsolutions.blogspot.com

Jeffery Hicks

unread,
May 23, 2007, 2:10:08 PM5/23/07
to

I think by design that ReadLine for STDIN will sit and wait forever.
That's why I asked if you really need to use STDIN. The one hack that
comes to mind is to kick off a a WMI ansynch event sink watching the
CSCript process and if the process lifetime exceeds a value, then
terminate the process. This is a real hack and not one I can even
guarantee will work. Sometimes we have to accept that some things just
can't be scripted.

--
Jeffery Hicks
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

VBScript & Windows PowerShell Training -
www.ScriptingTraining.com/classes.asp
Windows PowerShell? - www.SAPIENPress.com/powershell.asp

blog: http://blog.SAPIEN.com
blog: http://jdhitsolutions.blogspot.com

Dr J R Stockton

unread,
May 24, 2007, 12:30:19 PM5/24/07
to
In microsoft.public.scripting.vbscript message <Of3UE4RnHHA.5032@TK2MSFT
NGP02.phx.gbl>, Wed, 23 May 2007 05:37:13, Jeffery Hicks
<jhi...@sapien.com> posted:

>
>In that case you can't use Wscript.Sleep What you might do is use
>DateAdd to calculate a future time, how long you want to wait and
>create an outer loop that runs until the current time matches the
>target time. Within the loop, insert the Do While stdin example. Add a
>line to jump to quit after the loop.
>

Rather than attempting to hog the CPU, could one not do repeated short
sleeps, testing in between to see whether input was available?

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.

vdja...@gmail.com

unread,
May 25, 2007, 5:46:19 AM5/25/07
to
Hello John,

Could you please provide an example of you suggestion in code ?


On 24 май, 19:30, Dr J R Stockton <j...@merlyn.demon.co.uk> wrote:
> In microsoft.public.scripting.vbscript message <Of3UE4RnHHA.5032@TK2MSFT
> NGP02.phx.gbl>, Wed, 23 May 2007 05:37:13, Jeffery Hicks
> <jhi...@sapien.com> posted:
>
>
>
> >In that case you can't use Wscript.Sleep What you might do is use
> >DateAdd to calculate a future time, how long you want to wait and
> >create an outer loop that runs until the current time matches the
> >target time. Within the loop, insert the Do While stdin example. Add a
> >line to jump to quit after the loop.
>
> Rather than attempting to hog the CPU, could one not do repeated short
> sleeps, testing in between to see whether input was available?
>
> --

> (c) John Stockton, Surrey, UK. ?...@merlyn.demon.co.uk Turnpike v6.05 IE 6.

mr_unreliable

unread,
May 26, 2007, 9:03:21 PM5/26/07
to
vdja...@gmail.com wrote

>
> Could you please help me to write a script which makes wscript.sleep
> until any keystroke.

Unfortunately, this is almost impossible from script.

If you are willing to try another language, one that supports
api calls, then the "right way" to do this is to use system
"hooking". For example, if you set a keyboard hook, then the
system will notify you every time a keyboard key is pressed,
(a.k.a. a keyboard event).

If interested, there is already a preprogrammed system-wide
keyboard hooking utility, provided by "Delphin Software"
(Juregen Thuemmier) called "DSKEYBRD", and available from
"allapi":

http://allapi.mentalis.org/vbexamples/vbexample.php?vbexample=DSKEYBRD&category=MISC

DSKEYBRD documentation may be found here:

http://www.aixsoft.de/downloads/freeware/delphin/dskeybrd.txt

This is an actX dll, and may be called from script, but
is not "usable" from script, because you have to pass it
an address for your "hookproc" (something difficult if not
impossible from script). And so, you have to use some other
language.

If you want to follow up on this, I suggest writing a "wrapper"
for DSKEYBRD in vb, in the form of an actX object (ocx or dll).
VB has what you need to interface with DSKEYBRD. Then you can
code an appropriate interface that would be acceptable
to vbScript.

cheers, jw

0 new messages