Send Text to a hidden CMD-Window

Started by Gustav, December 05, 2023, 07:37:27 AM

Previous topic - Next topic

Gustav

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?

td

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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

Gustav

Thank you! The programme is running under "runhidden". My script has to run unattanded as a server task...

td

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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

td

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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade