Sending keystrokes to Windows exe programs

  • Thread starter Alex van der Spek
  • Start date
A

Alex van der Spek

I can start a windows program on Vista with:

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

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

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
 
C

Chris Angelico

I can start a windows program on Vista with:


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


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

Amusing. :)
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
 
C

Chris Angelico

I can start a windows program on Vista with:


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


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top