Reading a keypress

W

wyleu

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?
 
D

Dennis Lee Bieber

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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
M

Mike Driscoll

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/Recipe/134892

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

HTH

Mike
 
W

wyleu

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

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?
 
J

Jeff Schwab

Dennis said:
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.
 
D

Dennis Lee Bieber

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.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
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top