Check for keystroke

Brian ThisIsNotMyReal at ddress.com
Thu Sep 23 02:15:22 CEST 2004


It took a little bit of coaxing, but I finally got it to run.  
Unfortunately, it still pauses while waiting for a keystroke.  Am I 
missing something?

Fixed code:
import termios, sys, os
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd) # Old term info for restoration later
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)

while 1:
        try:
                command = os.read(fd, 1)
                print command+">", #Echo manually
                if command == 'p':
                        termios.tcsetattr(fd, termios.TCSADRAIN, old) 
#Terminal back to line mode
                        break
        finally:
                termios.tcsetattr(fd, termios.TCSAFLUSH, old)



Lee Phillips <lee at leeHYPHENphillips.org.invalid> wrote in
news:slrncl3upc.b6e.lee at bad-bart.lcp.nrl.navy.mil: 

> import termios
> # in your method:
>         old = termios.tcgetattr(fd) # Old term info for restoration
>         later 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)
> 
>         while 1:
>             try:
>                 command = os.read(fd, 1)
>                 print command+">", #Echo manually
>                 if command == '0':
>                     termios.tcsetattr(fd, termios.TCSADRAIN, old)
>                     #Terminal back to line mode 
>                     break
>                 elif command == 'b':
>                 # do something....
>                 # and so on.....
>             finally:
>                 termios.tcsetattr(fd, termios.TCSAFLUSH, old)
> 




More information about the Python-list mailing list