Hello,
I'd like to check, if a single key is pressed on a Linux xterm.
This code waits for a key to be pressed and returns the character:
--------------------------------------------
#!/usr/bin/env python
import sys
import tty
import termios
fd = sys.stdin.filen o()
old_settings = termios.tcgetat tr(fd)
def getOneKey():
try:
tty.setcbreak(s ys.stdin.fileno ())
ch = sys.stdin.read( 1)
return ord(ch)
finally:
termios.tcsetat tr(fd, termios.TCSADRA IN, old_settings)
while True:
a = chr(getOneKey() )
print a
--------------------------------------------
My problem is, I don't want my program to wait for the keypress.
I just want to check, if a key is currently pressed and if not, I'd like to
continue with my program (like "INKEY$" in some BASIC-dialects).
I tried several things:
- event-handling from pygame: But that would open a pygame-window, I don't
need.
- same thing for window-managers like Tkinter.
- threads: I couldn't do it (especially return values from thread-functions
to the main-program).
- curses: "nodelay()" or "halfdelay( )" sound interesting. Maybe; but don't
know how right now. I even wouldn't be able to "print" then ...
- python-Xlib: Too complicated for me too right now; perhaps, if
documentation becomes more complete.
Does anybody know a code example (for Linux xterm) that does it ?
TIA
H. 7 5242
Grant Edwards wrote:
On 2007-04-09, hlubenow <hl*******@gmx. netwrote:
>My problem is, I don't want my program to wait for the keypress. I just want to check, if a key is currently pressed and if not, I'd like to continue with my program (like "INKEY$" in some BASIC-dialects).
The answer to this frequently asked question is actually in the FAQ:
http://www.python.org/doc/faq/librar...ress-at-a-time
>
Google finds us further examples:
http://mail.python.org/pipermail/pyt...ry/010140.html http://mail.python.org/pipermail/pyt...ne/041251.html
You're answer is only less than half correct:
Most of the given examples use something like
c = sys.stdin.read( 1)
like my example does. This blocks input. At the end of your last link the
author there says it. He also shows some ways into my direction.
I'll test them.
H.
On 2007-04-10, hlubenow <hl*******@gmx. netwrote:
>>My problem is, I don't want my program to wait for the keypress. I just want to check, if a key is currently pressed and if not, I'd like to continue with my program (like "INKEY$" in some BASIC-dialects).
The answer to this frequently asked question is actually in the FAQ:
http://www.python.org/doc/faq/librar...ress-at-a-time
You're answer is only less than half correct:
Most of the given examples use something like
c = sys.stdin.read( 1)
like my example does. This blocks input.
read() will not block if the file has been set to non-blocking
mode. That's what these two lines in the FAQ answer do:
oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
I do make mistakes, but before telling somebody he's wrong, it
might be a good idea to actually try what he's suggested. ;)
--
Grant Edwards grante Yow! Edwin Meese made me
at wear CORDOVANS!!
visi.com
Grant Edwards wrote:
I do make mistakes, but before telling somebody he's wrong, it
might be a good idea to actually try what he's suggested. ;)
I completely agree. The script waited at first for key-input, so I thought,
I was right. But I was not. I apologize !
H.
I wrote:
Hello,
I'd like to check, if a single key is pressed on a Linux xterm.
My problem is, I don't want my program to wait for the keypress.
I just want to check, if a key is currently pressed and if not, I'd like
to continue with my program (like "INKEY$" in some BASIC-dialects).
Ok, here's the code I use now. Thanks to Grant Edwards for pointing me into
the right direction:
----------------------------------------------------------
#!/usr/bin/env python
import os
import sys
import tty
import termios
import fcntl
import time
fd = sys.stdin.filen o()
oldterm = termios.tcgetat tr(fd)
oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
tty.setcbreak(s ys.stdin.fileno ())
newattr = termios.tcgetat tr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
def oldTerminalSett ings():
termios.tcsetat tr(fd, termios.TCSAFLU SH, oldterm)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
def newTerminalSett ings():
termios.tcsetat tr(fd, termios.TCSANOW , newattr)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
def checkKey():
try:
c = sys.stdin.read( 1)
return ord(c)
except IOError:
return 0
print
print "Ok, in 3 seconds, I'll check 100 times, which key you press."
print
# Initializing: Things like "raw_input( )" won't work after that:
newTerminalSett ings()
time.sleep(3)
for i in range(100):
a = "Key pressed: "
key = checkKey()
if key:
a += chr(key)
a += "."
else:
a += "Nothing pressed."
print a
# Somehow it doesn't work, if this loop runs too fast, so:
time.sleep(0.05 )
oldTerminalSett ings()
print
print "Terminal-settings restored."
print
raw_input("raw_ input() works again. So please press Return: ")
print
----------------------------------------------------------
Thanks again
H.
On 2007-04-11, hlubenow <hl*******@gmx. netwrote:
I wrote:
>Hello,
I'd like to check, if a single key is pressed on a Linux xterm. My problem is, I don't want my program to wait for the keypress. I just want to check, if a key is currently pressed and if not, I'd like to continue with my program (like "INKEY$" in some BASIC-dialects).
Ok, here's the code I use now. Thanks to Grant Edwards for pointing me into
the right direction:
----------------------------------------------------------
#!/usr/bin/env python
import os
import sys
import tty
import termios
import fcntl
import time
fd = sys.stdin.filen o()
oldterm = termios.tcgetat tr(fd)
oldflags = fcntl.fcntl(fd, fcntl.F_GETFL)
tty.setcbreak(s ys.stdin.fileno ())
newattr = termios.tcgetat tr(fd)
newattr[3] = newattr[3] & ~termios.ICANON & ~termios.ECHO
def oldTerminalSett ings():
termios.tcsetat tr(fd, termios.TCSAFLU SH, oldterm)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags)
def newTerminalSett ings():
termios.tcsetat tr(fd, termios.TCSANOW , newattr)
fcntl.fcntl(fd, fcntl.F_SETFL, oldflags | os.O_NONBLOCK)
def checkKey():
try:
c = sys.stdin.read( 1)
return ord(c)
except IOError:
return 0
Ah, but how do you tell "no key" from ctrl-@ (which has a
ordinal value of 0)? If I were you, I'd return None in the case
where there is nothing to return:
def checkKey():
try:
return ord(sys.stdin.r ead(1))
except IOError:
return None
I'm also not sure if there are other possible causes for the
IOError exception that you'd need to watch out for. Ideally,
you'd check to make sure its an EAGAIN.
print
print "Ok, in 3 seconds, I'll check 100 times, which key you press."
print
# Initializing: Things like "raw_input( )" won't work after that:
newTerminalSett ings()
time.sleep(3)
for i in range(100):
a = "Key pressed: "
key = checkKey()
if key:
And here, you'd have this:
if key is not None:
a += chr(key)
a += "."
else:
a += "Nothing pressed."
print a
--
Grant Edwards grante Yow! When you get your
at PH.D. will you get able to
visi.com work at BURGER KING? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: apchar |
last post by:
I'm sitting here staring at my favorite poster. It's a photo of a ship
that hit a reef and sunk long ago but still has its ass-end sticking
out of the water. The caption reads "It may be that your sole purpose
in life is to serve as a warning to others." (checkout despair.com for
more of these jewels.) That's how I feel right now after spending (or
rather EXpending) the entire day looking for a PHP IDE for my
mandrake linux box. All I...
|
by: Adi |
last post by:
A java program we have written crashes with IBM JDK 1.3.1 on linux.
It works fine on other platforms(Solaris,HPUx). It gets a SIGSERV
Signal 11 and crashes just after few minutes after starting up. THe
program creates a good number of threads.
Any suggestions most welcome.
Have tried following
setting LD_ASSUME_KERNEL=2.2.5
disabling JIT by passing -Djava.compiler=NONE
|
by: Stelios Xanthakis |
last post by:
Hi.
I'm trying to open an xterm in slave mode connected
to a pseudo tty with an interactive python shell.
This can be useful when working in a GUI to pop up
xterm'd shells. The program below is a first attempt.
It opens an xterm with an interactive python interpreter
in which we can even run "os.system ('bash')" and
call vi. However there are problems...
|
by: shannonl |
last post by:
Hi all,
For some reason this bind is calling the donothing function, like it
should, but is then allowing the text to be inserted into the Text
widget.
Here is the code:
self.framebody.tag_config("name", underline=1)
self.framebody.tag_bind("name", "<Any-KeyPress>", self.donothing)
|
by: Alex007 |
last post by:
Hi,
I've been working on this C assignement for a CS course.... the
assignement is going pretty well and all my code works well on both
Windows and Linux.
The only thing that doesn't work is a stupid function that only has to
wait for a keypress from the user (it's a CLI program).
Here's what I have :
| |
by: jpaulus |
last post by:
I'd like to embed an xterm into a larger qt application running on
linux into which I can direct commands and allow users to interact with
existing scripts that would require their command line input. Does
anyone have code that they would like to share to help out a relative
python newbie?
thanks,
jack
|
by: valpa |
last post by:
I'm a net admin for about 20 unix servers, and I need to frequently
telnet on to them and configure them.
It is a tiring job to open a xterm and telnet, username, password to
each server.
Can I do it automatically by python? After that, there have 20 xterm
consoles opened and telneted to their corresponding servers. Then I
could start to type command in these xterms.
Any suggestion appreciate. Much thanks.
|
by: Roy, Anirban (Anirban) |
last post by:
Hi,
I am new to Pythion world.
My Objective : I want to open an Xterm and try to send message to that
xterm. I dont want to use Expect script.
I have written a small program
import os
|
by: dwelch91 |
last post by:
c.l.p-
I am undertaking writing an installer for a software package using
Python. It is exclusively for Linux. Because this is an installer and
has to run on numerous Linux distros, it is presenting some unique
challenges.
First off, I have begun this project by writing a text mode only
interface. I would like to provide a GUI as an alternative, but I ran
into problems in my early tests. Thinking that Tkinter would be the
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |