[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Reading a keypress

chris.lyon

2/25/2008 6:36:00 PM

I'm trying to read a single keypress on Linux but expect to have the
programme running on Windows platform as well and find the mention in
the FAQ:

import termios, fcntl, sys, os
fd = sys.stdin.fileno()

oldterm = termios.tcgetattr(fd)
newattr = termios.tcgetattr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
termios.tcsetattr(fd, termios.TCSANOW, newattr)

oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)

try:
while 1:
try:
c = sys.stdin.read(1)
print "Got character", `c`
except IOError: pass
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)

However this fails on the second line as sys.stdin seems to have no
method fileno.
Any idea how I might proceed?
9 Answers

Dennis Lee Bieber

2/25/2008 7:07:00 PM

0

On Mon, 25 Feb 2008 10:35:54 -0800 (PST), wyleu
<chris.lyon@spritenote.co.uk> declaimed the following in
comp.lang.python:

> I'm trying to read a single keypress on Linux but expect to have the
> programme running on Windows platform as well and find the mention in
> the FAQ:

UNIX terminal control stuff snipped.

There is NO directly portable way to do low-level I/O with the
console.

On Windows you need to use the msvcrt module:

msvcrt.kbhit()
msvcrt.getch()
or .getche() if you want it to echo to the console
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/

Mike Driscoll

2/25/2008 7:14:00 PM

0

On Feb 25, 12:35 pm, wyleu <chris.l...@spritenote.co.uk> wrote:
> I'm trying to read a single keypress on Linux but expect to have the
> programme running on Windows platform as well and find the mention in
> the FAQ:
>
> import termios, fcntl, sys, os
> fd = sys.stdin.fileno()
>
> oldterm = termios.tcgetattr(fd)
> newattr = termios.tcgetattr(fd)
> newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
> termios.tcsetattr(fd, termios.TCSANOW, newattr)
>
> oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
> fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
>
> try:
> while 1:
> try:
> c = sys.stdin.read(1)
> print "Got character", `c`
> except IOError: pass
> finally:
> termios.tcsetattr(fd, termios.TCSAFLUSH, oldterm)
> fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
>
> However this fails on the second line as sys.stdin seems to have no
> method fileno.
> Any idea how I might proceed?

I've never done this sort of thing (except in wxPython), but with a
little Google-magic I found the following:

A recipe that supposedly does this in a cross-platform way:
http://aspn.activestate.com/ASPN/Cookbook/Python/Rec...

And a Windows only module:
http://effbot.org/librarybook/...

HTH

Mike

chris.lyon

2/25/2008 8:53:00 PM

0


>
> A recipe that supposedly does this in a cross-platform way:http://aspn.activestate.com/ASPN/Cookbook/Python/Rec...

class _Getch:
"""Gets a single character from standard input. Does not echo to
the
screen."""
def __init__(self):
try:
self.impl = _GetchWindows()
except ImportError:
self.impl = _GetchUnix()

def __call__(self): return self.impl()


class _GetchUnix:
def __init__(self):
import tty, sys

def __call__(self):
import sys, tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch


class _GetchWindows:
def __init__(self):
import msvcrt

def __call__(self):
import msvcrt
return msvcrt.getch()


getch = _Getch()



Sadly this also fails with:


Traceback (most recent call last):
File "<pyshell#2>", line 1, in -toplevel-
a = getch()
File "/home/chris/getch.py", line 10, in __call__
def __call__(self): return self.impl()
File "/home/chris/getch.py", line 19, in __call__
fd = sys.stdin.fileno()
AttributeError: fileno


What is fileno, and why might I not have it?







chris.lyon

2/25/2008 10:53:00 PM

0

Aaah it doesn't work from idle but it does from the command line...

Rolf van de Krol

2/26/2008 12:17:00 AM

0

wyleu wrote:
> Aaah it doesn't work from idle but it does from the command line...
>
>
You are right. You can't read STDIN from IDLE. There has been a topic
about that before:
http://groups.google.com/group/comp.lang.python/browse_thread/thread/9f9c90...

Jeff Schwab

2/26/2008 1:48:00 AM

0

Dennis Lee Bieber wrote:
> On Mon, 25 Feb 2008 10:35:54 -0800 (PST), wyleu
> <chris.lyon@spritenote.co.uk> declaimed the following in
> comp.lang.python:
>
>> I'm trying to read a single keypress on Linux but expect to have the
>> programme running on Windows platform as well and find the mention in
>> the FAQ:
>
> UNIX terminal control stuff snipped.
>
> There is NO directly portable way to do low-level I/O with the
> console.

What about curses?

http://docs.python.org/lib/module-c...
http://adamv.com/dev/pyth...

Dennis Lee Bieber

2/26/2008 5:31:00 AM

0

On Mon, 25 Feb 2008 17:48:21 -0800, Jeff Schwab <jeff@schwabcenter.com>
declaimed the following in comp.lang.python:
>
> What about curses?
>
> http://docs.python.org/lib/module-c...
> http://adamv.com/dev/pyth...

I don't consider needing a 3rd party library for Windows, but not
for UNIX/Linux a "portable" method...
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/

Jeff Schwab

2/26/2008 5:15:00 PM

0

Dennis Lee Bieber wrote:
> On Mon, 25 Feb 2008 17:48:21 -0800, Jeff Schwab <jeff@schwabcenter.com>
> declaimed the following in comp.lang.python:
>> What about curses?
>>
>> http://docs.python.org/lib/module-c...
>> http://adamv.com/dev/pyth...
>
> I don't consider needing a 3rd party library for Windows, but not
> for UNIX/Linux a "portable" method...

The Python module docs claim to support DOS without any kind of
extension. I don't know how well (or whether) it works with new
versions of Windows.

Dennis Lee Bieber

2/26/2008 7:11:00 PM

0

On Tue, 26 Feb 2008 09:15:02 -0800, Jeff Schwab <jeff@schwabcenter.com>
declaimed the following in comp.lang.python:


> The Python module docs claim to support DOS without any kind of
> extension. I don't know how well (or whether) it works with new
> versions of Windows.

Command shell on WinXP:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Dennis Lee Bieber>python
ActivePython 2.4.3 Build 12 (ActiveState Software Inc.) based on
Python 2.4.3 (#69, Apr 11 2006, 15:32:42) [MSC v.1310 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import curses
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "E:\Python24\lib\curses\__init__.py", line 15, in ?
from _curses import *
ImportError: No module named _curses
>>>

From the help file:
"""
While curses is most widely used in the Unix environment, versions are
available for DOS, OS/2, and possibly other systems as well. This
extension module is designed to match the API of ncurses, an open-source
curses library hosted on Linux and the BSD variants of Unix.
"""

My interpretation is: third party for anything that doesn't have
ncurses as a regular feature...
--
Wulfraed Dennis Lee Bieber KD6MOG
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/