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

Discussion in 'Python' started by GoogleGroups@garringer.net, Jul 14, 2005.

  1. Guest

    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.
     
    , Jul 14, 2005
    #1
    1. Advertising

  2. Benji York Guest

    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
     
    Benji York, Jul 16, 2005
    #2
    1. Advertising

  3. Guest

    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.)
     
    , Jul 18, 2005
    #3
  4. Benji York Guest

    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
     
    Benji York, Jul 19, 2005
    #4
  5. RTG Guest

    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?

    Thank you for your help.
     
    RTG, Jul 19, 2005
    #5
  6. Peter Hansen Guest

    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
     
    Peter Hansen, Jul 20, 2005
    #6
  7. RTG Guest

    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
     
    RTG, Jul 20, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lucas Tam
    Replies:
    5
    Views:
    6,239
    WizyDig
    Dec 2, 2004
  2. Replies:
    0
    Views:
    317
  3. Miktor
    Replies:
    7
    Views:
    12,060
    Joe Bacigalupa
    Aug 9, 2005
  4. Stedwick
    Replies:
    8
    Views:
    300
    Kajan Sittampalam
    Dec 25, 2008
  5. Babu Raj
    Replies:
    2
    Views:
    254
    Brian Candler
    Nov 26, 2009
Loading...

Share This Page