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

WScript Shell SendKeys to HTA

477 views
Skip to first unread message

Jason

unread,
Jun 20, 2005, 3:00:15 PM6/20/05
to
Does anyone know of any limitations to using SendKeys to an HTA?

I'm using a TEXTAREA element and sending keystrokes to it. It seems to work
fine until a certain point. For example, in the code below, upon loading, it
will loop from 1 to 1000, each time adding a line number then going to the
next line.

Results in the textarea _should_ look like this:
1
2
[...snip...]
999
1000

On my PC, it only goes to about 850 and looks like this:
1
2
[...snip...]
849
850

Occassionally it will stop a few characters more or less, but usually right
about that location.
If I run it through less than 850 (i.e. 800, even 845) it runs just fine.
Maybe it's a machine speed limitation? I'd be curious to know if others get
less or more on faster/slower PCs.
I'm on a 3Ghz Dell Precision with 256MB RAM.

--- Code Below ---

<HTML>
<HEAD>
<TITLE>Default HTA</TITLE>
<HTA:APPLICATION>
</HEAD>
<SCRIPT Language="VBScript">
Sub PutInSampleText()
Set WshShell = CreateObject("WScript.Shell")
textbox.focus
st = timer()
For i = 1 to 1000
WshShell.SendKeys i & "{ENTER}"
Next
End Sub
</SCRIPT>

<BODY onLoad=PutInSampleText>

<TEXTAREA COLS=40 ROWS=30 WRAP="off" ID="textbox"></TEXTAREA>

</BODY>

</HTML>


Monkey

unread,
Jun 20, 2005, 7:50:56 PM6/20/05
to
That's really weird. I ran it on my laptop, a 900 Mhz P3 with 512 Ram
and it did the same thing. It ran to around 850 and then died. I
don't think that it's a limitation of the machine.

Perhaps it is a buffer limitation somewhere...

Monkey

Jason

unread,
Jun 21, 2005, 8:58:15 AM6/21/05
to
Thanks Monkey for confirming it's not related to PC performance.

Another weird behavior is on refreshing. It will go further but still stall
at a certain point.
Change the top end of the for loop to 2000.
When it loads, it will still run up to 850 or so.
Refresh the HTA (F5 or right-click and refresh) and it will run up to about
1220.

Also it doesn't seem to be timing out based on the loop count, but by the
number of keys it is sending. For example, add a couple spaces or characters
to the SendKeys line:

WshShell.SendKeys i & " {ENTER}"

And it times out even earlier. (Adding 3 spaces it times out on line 566, on
refreshing line 730)

Very odd...

"Monkey" <mnr...@hotmail.com> wrote in message
news:1119311456.3...@g43g2000cwa.googlegroups.com...

Monkey

unread,
Jun 27, 2005, 1:56:34 AM6/27/05
to
I'm still not sure why it's doing this, but I found a work around. I
just use a synthetic sleep function to pause once every so often. I've
tried this and it works fine.

<HTML>
<HEAD>
<TITLE>Default HTA</TITLE>
<HTA:APPLICATION>
</HEAD>
<SCRIPT Language="VBScript">
Sub PutInSampleText()
Set WshShell = CreateObject("WScript.Shell")
textbox.focus

j = 0
For i = 1 to 3000
WshShell.SendKeys i & "{ENTER}"
j = j + 1
If j = 100 Then
ccSleep 1
j = 0
End If
Next
End Sub


Sub ccSleep(seconds)
set oShell = CreateObject("Wscript.Shell")
cmd = "%COMSPEC% /c ping -n " & 1 + seconds & " 127.0.0.1>nul"
oShell.Run cmd,0,1
End Sub


</SCRIPT>

<BODY onLoad=PutInSampleText>

<TEXTAREA COLS=40 ROWS=30 ID="textbox"></TEXTAREA>

</BODY>

</HTML>

Michael Reese

Joe Earnest

unread,
Jun 27, 2005, 11:06:03 AM6/27/05
to
Hi

[top post]

Good solution. All send-key operations are sensitive to timing and buffer
overruns. The AutoItX Send is more robust, if you can use an ActiveX. It
has different modes to reduce uneeded interpretation or provide more when
needed, includes keys and codes that SendKeys does not, and, significantly,
allows you to set the timing between each send. Slowing down the stream can
often avoid these problems. Your solution is as good as I've seen for
straight WSH operation, though it may have to be customized to different
machines.

Regards,
Joe Earnest

"Monkey" <mnr...@hotmail.com> wrote in message

news:1119851794.5...@g49g2000cwa.googlegroups.com...

0 new messages