[Tutor] Need to be able to accept Page Down or CTRL-E
Alan Gauld
alan.gauld at btinternet.com
Fri Feb 1 09:34:25 CET 2013
On 01/02/13 03:19, Dave Wilder wrote:
>> So how are you taking this input in?
>
> I am using a terminal application and an application called pexpect
OK, We need to get the terminology a lot more exact.
I'm guessing that what you mean is that you have an application that
somebody else wrote running in a terminal session and that you are
writing a program to communicate with that application using the pexpect
module. Is that correct?
> (instead of raw_input) where I send something to the console
> and then look for a result,
When you say you send "something" to the console I'm guessing you mean
you send a command string to the external application via pexpect
and then read the response from the application coming back via
pexpect? Is that correct?
> such as <PAGE DOWN> or UP/DOWN arrows.
If I got the previous guesses right then this is, I suspect, impossible.
Pagedown and page up are terminal control characters that are not part
of the output that would be sent to pexpect. pexpect reads the monitored
session's stdin/stdout. There are some exceptional cases where you might
see those characters but in most cases they will be swallowed by the app
before pexpect ever sees them.
OTOH if you mean you want to send the page up/down/arrows as the
"something" then its a different game and you can do it. In that case
you need todo what you did with ESC. The character set your external app
uses will determine the codes you send. Assuming its the same set as
your local setup you can use the following program(untested!) to read
the codes from your keyboard (assuming you are on Windows):
import msvcrt
print "Hit space to end..."
print
while True:
ky = msvcrt.getch()
length = len(ky)
if length != 0:
if ky == " ": # check for quit event
print ord(ky)
raise SystemExit
else:
if ky == '\x00' or ky == '\xe0': # non ASCII
ky = msvcrt.getch() # fetch second character
print ord(ky),
There is also a curses version for Linux/MacOS on my tutor
topic page for event driven programming...
> I'm not sure if pexpect is standard Python application.
It's not an application it's a module. If you are using an app called
pexpect then its not part of Python (although it may still be written in
Python!)
hth
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list