473,387 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

[Linux] Detect a key press

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
3 7946
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Chris Williams | last post by:
Hi all, I am becoming a little stressed with the age-old problem of detecting when a user types (for example) a quote character ("). On a UK keyboard, I can look for a 2 + shift mask; on US,...
1
by: Roy | last post by:
Hi, I have a problem that I have been working with for a while. I need to be able from server side (asp.net) to detect that the file i'm streaming down to the client is saved...
0
by: Alex Taslab | last post by:
Hi everybody, does anyone know how to detect a key press from a keyboard. Well I do know how to do that, but i need to detect just one press and ignore the others. I mean, my program checks an...
2
by: ra88it | last post by:
I'm working on some javascript games, and I encountered what appears to be a bug specific to Firefox on Linux (I'm using several recent versions of Ubuntu). I'd love to get some help verifying...
8
by: asgarde | last post by:
hello, I'm new in Python and i would like to use Pexpect to execute a root command (i want to mount via a Pyhton script a drive) so that's my script for the moment : from os import *...
8
by: =?Utf-8?B?S2Vsdmlu?= | last post by:
Hi all, I am writing an application to detect the dead pixel of LCD panel by using visual C++ (MFC). My flow is follow: 1. Display a dialog with a start button. When the user press the start...
1
Nepomuk
by: Nepomuk | last post by:
In most modern distributions of Linux and Unix (at least ones with graphical environments like KDE, Gnome or XFCE), you get to mount your partitions easily from the desktop. In some cases however, it...
2
jwwicks
by: jwwicks | last post by:
C/C++ Programs and Debugging in Linux This tutorial will give you a basic idea how to debug a program in Linux using GDB. As you are aware Visual Studio doesn’t run on Linux so you have to use...
4
by: raylopez99 | last post by:
Compound question: first, and this is not easy, if there's a way to detect multiple simultaneous key presses in C# let me know (in the below code, keys c and d being pressed simultaneously or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.