[Tutor] Looking for suggestions for improving chessTimer.py code

bhaaluu bhaaluu at gmail.com
Mon Oct 29 12:17:10 CET 2007


# tty-example-2.py
# Tue Oct 19 09:07:14 CEST 1999
# Fredrik Lundh fredrik at pythonware.com
# http://mail.python.org/pipermail/python-list/1999-October/014151.html
import sys

try:
    # windows or dos
    import msvcrt
    getkey = msvcrt.getch
except ImportError:
    # assume unix
    import tty, termios

    print dir(termios)

    def getkey():
        file = sys.stdin.fileno()
        mode = termios.tcgetattr(file)
        try:
            tty.setraw(file, termios.TCSANOW)
            ch = sys.stdin.read(1)
        finally:
            termios.tcsetattr(file, termios.TCSANOW, mode)
        return ch

print "press 'q/p/r/s' to quit..."

while 1:
    ch = getkey()
    if ch == "q":
        break
    print ch,

print


On 10/29/07, Dick Moores <rdm at rcblue.com> wrote:
> At 03:44 AM 10/29/2007, Kent Johnson wrote:
> >http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/134892
>
> Kent, I'm still too dumb to see how to use that. Could you show a
> script using it that, say, prints 1 if  '1' is pressed and 2 if '2' is?
>
> Thanks,
>
> Dick
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>


-- 
b h a a l u u at g m a i l dot c o m
http://www.geocities.com/ek.bhaaluu/index.html


More information about the Tutor mailing list