Read one key press.

Gerrit Holl gerrit.holl at pobox.com
Mon Oct 18 14:19:08 EDT 1999


Adrian Eyre schreef:
> > key = raw_input("press a key: ")
> > if key == 'a':
> >     print "you pressed 'a'"
> > elif key == '^['
> >     print "you pressed escape"
> 
> This is a platform dependent issue.
> 
> For Win32, you can use:
> 
> import msvcrt
> key = msvcrt.getch()
> 
> For unix, it's probably easiest to use the curses module:
> 
> import curses
> win = curses.initscr()
> key = win.getch()
> I think then you're stuck with curses for all IO though.
> Not sure about this one.

Hmm. I think curses is the module I need, but I can't find documentation and
the module is written in c so I can't read that. If I run your example, I get
Traceback (innermost last):it:~/oefen/python$ python modcurse.py
                             File "modcurse.py", line 6, in ?
                                                                 key = curses.getch()
     AttributeError: getch
                          20:17:46:17/516:gerrit at gerrit:~/oefen/python$
and my terminal is totally wrong (if I press enter I don't get a newline, etc.)

The important part of dir(curses):
...
KEY_SUNDO', 'KEY_SUSPEND', 'KEY_UNDO', 'KEY_UP', '__doc__', '__file__', '__name__', '__version__', 'beep', 'cbreak', 'doupdate', 'echo', 'endwin', 'error', 'flash', 'flushinp', 'initscr', 'intrflush', 'isendwin', 'keyname', 'meta', 'newwin', 'nl', 'nocbreak', 'noecho', 'nonl', 'noraw', 'raw', 'ungetch', 'version']

Ah, there is some little documentation in the source.

I found it, it must be: win.getch()

Thanks!

regards,
Gerrit.
-- 
linuxgames.nl.linux.org	 All about games on Linux in Dutch (under construction).
www.nl.linux.org	       The Dutch resource for Dutch Linux documentation.
www.nl.linux.org/discoverb		    Learn foreign words and definitions.
www.nl.linux.org/~gerrit/asperger	 I'm an Asperger, go here for more info.
www.nl.linux.org/~gerrit			    About me and pictures of me.




More information about the Python-list mailing list