Reading a keypress

Discussion in 'Python' started by wyleu, Feb 25, 2008.

  1. wyleu

    wyleu Guest

    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?
     
    wyleu, Feb 25, 2008
    #1
    1. Advertising

  2. On Mon, 25 Feb 2008 10:35:54 -0800 (PST), wyleu
    <> 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

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Feb 25, 2008
    #2
    1. Advertising

  3. On Feb 25, 12:35 pm, wyleu <> 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/Recipe/134892

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

    HTH

    Mike
     
    Mike Driscoll, Feb 25, 2008
    #3
  4. wyleu

    wyleu Guest


    >
    > 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?
     
    wyleu, Feb 25, 2008
    #4
  5. wyleu

    wyleu Guest

    Aaah it doesn't work from idle but it does from the command line...
     
    wyleu, Feb 25, 2008
    #5
  6. Rolf van de Krol, Feb 26, 2008
    #6
  7. wyleu

    Jeff Schwab Guest

    Dennis Lee Bieber wrote:
    > On Mon, 25 Feb 2008 10:35:54 -0800 (PST), wyleu
    > <> 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-curses.html
    http://adamv.com/dev/python/curses/
     
    Jeff Schwab, Feb 26, 2008
    #7
  8. Dennis Lee Bieber, Feb 26, 2008
    #8
  9. wyleu

    Jeff Schwab Guest

    Dennis Lee Bieber wrote:
    > On Mon, 25 Feb 2008 17:48:21 -0800, Jeff Schwab <>
    > declaimed the following in comp.lang.python:
    >> What about curses?
    >>
    >> http://docs.python.org/lib/module-curses.html
    >> http://adamv.com/dev/python/curses/

    >
    > 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.
     
    Jeff Schwab, Feb 26, 2008
    #9
  10. On Tue, 26 Feb 2008 09:15:02 -0800, Jeff Schwab <>
    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

    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: )
    HTTP://www.bestiaria.com/
     
    Dennis Lee Bieber, Feb 26, 2008
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?bWc=?=

    keypress event of TextBox - WebForm

    =?Utf-8?B?bWc=?=, Feb 8, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    12,318
    Marshal Antony
    Feb 8, 2004
  2. csgraham74

    Replicate keypress in code asp.net

    csgraham74, Jun 22, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    587
    Patrice
    Jun 22, 2005
  3. =?Utf-8?B?QXNzYW5LaGFuIElzbWFpbA==?=

    How to invoke keyPress event in ASP.Net pages

    =?Utf-8?B?QXNzYW5LaGFuIElzbWFpbA==?=, Oct 17, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    6,176
    Gary K
    Oct 17, 2005
  4. =?Utf-8?B?RHVzdHkgSGFja25leQ==?=

    KeyPress Event

    =?Utf-8?B?RHVzdHkgSGFja25leQ==?=, Feb 2, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    627
    Noldis Chumacero
    Feb 2, 2006
  5. Sard Aukary

    Non blocking keypress reading?

    Sard Aukary, Sep 8, 2006, in forum: Ruby
    Replies:
    3
    Views:
    187
    Michael Fellinger
    Sep 11, 2006
Loading...

Share This Page