Sending keystrokes to Windows exe programs

Discussion in 'Python' started by Alex van der Spek, Apr 2, 2011.

  1. I can start a windows program on Vista with:

    >>> import subprocess
    >>> dva=subprocess.Popen(DVAname,stdin=subprocess.PIPE)


    Unfortunately sending keystrokes with communicate() does not appear to work:

    >>> dva.communicate('F2')


    this does not produce any result but it does make IDLE become really idle.

    >>> dva.terminate()


    however does work fine and kills the program as it should.

    Is there a way or do I have to go back to Visual Basic?

    Regards,
    Alex van der Spek
    Alex van der Spek, Apr 2, 2011
    #1
    1. Advertising

  2. On Sun, Apr 3, 2011 at 2:48 AM, Alex van der Spek <> wrote:
    > I can start a windows program on Vista with:
    >
    >>>> import subprocess
    >>>> dva=subprocess.Popen(DVAname,stdin=subprocess.PIPE)

    >
    > Unfortunately sending keystrokes with communicate() does not appear to work:
    >
    >>>> dva.communicate('F2')

    >
    > this does not produce any result but it does make IDLE become really idle.


    Amusing. :)

    >>>> dva.terminate()

    >
    > however does work fine and kills the program as it should.
    >
    > Is there a way or do I have to go back to Visual Basic?


    I've not used the subprocess module actually, and your post suggests
    that what I've been doing may be suboptimal, but in the Yosemite
    project I have this code:


    import win32api
    def dokey(key1,key2=None):
    win32api.keybd_event(key1,0,0,0)
    if key2!=None:
    win32api.keybd_event(key2,0,0,0)
    win32api.keybd_event(key2,0,2,0)
    win32api.keybd_event(key1,0,2,0)
    shift=16; ctrl=17; left=37; right=39; space=32

    To send Shift-Right Arrow, I call:
    dokey(shift,right)

    (This code multiplexes its options for cross-platform capabilities,
    taking advantage of the fact that Python allows you to define
    functions inside an if block.)

    win32api.keybd_event is a fairly "raw" call that just passes its
    parameters straight through to the underlying keybd_event Windows API.
    It doesn't send keys to a specific window, it instead sends keys to
    "whichever window currently has focus" (which is what I want for
    Yosemite). Not sure if that's suited to your needs.

    Chris Angelico
    Chris Angelico, Apr 2, 2011
    #2
    1. Advertising

  3. On Sun, Apr 3, 2011 at 2:48 AM, Alex van der Spek <> wrote:
    > I can start a windows program on Vista with:
    >
    >>>> import subprocess
    >>>> dva=subprocess.Popen(DVAname,stdin=subprocess.PIPE)

    >
    > Unfortunately sending keystrokes with communicate() does not appear to work:
    >
    >>>> dva.communicate('F2')

    >
    > this does not produce any result but it does make IDLE become really idle.


    I've just looked over the Python subprocess module. Is your subprocess
    (named by the variable DVAname) one which takes key names on STDIN and
    emits the appropriate keys?

    dva.communicate('F2') will send the two-character string "F2" to the
    STDIN of the process, and then wait for process termination. That's
    why IDLE stops dead. If you want to send it a string and then keep
    running, I think you want to use the stdin attribute:
    dva.stdin.write('F2')

    Chris Angelico
    Chris Angelico, Apr 2, 2011
    #3
    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. Steven Raasch

    Sending keystrokes to QT applications

    Steven Raasch, Jul 17, 2003, in forum: C++
    Replies:
    2
    Views:
    500
    Rolf Magnus
    Jul 17, 2003
  2. eryksun ()
    Replies:
    0
    Views:
    1,313
    eryksun ()
    Apr 3, 2011
  3. Ron Coutts
    Replies:
    0
    Views:
    122
    Ron Coutts
    Nov 11, 2003
  4. Kajan Sittampalam

    Sending Keystrokes to Flash Textbox

    Kajan Sittampalam, Dec 25, 2008, in forum: Ruby
    Replies:
    0
    Views:
    97
    Kajan Sittampalam
    Dec 25, 2008
  5. Zaidan
    Replies:
    1
    Views:
    425
    Vincent van Beveren
    Jun 9, 2004
Loading...

Share This Page