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

Check for keystroke

P: n/a
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
Share this Question
Share on Google+
7 Replies


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

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

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

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

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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.