Detección de pulsación de una tecla

Marcos Sánchez Provencio rapto en arrakis.es
Mie Sep 11 22:24:28 CEST 2002


Está en la FAQ, si lo quieres en castellano pídelo.
http://www.python.org/cgi-bin/faqw.py?req=show&file=faq04.074.htp
http://www.python.org/cgi-bin/faqw.py?req=show&file=faq08.002.htp
Python FAQ Entry
4.74. How to get a single keypress at a time?
For Windows, see question 8.2. Here is an answer for Unix.

There are several solutions; some involve using curses, which is a
pretty big thing to learn. Here's a solution without curses, due to
Andrew Kuchling (adapted from code to do a PGP-style randomness pool):

        import termios, sys, os
        fd = sys.stdin.fileno()
        old = termios.tcgetattr(fd)
        new = termios.tcgetattr(fd)
        new[3] = new[3] & ~termios.ICANON & ~termios.ECHO
        new[6][termios.VMIN] = 1
        new[6][termios.VTIME] = 0
        termios.tcsetattr(fd, termios.TCSANOW, new)
        s = ''    # We'll save the characters typed and add them to the
pool.
        try:
            while 1:
                c = os.read(fd, 1)
                print "Got character", `c`
                s = s+c
        finally:
            termios.tcsetattr(fd, termios.TCSAFLUSH, old)

You need the termios module for any of this to work, and I've only tried
it on Linux, though it should work elsewhere. It turns off stdin's
echoing and disables canonical mode, and then reads a character at a
time from stdin, noting the time after each keystroke.
------------------------------------------------------------------
8.2. How to check for a keypress without blocking?
Use the msvcrt module. This is a standard Windows-specific extensions in
Python 1.5 and beyond. It defines a function kbhit() which checks
whether a keyboard hit is present; also getch() which gets one character
without echo. Plus a few other goodies.

(Search for "keypress" to find an answer for Unix as well.) 


El mié, 11-09-2002 a las 18:48, Tomás Javier Robles Prado escribió:
> Hola a todos,
> 
> ¿Alguien sabe si existe en Python alguna función estilo keypressed de Pascal? 
> 
> while not tecla_pulsada() :
> 	instrucción 1
> 	instruccion 2
> 	...
> 
> Esa es la idea. Busco una solución para Unix/Linux, pero si existe una 
> multiplataforma, mucho mejor. Me sugirieron cosas como
> 
> def parada():
> 	cant = os.fstat(sys.stdin.fileno())[stat.ST_SIZE]
> 	if cant > 0:
> 		return 1
> 	else:
> 		return 0
> 
> o
> 
> import termios, sys, os 
> fd = sys.stdin.fileno() 
> old = termios.tcgetattr(fd) 
> new = termios.tcgetattr(fd) 
> new[3] = new[3] & ~termios.ICANON & ~termios.ECHO 
> new[6][termios.VMIN] = 1 
> new[6][termios.VTIME] = 0 
> termios.tcsetattr(fd, termios.TCSANOW, new) 
> s = ''    # We'll save the characters typed and add them to the pool. 
> try: 
>      while 1: 
>           c = os.read(fd, 1) 
>           print "Got character", `c` 
>           s = s+c 
> finally: 
>      termios.tcsetattr(fd, termios.TCSAFLUSH, old) 
> 
> pero ninguna de las dos sirven. Quizá una combinación de ambas... ¿alguna 
> idea?
> 
> Gracias
> -- 
> Un saludo,
> T. Javier Robles Prado tjavier en usuarios.retecal.es
> http://users.servicios.retecal.es/tjavier
> 
> _______________________________________________
> Python-es mailing list
> Python-es en aditel.org
> http://listas.aditel.org/listinfo.py/python-es






Más información sobre la lista de distribución Python-es