473,320 Members | 1,825 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,320 software developers and data experts.

Check for keystroke

Inside a loop, I need to passively check to see if a key is pressed or not
without pausing like raw_input does. Some pseudocode:

While true:
If a key is pressed
which key is pressed

do other stuff inside loop
Google groups turned up this thread:

http://groups.google.com/groups?hl=e...8&safe=off&th=
3221aa1ee6ef2ec2&seekm=2259b0e2.0307310800.3310ec8 3%
40posting.google.com&frame=off

TinyURL: http://tinyurl.com/4chqh
But neither the pygame or curses solution worked. Any new thoughts on
this?
Jul 18 '05 #1
7 3681
Also, this is being done in Linux and does NOT need to be multi-OS
compatible.
Brian <Th*************@ddress.com> wrote in
news:Xn*************************@24.93.43.121:
Inside a loop, I need to passively check to see if a key is pressed or
not without pausing like raw_input does. Some pseudocode:

While true:
If a key is pressed
which key is pressed

do other stuff inside loop
Google groups turned up this thread:

http://groups.google.com/groups?hl=e...8&safe=off&th=
3221aa1ee6ef2ec2&seekm=2259b0e2.0307310800.3310ec8 3%
40posting.google.com&frame=off

TinyURL: http://tinyurl.com/4chqh
But neither the pygame or curses solution worked. Any new thoughts on
this?


Jul 18 '05 #2
Hi Brian,

I saw an example on pyserial called "miniterm.py". It was about doing a
text based chat between two computers connected with through the serial
port. It is made with threads, so you can do something else while it
waits for characters from the keyboard. When a key is pressed, then it
is echoed to the console. I think you can easilly remove the serial
stuff and do the check you need. Here is the link:

http://cvs.sourceforge.net/viewcvs.p...es/miniterm.py

Regards,
Josef

Brian wrote:
Inside a loop, I need to passively check to see if a key is pressed or not
without pausing like raw_input does. Some pseudocode:

While true:
If a key is pressed
which key is pressed

do other stuff inside loop
Google groups turned up this thread:

http://groups.google.com/groups?hl=e...8&safe=off&th=
3221aa1ee6ef2ec2&seekm=2259b0e2.0307310800.3310ec8 3%
40posting.google.com&frame=off

TinyURL: http://tinyurl.com/4chqh
But neither the pygame or curses solution worked. Any new thoughts on
this?

Jul 18 '05 #3
On Wed, 22 Sep 2004 14:37:56 GMT, Brian <Th*************@ddress.com> did write:
Inside a loop, I need to passively check to see if a key is pressed or not
without pausing like raw_input does.


I've had partial success on Mac OS X using termios (partial because
it seems to miss some keystrokes - also I haven't looked at this in
a while):

import termios
# in your method:
old = termios.tcgetattr(fd) # Old term info for restoration later
new = termios.tcgetattr(fd)
new[3] = new[3] & ~termios.ICANON & ~termios.ECHO
new[6][termios.VMIN] = 1
new[6][termios.VTIME] = 0
termios.tcsetattr(fd, termios.TCSANOW, new)

while 1:
try:
command = os.read(fd, 1)
print command+">", #Echo manually
if command == '0':
termios.tcsetattr(fd, termios.TCSADRAIN, old) #Terminal back to line mode
break
elif command == 'b':
# do something....
# and so on.....
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, old)

Jul 18 '05 #4
It took a little bit of coaxing, but I finally got it to run.
Unfortunately, it still pauses while waiting for a keystroke. Am I
missing something?

Fixed code:
import termios, sys, os
fd = sys.stdin.fileno()
old = termios.tcgetattr(fd) # Old term info for restoration later
new = termios.tcgetattr(fd)
new[3] = new[3] & ~termios.ICANON & ~termios.ECHO
new[6][termios.VMIN] = 1
new[6][termios.VTIME] = 0
termios.tcsetattr(fd, termios.TCSANOW, new)

while 1:
try:
command = os.read(fd, 1)
print command+">", #Echo manually
if command == 'p':
termios.tcsetattr(fd, termios.TCSADRAIN, old)
#Terminal back to line mode
break
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, old)

Lee Phillips <le*@leeHYPHENphillips.org.invalid> wrote in
news:sl****************@bad-bart.lcp.nrl.navy.mil:
import termios
# in your method:
old = termios.tcgetattr(fd) # Old term info for restoration
later new = termios.tcgetattr(fd)
new[3] = new[3] & ~termios.ICANON & ~termios.ECHO
new[6][termios.VMIN] = 1
new[6][termios.VTIME] = 0
termios.tcsetattr(fd, termios.TCSANOW, new)

while 1:
try:
command = os.read(fd, 1)
print command+">", #Echo manually
if command == '0':
termios.tcsetattr(fd, termios.TCSADRAIN, old)
#Terminal back to line mode
break
elif command == 'b':
# do something....
# and so on.....
finally:
termios.tcsetattr(fd, termios.TCSAFLUSH, old)


Jul 18 '05 #5
stdscr.nodelay(1)

Gah, I'm a retard.
Brian <Th*************@ddress.com> wrote in
news:Xn*************************@24.93.43.121:
Inside a loop, I need to passively check to see if a key is pressed or
not without pausing like raw_input does. Some pseudocode:

While true:
If a key is pressed
which key is pressed

do other stuff inside loop
Google groups turned up this thread:

http://groups.google.com/groups?hl=e...8&safe=off&th=
3221aa1ee6ef2ec2&seekm=2259b0e2.0307310800.3310ec8 3%
40posting.google.com&frame=off

TinyURL: http://tinyurl.com/4chqh
But neither the pygame or curses solution worked. Any new thoughts on
this?


Jul 18 '05 #6
On Thu, 23 Sep 2004 00:15:22 GMT, Brian <Th*************@ddress.com> did write:
It took a little bit of coaxing, but I finally got it to run.
Unfortunately, it still pauses while waiting for a keystroke. Am I
missing something?


No, I am - I didn't pay enough attention to what you said you wanted.
Jul 18 '05 #7
Brian <Th*************@ddress.com> writes:
stdscr.nodelay(1)

Gah, I'm a retard.


Another good option is select.poll() or select.select().

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 18 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ste | last post by:
Hello, my goal would be implementing some keystroke analysis using php. I'm a novice in php, so i ask your advice: according to you, is it possible to log each keystroke client-side even if php is...
7
by: hokieghal99 | last post by:
Does anyone know of a keystroke logger that has been written in Python for Windows machines? I'd like to see such a script and use it as a point of reference for a real-time backup project that I'm...
2
by: Wayne Wengert | last post by:
I want to get the decimal (integer) value of user keystrokes. I am using the following code. chrInput is the character entered (e.g. "M") and "keystroke is DIMed as Short keystroke =...
1
by: Amin Gunawan via .NET 247 | last post by:
I would like to put a combo box in a column of a datagrid. I've seen the samples in the MSDN library of other sources, butall samples didn't catch the 'DOWN/UP' keystroke. It means thatwhen I select...
15
by: Frank Bormann | last post by:
Hi, probably a stupid question, but I haven't been able to find anything. Is there a istream related function that let me read exactly one keystroke from the keyboard through cin? What I...
10
by: PythonistL | last post by:
I am a newbie with Javascript and would like to check if a text provided by a users , during filling in an input field , has digits only. Any idea? Thank you L
2
by: John | last post by:
We have an interesting programming problem and I wonder if anyone would know how to accomplish this, or be able to point me in the right direction I have a list of 100 items, in this case it's...
3
by: John Wright | last post by:
I have a VB program that will be generating documentation that will be stored for 60 years. This program really needs to have spell check and I read the post below on using spell check. I was...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.