[Linux] Detect a key press

Sergei Organov osv at javad.com
Fri Oct 6 07:32:07 EDT 2006


"Jia,Lu" <Roka100 at gmail.com> writes:

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

There is none. The output I see when I type 1 2 q is:

->1
   ->2
      ->q

If that is what you see, the problem is in your

   print "->%s"%ch

statement. It implicitly outputs '\n' at the end, and when terminal is
set in raw mode, this is not translated into '\r\n' as when terminal is
in canonical mode.

Try

  print "->%s\r" % ch

or just

  sys.stdout.write(ch)

-- Sergei.




More information about the Python-list mailing list