[Linux] Detect a key press

Jia,Lu Roka100 at gmail.com
Fri Oct 6 00:45:47 EDT 2006


Hi all
  I write a program to detect key press,but , why there is a *space*
before the character I typed.??

#!/usr/bin/env python

import sys
import tty
import termios

i = sys.stdin.fileno()
o = sys.stdout.fileno()

backup = termios.tcgetattr(i)

def loop():
	while 1:
		ch = sys.stdin.read(1)
		print "->%s"%ch
		if ch == 'q':break

try:
	tty.setraw(i)
	loop()
finally:
	termios.tcsetattr(i, termios.TCSADRAIN, backup)




More information about the Python-list mailing list