Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Send alt key to subprocess.PIPE stdin

2,756 views
Skip to first unread message

Wanderer

unread,
Sep 11, 2013, 10:26:32 AM9/11/13
to
How do I send the command 'Alt+D' to subprocess.PIPE?

My code is

import subprocess
rsconfig = subprocess.Popen(["C:\Program Files\Photometrics\PVCam64\utilities\RSConfig\RSConfig.exe", ],stdin=subprocess.PIPE)

rsconfig.stdin.write('Alt+D')

Thanks

Nobody

unread,
Sep 11, 2013, 1:18:07 PM9/11/13
to
On Wed, 11 Sep 2013 07:26:32 -0700, Wanderer wrote:

> How do I send the command 'Alt+D' to subprocess.PIPE?

You don't. GUI programs don't read stdin, they receive key press events
from the windowing system.

Gary Herron

unread,
Sep 11, 2013, 1:43:09 PM9/11/13
to pytho...@python.org
That question doesn't really make sense. A pipe provides a method to
transfer a stream of bytes. You could indeed send that byte across the
pipe, but it's just a byte, written by one process and read by another
process. The receiving process can examine the byte stream, and do
whatever it wants in response.

By calling it a _command_, you seem to expect some particular behavior
out of the receiving process. Please tell us *what* that might be, and
we'll see what we can do to help out.

Gary Herron



--
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418

Wanderer

unread,
Sep 11, 2013, 4:15:48 PM9/11/13
to
I found Sendkeys works.

import subprocess
import time
import SendKeys

rsconfig = subprocess.Popen(["C:\Program Files\Photometrics\PVCam64\utilities\RSConfig\RSConfig.exe", ])
time.sleep(2)
SendKeys.SendKeys('%D\n', with_newlines=True)

Basically there is a dialog with a Done button and 'Alt D' selects the Done button. The subprocess program runs some interface code which sets up an ini file that the rest of the program needs. But then I run into another problem with Windows permissions. The program needs to edit this ini file located in the C:\Windows directory. Windows won't let you without administrator permissions. So then I run into login issues, etc. I kind of gave up and this program has to be run outside my main program. It's annoying since it's just this stupid little text ini file, but I can't convince Windows it doesn't matter if gets edited because it's in a system directory.

Dave Angel

unread,
Sep 11, 2013, 4:23:57 PM9/11/13
to pytho...@python.org
On 11/9/2013 10:26, Wanderer wrote:

> How do I send the command 'Alt+D' to subprocess.PIPE?

That's not a command, it's a keystroke combination. And without knowing
what RSConfig.exe is looking to get its keystrokes, it might not even be
possible to feed it any keystrokes via the pipe.

if the program does indeed use stdin, there's no portable encoding for
Alt-D. But if your program only runs on one particular platform, you
might be able to find docs for that platform that explain it.

>
> My code is
>
> import subprocess
> rsconfig = subprocess.Popen(["C:\Program Files\Photometrics\PVCam64\utilities\RSConfig\RSConfig.exe", ],stdin=subprocess.PIPE)

That string will only work by accident. You need to make it a raw
string, or use forward slashes, or double them. As it happens, with
this PARTICULAR set of directories, you won't get into trouble with
Python 2.7.3



--
DaveA


Wanderer

unread,
Sep 12, 2013, 10:06:01 AM9/12/13
to
Thanks, I didn't know that. I thought there would be some \n \t kind of combination or a unicode string for all the key combinations on my keyboard.

Chris Angelico

unread,
Sep 12, 2013, 11:27:53 AM9/12/13
to pytho...@python.org
On Fri, Sep 13, 2013 at 12:06 AM, Wanderer <wand...@dialup4less.com> wrote:
> Thanks, I didn't know that. I thought there would be some \n \t kind of combination or a unicode string for all the key combinations on my keyboard.

Unicode identifies every character, but keystrokes aren't characters.
Consider, for instance, the difference between the keypress Shift+A
and the letter produced - even in the most simple ASCII-only US-only
situation, that could produce either "A" or (if Caps Lock is active)
"a". So if you actually want to trigger Shift+A, you can't represent
that with a character. Controlling a GUI app has to be done with
keystrokes, so it needs a GUI controlling tool.

That said, though: These sorts of keystrokes often can be represented
with escape sequences (I just tried it in xterm and Alt-D came out as
"\e[d"), so you could control a console program using sequences that
you could put into a string. But that's not true of your typical GUI
system.

ChrisA

Nobody

unread,
Sep 12, 2013, 1:20:14 PM9/12/13
to
On Fri, 13 Sep 2013 01:27:53 +1000, Chris Angelico wrote:

> That said, though: These sorts of keystrokes often can be represented
> with escape sequences (I just tried it in xterm and Alt-D came out as
> "\e[d"),

Technically, that would be Meta-D (even if your Meta key has "Alt" printed
on it).

Alt-<char> produces chr(ord(char)+128); Meta-<char> produces "\e"+char.

At least, that's the historical distinction between Meta and Alt. With
xterm, the behaviour is configurable via the resources altIsNotMeta,
altSendsEscape and metaSendsEscape.

None of which has anything to do with trying to feed a GUI program key
events via stdin ...

0 new messages