NewBie: How Can I Scan The Keyboard In Python?

Donn Cave donn at u.washington.edu
Fri Jan 12 14:29:45 EST 2001


Quoth Alain TESIO <alain at onesite.org>:
| On Fri, 12 Jan 2001 13:47:30 GMT, jbranthoover at my-deja.com wrote:
|> Hello Fredrik,
|> 	Thank you for your response.  I guess that neglected to mention
|> that I am running Python 1.5.2 on a Lynx (not linux) box.  I suppose
|> that the msvcrt module makes use of windows API calls and must run on a
|> windows based system?
|> 
|> 	Anyway thanks for the information.  If you have any other
|> Ideas,  please pass them along.  Have a great day...!
|
| I think you can use select to check if there is something in stdin.
| Look at "man select" for an example in C.

You can use select to check if there is something on the input unit,
file descriptor 0 (or any other file descriptor), if it's a device
that supports select (for example, not a disk file.)  The Python file
object sys.stdin is a buffer in process memory.  You can get the file
descriptor that sys.stdin will use to refill its buffer, and check if
there that unit is readable, but select won't tell you if stdin already
has data in its buffer.

Now in the case of a tty device (UNIX), the unit doesn't naturally
become readable the moment you press a key.  In its normal line mode,
the tty device driver has its own buffer, so it can apply back-spaces
and other edits.  Until you press Enter, no data.  When you want to
read keystrokes directly, you must change the device parameters with
POSIX terminal ioctls via termios.tcsetattr().  select() doesn't help
at all.  Whether any of this applies to LynxOS, I don't know;  it
doesn't even apply to UNIX keyboard in general, only the UNIX tty
device used by the console, telnet and similar programs.  (And other
POSIX 1003.1 platforms, like BeOS, but there's no guarantee that a
POSIX platform will actually use a tty device for anything, or even
have one.)

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list