pi************@gmail.com wrote:
I need a function (blocking or non-blocking) that tells me if a key has
been pressed (even before it has been released etc.). Also, I would of
course like to know _which_ key has been pressed.
I know that this probably does not exist in the Python library already
as a platform-independant abstraction (even though it probably could),
but then I would at least like solutions that works on Windows and on
Linux.
/David
Its a terminal I/O function - not a platform function. E.g. On Win only in a rough console msvcrt.kbhit() does it. In PythonWin, IPython, Crust ... things are of course different.
On regular Unix terminals you have the sys.stdin file:
sys.stdin.read(1) #maybe in a thread and interthread-pass it to your main loop
or possibly trick with fcntl.fcntl(sys.stdin, fcntl.F_SETFL, os.O_NDELAY|os.O_NONBLOCK)
when nothing is on sys.stdin - you get immediately an IOError:
>>sys.stdin.read(1)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IOError: [Errno 11] Resource temporarily unavailable
And see also other ioctl, termios, tty, cbreak, curses .. functions to get things early before \n buffering depending on the terminal mode )
Robert