WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: Gustav on December 05, 2023, 07:37:27 AM

Title: Send Text to a hidden CMD-Window
Post by: Gustav on December 05, 2023, 07:37:27 AM
I run a programm in a hidden CMD-Window (or PS-Window) that after starting awaits user input (in fact only a simple <ENTER>) in order to continue. With "SendKeysTo" it would work perfectly, but only as long as CMD (or PS) are visible. Is there any way to enter the <ENTER> to a hidden window?
Title: Re: Send Text to a hidden CMD-Window
Post by: td on December 05, 2023, 01:45:24 PM
It depends to some extent on what you mean by hidden but whatever you mean it is a bit of a problem. The foreground window is usually the window with the keyboard focus and with a few exceptions only the window with the focus can receive keyboard input. That is why SendKeysTo goes to a great deal of effort to make the target window the foreground window before sending any keys.

There are potential workarounds but those types of solutions are more than most WinBatch users would be willing to take on with no guarantee of success. I suppose there is a long shot that someone else has a sample script to do something like this.
Title: Re: Send Text to a hidden CMD-Window
Post by: Gustav on December 06, 2023, 01:06:01 AM
Thank you! The programme is running under "runhidden". My script has to run unattanded as a server task...
Title: Re: Send Text to a hidden CMD-Window
Post by: td on December 06, 2023, 09:32:10 AM
Assuming you are referring to the WIL RunHide or ShellExecute function, the following works:
Code (winbatch) Select
RunHide("C:\WINDOWS\system32\cmd.exe","")
TimeDelay(.5)
SendKeysTo("Administrator: C:\WINDOWS\system32\cmd.exe", "~")
; Used only to provide visual evidence that the window received the keys.
WinShow("Administrator: C:\WINDOWS\system32\cmd.exe")


While the above does the job you may wish to consider a different approach to your script's implementation. Many command prompt tasks can be performed by WinBatch using WIL/extender functions without a command prompt and UI manipulations.
Title: Re: Send Text to a hidden CMD-Window
Post by: td on December 07, 2023, 06:51:07 AM
Note that if by "server task" you mean a Windows service, you would definitely need to rethink your implementation. Windows services cannot interact with a Windows user desktop.