Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #2461 > unrolled thread

Sending keystrokes to Windows exe programs

Started by"Alex van der Spek" <zdoor@xs4all.nl>
First post2011-04-02 18:48 +0200
Last post2011-04-03 05:52 +1000
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  Sending keystrokes to Windows exe programs "Alex van der Spek" <zdoor@xs4all.nl> - 2011-04-02 18:48 +0200
    Re: Sending keystrokes to Windows exe programs Chris Angelico <rosuav@gmail.com> - 2011-04-03 05:42 +1000
    Re: Sending keystrokes to Windows exe programs Chris Angelico <rosuav@gmail.com> - 2011-04-03 05:52 +1000

#2461 — Sending keystrokes to Windows exe programs

From"Alex van der Spek" <zdoor@xs4all.nl>
Date2011-04-02 18:48 +0200
SubjectSending keystrokes to Windows exe programs
Message-ID<4d97536c$0$81483$e4fe514c@news.xs4all.nl>
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 

[toc] | [next] | [standalone]


#2468

FromChris Angelico <rosuav@gmail.com>
Date2011-04-03 05:42 +1000
Message-ID<mailman.137.1301773334.2990.python-list@python.org>
In reply to#2461
On Sun, Apr 3, 2011 at 2:48 AM, Alex van der Spek <zdoor@xs4all.nl> 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

[toc] | [prev] | [next] | [standalone]


#2469

FromChris Angelico <rosuav@gmail.com>
Date2011-04-03 05:52 +1000
Message-ID<mailman.139.1301773961.2990.python-list@python.org>
In reply to#2461
On Sun, Apr 3, 2011 at 2:48 AM, Alex van der Spek <zdoor@xs4all.nl> 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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web