[Tutor] two questions

Magnus Lycka magnus@thinkware.se
Thu Nov 7 06:51:16 2002


At 04:37 2002-11-07 -0500, gmlloyd wrote:
>Good morning.

04:37! Are you very early or very late? ;)

>(1) Is it possible to capture key-presses (like: page up, page down,
>aroow keys, etc.) in Python program? In other words, is there a Python
>equivalent of the BASIC Inkey$ function?

You should really tell us what OS you are using for such
a question. I'm really not sure how to do it on AS/400
for instance... ;)

Depending on your OS, there will be varying I/O features.
Unix-like operating systems use curses etc.

Surf to http://www.python.org/cgi-bin/faqw.py

Type "keypress" in the search field.

Press [Search] button

(In case this doesn't work, it's entries 4.74, 4.94 and 8.2
in the FAQ.)

For Windows, take a look in the Library Reference for the
msvcrt module.

>(2) Is it possible to change foreground or background colors within the
>Python command line interface? (i.e. not Idle)

I've seen it on unix at least. I'm not sure where...
Basically you should be able to send escape sequences
to stdout. I'm not sure exactly how though... Using
Win 2000 I tried:

 >>> x = '\x1B[2J' # Clear screen
 >>> print x

But that only prints a little left arrow followed by [2J.

Writing x to a file and typing it gives the same result.

 >>> f = file('esc.txt')
 >>> f.write(x)
 >>> f.close()

And then at the prompt

C:\> type esc.txt

Gives the same left-arrow followed by [2J.

BUT:

C:\> cat esc.txt

will clear the screen as expected! (cat is a unix command for
concatenating and displaying files. It's part of cygwin, which
is a needed toolkit on any Windows box.

So, there seems to be some kind of Windows buffering which
is beyond my understanding...


-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se