By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,713 Members | 764 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,713 IT Pros & Developers. It's quick & easy.

[Linux] Detect a key press

P: n/a
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)

Oct 6 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On 5 Oct 2006 21:45:47 -0700, Jia,Lu <Ro*****@gmail.comwrote:
Hi all
I write a program to detect key press,but , why there is a *space*
before the character I typed.??
Puzzles me too, but I know this is really easy in Curses (on top of
that, it isn't specific to Linux; curses works on many terminals)

http://www.amk.ca/python/howto/curses/

-- Theerasak
Oct 6 '06 #2

P: n/a
"Jia,Lu" <Ro*****@gmail.comwrites:
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.

Oct 6 '06 #3

P: n/a
On 10/6/06, Sergei Organov <os*@javad.comwrote:
Try

print "->%s\r" % ch

or just

sys.stdout.write(ch)
Ah!
Oct 7 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.