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

How do I send keystrokes to a console window in Windows XP?

2,064 views
Skip to first unread message

Google...@garringer.net

unread,
Jul 14, 2005, 1:22:56 PM7/14/05
to
How do I use Python to send keystrokes to a console window in Windows
XP?
Or perhaps there is an application that I can call to do this?

Thank you for your help.

Benji York

unread,
Jul 16, 2005, 8:46:34 AM7/16/05
to pytho...@python.org
Google...@garringer.net wrote:
> How do I use Python to send keystrokes to a console window in Windows
> XP?

import win32com.client

shell = win32com.client.Dispatch("WScript.Shell")
shell.AppActivate("Command Prompt")

shell.SendKeys("cls{ENTER}")
shell.SendKeys("dir{ENTER}")
shell.SendKeys("echo Hi There{ENTER}")
--
Benji York

Google...@garringer.net

unread,
Jul 18, 2005, 2:56:46 PM7/18/05
to
Thank you, Benji.

This gives me hope, but what I really need to do is to send keystrokes
to an <<<already existing>>> console window.

Any help there?

(P.S. Sorry that I wasn't more specific.)

Benji York

unread,
Jul 18, 2005, 10:47:52 PM7/18/05
to pytho...@python.org
Google...@garringer.net wrote:
> This gives me hope, but what I really need to do is to send keystrokes
> to an <<<already existing>>> console window.

That's exactly what the code does. Try it out, you'll see how it works
quickly enough.
--
Benji York


RTG

unread,
Jul 19, 2005, 10:47:35 AM7/19/05
to
Benji,

This appears to be exactly what we need.
I also see that by changing "Command Prompt" to "Notepad" or another
application, the key strokes are sent there.

With this capability, other possibilities open up.
Is there a way to read the output from the from the console window?
For example, how can we capture the output of the dir command?

Peter Hansen

unread,
Jul 19, 2005, 9:50:33 PM7/19/05
to
RTG wrote:
> With this capability, other possibilities open up.
> Is there a way to read the output from the from the console window?
> For example, how can we capture the output of the dir command?

Normally one does that using a call to things like os.popen, or using
the new subprocess module.

If that's not suitable (if for some bizarre reason you really do want to
pick up the output of "dir" after someone has run it manually in the
console, and I can't imagine why you would want that), then search in
the list archives for a recent thread that did investigate just that
issue: how to capture text from a console window in Win32. I don't
recall the answer but I'm sure you can find it.

-Peter

RTG

unread,
Jul 20, 2005, 9:53:49 AM7/20/05
to
Thank you, Peter.

The application is a <<<continuously running interactive program>>> and
we want to automatically interact with it (e.g. sendkeys and capture
certain text responses).

I will look for the thread you mentioned.

- Roy

jglobo...@gmail.com

unread,
Nov 28, 2017, 3:14:28 AM11/28/17
to
Hey!
Do you have any idea on how to open 2 command panels at the same time and that every command would write to 1 command panel and other command to 2 command panel, when i tried to do something like this:
shell.run("cmd")
shell2.run("cmd")

shell.AppActivate("cmd")
time.sleep(5)
shell.SendKeys('ffmpeg -y -f dshow -i video="Logitech HD Webcam C270" kamera'+datestring+'.mp4')
shell2.SendKeys("xxxccccccccccccc{ENTER}")
time.sleep(1)
shell.SendKeys("{ENTER}")



time.sleep(2)
ffmpeg -y -f dshow -i ffmpeg -y -f dshow -i video="Logitech HD Webcam C270" kamera.mp4video="Logitech HD Webcam C270" kamera.mp4
shell.SendKeys('^c')
time.sleep(2)
shell.SendKeys('exit')
time.sleep(1)
shell.SendKeys("{ENTER}")'''


everything gets in random places

jglobocnik37

unread,
Nov 28, 2017, 2:04:45 PM11/28/17
to

zxym...@gmail.com

unread,
Jan 8, 2018, 9:06:40 PM1/8/18
to
在 2005年7月16日星期六 UTC+8下午8:46:34,Benji York写道:
Recently, I tried `.AppActivate("Command Prompt")` but cannot catch the `cmd` on my Windows 7, the result is `False`. I know it has been some years since your reply, and the name of the window object may be different.(Still I'm pretty thankful to your answer.) Just wondering if there is a similar solution with another object name.

zxymike93

unread,
Jan 17, 2018, 5:56:53 PM1/17/18
to
еЈ" 2005е1'7жЈк16жщШжяfжЈfеа UTC+8д,пемк8:46:34пјоBenji YorkееЦйьфпјЬ
0 new messages