Author Topic: Send Text to a hidden CMD-Window  (Read 458 times)

Gustav

  • Newbie
  • *
  • Posts: 2
Send Text to a hidden CMD-Window
« 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?

td

  • Tech Support
  • *****
  • Posts: 4444
    • WinBatch
Re: Send Text to a hidden CMD-Window
« Reply #1 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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade

Gustav

  • Newbie
  • *
  • Posts: 2
Re: Send Text to a hidden CMD-Window
« Reply #2 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...

td

  • Tech Support
  • *****
  • Posts: 4444
    • WinBatch
Re: Send Text to a hidden CMD-Window
« Reply #3 on: December 06, 2023, 09:32:10 am »
Assuming you are referring to the WIL RunHide or ShellExecute function, the following works:
Code: Winbatch
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

  • Tech Support
  • *****
  • Posts: 4444
    • WinBatch
Re: Send Text to a hidden CMD-Window
« Reply #4 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.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade