Home All Groups Group Topic Archive Search About

Sendkeys in hiden window

Author
8 Mar 2006 1:00 PM
Henrich
Hi, I just want to know how to send keys into hiden window - here is the code:

Private Sub command1_click()

    Dim PauseTime, Start, Finish, TotalTime
    Dim p As Variant

    cmd_line = Shell("c:\windows\system32\CMD.EXE", 0)

    PauseTime = 2   ' Set duration.
    Start = Timer   ' Set start time.

    Do While Timer < Start + PauseTime
        DoEvents
    Loop

    Finish = Timer   ' Set end time.
    TotalTime = Finish - Start   ' Calculate total time.

    p = "shutdown -s -f -t 1"
    SendKeys p, True
    SendKeys "{ENTER}", True
    SendKeys "exit", True
    SendKeys "{ENTER}", True

    PauseTime = 1   ' Set duration.
    Start = Timer   ' Set start time.

    Do While Timer < Start + PauseTime
        DoEvents
    Loop

    Finish = Timer   ' Set end time.
    TotalTime = Finish - Start   ' Calculate total time.
    Form1.Hide
    Unload Form1
    Exit Sub
End Sub

The problem is when the CMD is hiden it doesn't send keys but is opening
another window (CMD.EXE) and I must break the program with CTRL+BREAK or it
will still open anothers windows with CMD.

Thnx for any help.

Author
8 Mar 2006 5:40 PM
Karl E. Peterson
Henrich wrote:
> Hi, I just want to know how to send keys into hiden window - here is
> the code:

I'm curious why you're using SendKeys for this?  This would be a
straight-forward Shell directly to shutdown, rather than cmd.
--
Working without a .NET?
http://classicvb.org/


Show quoteHide quote
> Private Sub command1_click()
>
>     Dim PauseTime, Start, Finish, TotalTime
>     Dim p As Variant
>
>     cmd_line = Shell("c:\windows\system32\CMD.EXE", 0)
>
>     PauseTime = 2   ' Set duration.
>     Start = Timer   ' Set start time.
>
>     Do While Timer < Start + PauseTime
>         DoEvents
>     Loop
>
>     Finish = Timer   ' Set end time.
>     TotalTime = Finish - Start   ' Calculate total time.
>
>     p = "shutdown -s -f -t 1"
>     SendKeys p, True
>     SendKeys "{ENTER}", True
>     SendKeys "exit", True
>     SendKeys "{ENTER}", True
>
>     PauseTime = 1   ' Set duration.
>     Start = Timer   ' Set start time.
>
>     Do While Timer < Start + PauseTime
>         DoEvents
>     Loop
>
>     Finish = Timer   ' Set end time.
>     TotalTime = Finish - Start   ' Calculate total time.
>     Form1.Hide
>     Unload Form1
>     Exit Sub
> End Sub
>
> The problem is when the CMD is hiden it doesn't send keys but is
> opening another window (CMD.EXE) and I must break the program with
> CTRL+BREAK or it will still open anothers windows with CMD.
>
> Thnx for any help.
Author
9 Mar 2006 12:22 AM
Randy Birch
.... or just call ExitWindowsEx or InitiateSytemShutdown if targeting remote
systems.

--

Randy Birch
MS MVP Visual Basic
http://vbnet.mvps.org/

Please reply to the newsgroups so all can participate.




"Karl E. Peterson" <k***@mvps.org> wrote in message
news:%23w$ngdtQGHA.532@TK2MSFTNGP15.phx.gbl...
Henrich wrote:
> Hi, I just want to know how to send keys into hiden window - here is
> the code:

I'm curious why you're using SendKeys for this?  This would be a
straight-forward Shell directly to shutdown, rather than cmd.
--
Working without a .NET?
http://classicvb.org/


Show quoteHide quote
> Private Sub command1_click()
>
>     Dim PauseTime, Start, Finish, TotalTime
>     Dim p As Variant
>
>     cmd_line = Shell("c:\windows\system32\CMD.EXE", 0)
>
>     PauseTime = 2   ' Set duration.
>     Start = Timer   ' Set start time.
>
>     Do While Timer < Start + PauseTime
>         DoEvents
>     Loop
>
>     Finish = Timer   ' Set end time.
>     TotalTime = Finish - Start   ' Calculate total time.
>
>     p = "shutdown -s -f -t 1"
>     SendKeys p, True
>     SendKeys "{ENTER}", True
>     SendKeys "exit", True
>     SendKeys "{ENTER}", True
>
>     PauseTime = 1   ' Set duration.
>     Start = Timer   ' Set start time.
>
>     Do While Timer < Start + PauseTime
>         DoEvents
>     Loop
>
>     Finish = Timer   ' Set end time.
>     TotalTime = Finish - Start   ' Calculate total time.
>     Form1.Hide
>     Unload Form1
>     Exit Sub
> End Sub
>
> The problem is when the CMD is hiden it doesn't send keys but is
> opening another window (CMD.EXE) and I must break the program with
> CTRL+BREAK or it will still open anothers windows with CMD.
>
> Thnx for any help.
Author
9 Mar 2006 7:53 AM
Henrich
Hi, guys, don't take it personal but your answers are good for nothing. You
did not answer my question.

There is a reason why I am doing it this why, so PLEASE help, if you know
how to fix the problem. Thanks

Show quoteHide quote
"Randy Birch" wrote:

> .... or just call ExitWindowsEx or InitiateSytemShutdown if targeting remote
> systems.
>
> --
>
> Randy Birch
> MS MVP Visual Basic
> http://vbnet.mvps.org/
>
> Please reply to the newsgroups so all can participate.
>
>
>
>
> "Karl E. Peterson" <k***@mvps.org> wrote in message
> news:%23w$ngdtQGHA.532@TK2MSFTNGP15.phx.gbl...
> Henrich wrote:
> > Hi, I just want to know how to send keys into hiden window - here is
> > the code:
>
> I'm curious why you're using SendKeys for this?  This would be a
> straight-forward Shell directly to shutdown, rather than cmd.
> --
> Working without a .NET?
> http://classicvb.org/
>
>
> > Private Sub command1_click()
> >
> >     Dim PauseTime, Start, Finish, TotalTime
> >     Dim p As Variant
> >
> >     cmd_line = Shell("c:\windows\system32\CMD.EXE", 0)
> >
> >     PauseTime = 2   ' Set duration.
> >     Start = Timer   ' Set start time.
> >
> >     Do While Timer < Start + PauseTime
> >         DoEvents
> >     Loop
> >
> >     Finish = Timer   ' Set end time.
> >     TotalTime = Finish - Start   ' Calculate total time.
> >
> >     p = "shutdown -s -f -t 1"
> >     SendKeys p, True
> >     SendKeys "{ENTER}", True
> >     SendKeys "exit", True
> >     SendKeys "{ENTER}", True
> >
> >     PauseTime = 1   ' Set duration.
> >     Start = Timer   ' Set start time.
> >
> >     Do While Timer < Start + PauseTime
> >         DoEvents
> >     Loop
> >
> >     Finish = Timer   ' Set end time.
> >     TotalTime = Finish - Start   ' Calculate total time.
> >     Form1.Hide
> >     Unload Form1
> >     Exit Sub
> > End Sub
> >
> > The problem is when the CMD is hiden it doesn't send keys but is
> > opening another window (CMD.EXE) and I must break the program with
> > CTRL+BREAK or it will still open anothers windows with CMD.
> >
> > Thnx for any help.
>
>
>
>
Author
9 Mar 2006 9:29 AM
Mike Williams
"Henrich" <Henr***@discussions.microsoft.com> wrote in message
news:F78A2036-DC36-44EA-AD18-6D3305E93851@microsoft.com...

> Hi, guys, don't take it personal but your answers are good for
> nothing. You did not answer my question.

There are better ways of doing what you seem to be after, but even if you do
decide to stick with your own method there are better ways than SendKeys of
sending keystrokes. Also, there are better ways of phrasing your question.
Your specific choice of phraseology leads me to believe that you have
purchased a full service and support contract, but I can't find a record of
you having paid for this year's subscription so we can't really supply you
with an answer!

Mike
Author
9 Mar 2006 10:07 AM
Henrich
Excuse me, but my english skils are not so good as if was from english
speaking country, therefore I wrote DONT TAKE IT PERSONAL.

I dont want a full service, I just want a hint how to solve exacly this
problem - other options are maybe bether and simple but I want to do id this
way - there is REASON TO DO IT THIS WAY.

Thanks

Show quoteHide quote
"Mike Williams" wrote:

> "Henrich" <Henr***@discussions.microsoft.com> wrote in message
> news:F78A2036-DC36-44EA-AD18-6D3305E93851@microsoft.com...
>
> > Hi, guys, don't take it personal but your answers are good for
> > nothing. You did not answer my question.
>
> There are better ways of doing what you seem to be after, but even if you do
> decide to stick with your own method there are better ways than SendKeys of
> sending keystrokes. Also, there are better ways of phrasing your question.
> Your specific choice of phraseology leads me to believe that you have
> purchased a full service and support contract, but I can't find a record of
> you having paid for this year's subscription so we can't really supply you
> with an answer!
>
> Mike
Author
9 Mar 2006 10:27 PM
Karl E. Peterson
Hi Henrich --

> Hi, guys, don't take it personal but your answers are good for
> nothing. You did not answer my question.
>
> There is a reason why I am doing it this why, so PLEASE help, if you
> know how to fix the problem. Thanks

Likewise, no offense.  I personally answered the way I did, as I've used
that utility, and I honestly see no difference between what you're doing
(trying to, anyway) and what could be accomplished with a straight shell to
shutdown.

That said, there isn't a straight-forward answer to this question.  Sendkeys
will *not* work with a hidden window.  Period.  You could attempt to stuff
the keyboard buffer of the console window directly, but that's not gonna be
pretty.

If (and that's a bigass IF!) you can get the stdin handle in use by that
particular console, you could use WriteConsoleInput to send the keystrokes.
I've gotten this to work within consoles I've allocated myself, but never
tried with an alien console.

I guess what I'm really trying to say is, maybe it's time to re-examine your
percieved need.  I/we may be able to help with alternative suggestions, if
you could help us understand.  But, if this is your need, and that's that,
well, I think you're screwed.  No offense.

Good luck...   Karl
--
Working without a .NET?
http://classicvb.org/



Show quoteHide quote
>> "Karl E. Peterson" <k***@mvps.org> wrote in message
>> news:%23w$ngdtQGHA.532@TK2MSFTNGP15.phx.gbl...
>> Henrich wrote:
>>> Hi, I just want to know how to send keys into hiden window - here is
>>> the code:
>>
>> I'm curious why you're using SendKeys for this?  This would be a
>> straight-forward Shell directly to shutdown, rather than cmd.
>> --
>> Working without a .NET?
>> http://classicvb.org/
>>
>>
>>> Private Sub command1_click()
>>>
>>>     Dim PauseTime, Start, Finish, TotalTime
>>>     Dim p As Variant
>>>
>>>     cmd_line = Shell("c:\windows\system32\CMD.EXE", 0)
>>>
>>>     PauseTime = 2   ' Set duration.
>>>     Start = Timer   ' Set start time.
>>>
>>>     Do While Timer < Start + PauseTime
>>>         DoEvents
>>>     Loop
>>>
>>>     Finish = Timer   ' Set end time.
>>>     TotalTime = Finish - Start   ' Calculate total time.
>>>
>>>     p = "shutdown -s -f -t 1"
>>>     SendKeys p, True
>>>     SendKeys "{ENTER}", True
>>>     SendKeys "exit", True
>>>     SendKeys "{ENTER}", True
>>>
>>>     PauseTime = 1   ' Set duration.
>>>     Start = Timer   ' Set start time.
>>>
>>>     Do While Timer < Start + PauseTime
>>>         DoEvents
>>>     Loop
>>>
>>>     Finish = Timer   ' Set end time.
>>>     TotalTime = Finish - Start   ' Calculate total time.
>>>     Form1.Hide
>>>     Unload Form1
>>>     Exit Sub
>>> End Sub
>>>
>>> The problem is when the CMD is hiden it doesn't send keys but is
>>> opening another window (CMD.EXE) and I must break the program with
>>> CTRL+BREAK or it will still open anothers windows with CMD.
>>>
>>> Thnx for any help.
Author
9 Mar 2006 1:14 PM
R. MacDonald
Hello, Henrich,

I think that the problem here is with SendKeys itself.  SendKeys directs
its output to whichever Window has the focus.  This is a very unreliable
mechanism because you cannot guarantee which window this will be.  In
your case I think that it is even less useful, because I don't think
that a hidden window can ever have the focus.

You will probably need to use an API such as SendMessage for this.  This
will require the handle of the CMD window, but as I recall there is some
other routine to which you can pass "cmd_line" to get that.  Try a
google search for "SendMessage" (and also for "SendKeys alternatives" or
something like that) to get ideas about how to proceed.

By the way.  Your delay mechanism will turn into a pumpkin (i.e. create
problems) if used too close to midnight.

Cheers,
Randy


Henrich wrote:

Show quoteHide quote
> Hi, I just want to know how to send keys into hiden window - here is the code:
>
> Private Sub command1_click()
>
>     Dim PauseTime, Start, Finish, TotalTime
>     Dim p As Variant
>
>     cmd_line = Shell("c:\windows\system32\CMD.EXE", 0)
>
>     PauseTime = 2   ' Set duration.
>     Start = Timer   ' Set start time.
>
>     Do While Timer < Start + PauseTime
>         DoEvents
>     Loop
>
>     Finish = Timer   ' Set end time.
>     TotalTime = Finish - Start   ' Calculate total time.
>
>     p = "shutdown -s -f -t 1"
>     SendKeys p, True
>     SendKeys "{ENTER}", True
>     SendKeys "exit", True
>     SendKeys "{ENTER}", True
>
>     PauseTime = 1   ' Set duration.
>     Start = Timer   ' Set start time.
>
>     Do While Timer < Start + PauseTime
>         DoEvents
>     Loop
>
>     Finish = Timer   ' Set end time.
>     TotalTime = Finish - Start   ' Calculate total time.
>     Form1.Hide
>     Unload Form1
>     Exit Sub
> End Sub
>
> The problem is when the CMD is hiden it doesn't send keys but is opening
> another window (CMD.EXE) and I must break the program with CTRL+BREAK or it
> will still open anothers windows with CMD.
>
> Thnx for any help.
Author
9 Mar 2006 6:19 PM
Beall49
Im not quite sure what your problem is, but if your not able to focus
on certain windows you may want to use the AppActivate command. Using
that, you can call out specific windows for your sendkeys.

i.e. AppActivate "Microsoft Excel -(insert sheet name here)"