Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'api.': 0.05; 'passes': 0.05; 'subject:Sending': 0.07; 'python': 0.07; '>>>>': 0.09; 'subprocess': 0.09; 'sun,': 0.09; 'underlying': 0.09; 'def': 0.13; 'am,': 0.14; 'wrote:': 0.14; 'call:': 0.16; 'keystrokes': 0.16; 'should.': 0.16; 'subject:exe': 0.16; 'win32api': 0.16; 'fine': 0.18; 'idle': 0.19; 'appear': 0.19; '(which': 0.21; 'code': 0.22; 'header:In-Reply-To:1': 0.22; '(this': 0.22; 'keys': 0.23; 'received:209.85.214.174': 0.23; 'received:mail- iw0-f174.google.com': 0.23; 'suggests': 0.23; 'vista': 0.25; 'define': 0.26; 'parameters': 0.26; 'windows': 0.26; 'instead': 0.26; 'chris': 0.27; 'van': 0.27; 'message-id:@mail.gmail.com': 0.28; "doesn't": 0.28; 'subject:Windows': 0.29; 'sends': 0.29; 'work:': 0.29; 'does': 0.31; 'fact': 0.31; 'import': 0.32; 'to:addr:python-list': 0.32; 'project': 0.32; "i've": 0.33; 'fairly': 0.33; 'module': 0.33; 'options': 0.34; 'post': 0.34; 'there': 0.35; 'alex': 0.35; 'doing': 0.36; 'however': 0.37; 'received:209.85': 0.37; 'apr': 0.38; 'received:google.com': 0.38; 'but': 0.38; 'code:': 0.38; 'used': 0.38; 'received:209.85.214': 0.39; 'to:addr:python.org': 0.39; 'received:209': 0.39; 'header:Received:5': 0.40; 'allows': 0.40; 'straight': 0.60; 'taking': 0.61; 'back': 0.61; '2011': 0.62; 'become': 0.70; 'kills': 0.84; 'window,': 0.84; 'skip:d 50': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:in-reply-to:references:date :message-id:subject:from:to:content-type; bh=wY5hFQrjXfzz9FK2d0wOoG0IbNQh3mvnPel/K8rMnkk=; b=aYBRZn/hilJp1VQywFslNA+aGIJAa0WKI0X4bTiESAUk8tPDxggf5GkPTumPcXQhGR jNm+Kz22U7oXjmck4BvCzKNI0tKS0Va0ASSUYVsq/+S8xka2Py8TuF3yoM4p2MJAm4Op pfJxjJPe3Cp7oXXrDu/0+bWnj+JNHDjeb1Rhk= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; b=iEgUH0x19/0SH6+qsziIXtQd4TZKVjrQMuTLWIZoZ5oRg45Of/exIPOAznslBMo6ZE qig4TMFWgYEHr4tsHvREPaq9tLafy0N33Ov2wA4l366EocqNR3gOtOIY9ON63UFZddPS 3el27OQB1u+CZIQzFyoY9OCWwLagoR3qclIQ4= MIME-Version: 1.0 In-Reply-To: <4d97536c$0$81483$e4fe514c@news.xs4all.nl> References: <4d97536c$0$81483$e4fe514c@news.xs4all.nl> Date: Sun, 3 Apr 2011 05:42:10 +1000 Subject: Re: Sending keystrokes to Windows exe programs From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 48 NNTP-Posting-Host: 82.94.164.166 X-Trace: 1301773334 news.xs4all.nl 41114 [::ffff:82.94.164.166]:53998 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:2468 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