i am looking for a way to break a while True: when pressing "s" on my
keyboard. how can i do this?
pat
Ctrl-C
pat
I think you can use the 'curses' module to catch the keyboard event.
The name of the method is 'keyname()'. But I'm afraid curses is not
available on Windows Python, so you may have to be a bit more
imaginative. I just tried with Python 2.4 on Windows XP.
To leave the while loop, use the 'break' command.
Andy
That's not a very helpful response, since it doesn't address the OP's
request.
Patrick, the operating system "interrupt current process" keyboard
signal will cause Python to raise a 'KeyboardInterrupt' exception,
which unless caught will propagate back to the top-level Python
process and exit the program with a traceback.
On most operating systems, you can send this signal to the current
foreground process with the key combination Ctrl+C. I think that's
what Benjamin's laconic response is implying.
As for "detect a specific keypress", there's no OS-independent way to
do that. You'll need to write code that depends on a particular way of
getting at keypresses that won't work on all operating systems.
--
\ "Those who write software only for pay should go hurt some |
`\ other field." -- Erik Naggum, in _gnu.misc.discuss_ |
_o__) |
Ben Finney
> i am looking for a way to break a while True: when pressing "s" on my
> keyboard. how can i do this?
Windows only: Using the msvcrt module:
from msvcrt import kbhit, getch
a, b = 1, 1
while True:
a, b = a+b, a
print a
if kbhit() and getch() in 'sS':
print "\nThanks!"
break
There is a portable getch recipe in the Python Cookbook:
<http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/134892> that
supposedly should work on Windows, Linux and Mac OSX too.
If you just want to break the loop, and don't care too much in which state
your variables remain, you can use Ctrl-C and catch the KeyboardInterrupt:
a, b = 1, 1
try:
while True:
a, b = a+b, a
print a
except KeyboardInterrupt:
print "\nThanks!"
... continue program ...
--
Gabriel Genellina
> i am looking for a way to break a while True: when pressing "s" on my
> keyboard. how can i do this?
<http://groups.google.co.nz/groups?selm=fba8bo$331$1...@lust.ihug.co.nz>