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

bhaaluu bhaaluu at gmail.com
Mon Oct 29 13:03:58 CET 2007


On 10/29/07, Dick Moores <rdm at rcblue.com> wrote:
> Seems I should clarify that. I'm not looking for a script that echoes
> the key pressed. So I'll change that last line to "Could you show a
> script using it that, say, prints "Hello" if  '1' is pressed and
> "Bye" if '2' is, AND uses
> <http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/134892>
> Dick

# 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
   elif ch == "1":
       print "Hello",
   elif ch == "2":
       print "Bye",
   print ch,

print


Like that?
-- 
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